#config-file #ilo #disk #serialization #run-time #serde

ilo-config

Library for maintaining configs on disk in a simple, ergonomic way

3 unstable releases

0.2.1 May 3, 2024
0.2.0 May 3, 2024
0.1.0 Apr 7, 2024

#277 in Configuration

Download history 17/week @ 2024-07-28 22/week @ 2024-09-22 6/week @ 2024-09-29

107 downloads per month

MIT license

16KB
127 lines

ilo-config

Library for maintaining configs on disk in a simple, ergonomic way.

Quickstart

  1. Create a new Cargo binary package.

    cargo new ilo-config-example && cd ilo-config-example
    
  2. Install the dependencies. serde and reqwest are only needed for the example code; they're not necessary for using the library in general.

    cargo add ilo-config
    
    cargo add serde -F derive
    cargo add reqwest -F blocking
    
  3. Paste the example code into src/main.rs.

    use ilo_config::Config;
    use serde::{Deserialize, Serialize};
    
    #[derive(Debug, Serialize, Deserialize, Default)]
    struct QuickstartConfig {
        /// URL to which a GET request should be made
        url: Option<String>,
    
        /// Additional text to be saved in the config, e.g. reminder of how the file got created
        comment: Option<String>,
    }
    
    fn main() -> Result<(), Box<dyn std::error::Error>> {
        let mut config: Config<QuickstartConfig> = Config::load("example-config")?;
        let data = config.data_mut();
        if data.comment.is_none() {
            data.comment = Some(String::from(
                "Created by the ilo-config package's quickstart example.",
            ));
        }
    
        let url: &str = data
            .url
            .get_or_insert_with(|| String::from("https://httpbin.org/get"));
        let response = reqwest::blocking::get(url)?;
        let text = response.text()?;
    
        println!("Response from configured URL: {text}");
    
        config.save().map_err(|e| e.into())
    }
    
  4. Run export ILO_CONFIG_HOME=$(pwd) so that config files are created in the current directory instead of in the default directory (which is ~/.config/ilo).

  5. Run the example with cargo run.

  6. (Optional) Inspect the generated example-config.json file. Try changing the URL to, say, https://httpbin.org/headers and rerunning the program, or replacing the URL with a number and verifying that the type mismatch causes an error at runtime.

For more examples, see the examples/ directory.

Dependencies

~0.7–8.5MB
~72K SLoC