bfil / toml-config

Loads and decodes TOML files into Rust structures
https://bfil.github.io/toml-config
MIT License
13 stars 2 forks source link

Status of this crate #4

Open dtolnay opened 7 years ago

dtolnay commented 7 years ago

https://github.com/serde-rs/serde/pull/780 added support for a #[serde(default)] attribute on structs to use the Default implementation of the struct to fill in any missing fields.

The example from the toml-config rustdoc would look something like:

#[derive(Deserialize)]
struct Config  {
    nested: NestedConfig,
}

#[derive(Deserialize)]
#[serde(default)]
struct NestedConfig {
    value: String,
    values: Vec<u16>,
}

impl Default for NestedConfig {
    fn default() -> NestedConfig {
        NestedConfig {
            value: "default".to_owned(),
            values: vec![0, 0, 0],
        }
    }
}

// then use the toml deserializer as normal

Is there any other functionality of this crate that you think may be helpful for us to expose directly in Serde so that it applies consistently across all data formats?

bfil commented 7 years ago

The default support does look interesting, at the moment what I'm doing is using the whole default config struct and apply the generic toml::Table values on top of it (https://github.com/bfil/toml-config/blob/master/src/lib.rs#L192).

When I have some time I'll bump to the latest version of serde and will try to flip things around and see if it would be possible to remove the apply_overrides method altogether, it would be nice and it would pretty much make this library almost pointless (apart from the rustc_serialize/serde feature switch support).

I'll have a play around when I can and see if there's anything else that could be necessary or nice to have, but I doubt it because it's pretty much the core of what this library does.