curlyconf

Apache-2.0 licensed MIT licensed crates.io Released API docs

Curlyconf

Curlyconf is a configuration file reader for the configuration file format used by, for example, named.conf and dhcpd.conf.

Example config (file.cfg)

person charlie { fullname "Charlie Brown"; address 192.168.1.1; } person snoopy { fullname "Snoopy"; }

Example code

```rust use serde::Deserialize;

// The initial section of any config is a rust struct.

[derive(Debug, Deserialize)]

struct Config { person: Vec, }

[derive(Debug, Deserialize)]

struct Person { #[serde(rename = "label")] name: String, #[serde(default)] fullname: Option, #[serde(default)] address: Option, }

fn main() { // Read the configuration file. let config: Config = match curlyconf::from_file("file.cfg") { Ok(cfg) => cfg, Err(e) => { eprintln!("{}", e); std::process::exit(1); } };

// Print what we got (println!("{:?}", config) would be easier...).
for (i, p) in config.person.iter().enumerate() {
    println!("{}: {} fullname {:?} addr {:?}", i, p.name, p.fullname, p.address);
}

}

```

This will print:

0: charlie fullname Some("Charlie Brown") addr Some(V4(192.168.1.1)) 1: snoopy fullname Some("Snoopy") addr None

Curlyconf uses serde to deserialize the configuration file values to rust types, just like almost every other crate that does something similar.

Sections and values.

The configuration file contains section names, labels, sections, value names, and values:

A section can only have a label if:

The label type can be any type, it does not have to be a string - it could also be, for example, a PathBuf or IpAddr.

The basic structure of a config file is thus:

section_name [label] { value_name value [,value...]; value_name value [,value...]; section_name [label] { value_name value [,value...]; } }

Enums are also supported (see the serde docs) so you can do things like:

```rust

[derive(Debug, Deserialize)]

struct Config { animal: Animal, }

[derive(Debug, Deserialize)]

enum Animal { Cat { purrs: bool, }, Dog { barks: bool, }, } ```

And then have a config like

animal cat { purrs; }

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.