SergioBenitez / Figment

A hierarchical configuration library so con-free, it's unreal.
Apache License 2.0
604 stars 35 forks source link

Pattern for mixing required fields and optional fields with defaults #77

Closed jalaziz closed 1 year ago

jalaziz commented 1 year ago

I apologize if this should be trivial as I'm relatively new to Rust.

Is there a recommended pattern for defining defaults for a config struct where some fields should be optional with defaults but others must be passed in by the user?

For example, given the struct:

#[derive(Deserialize, Serialize)]
pub struct Config {
    host: String,
    port: u16,
    required_field String: 
}

I would like to provide defaults for everything except required_field, e.g.:

impl Default for Config {
    fn default() -> Self {
        Self {
            host: "[::]".to_string(),
            port: 8080,
        }
    }
}

Unfortunately, this won't work due to having to define all fields on a struct. Using Options doesn't solve the problem because then figment treats them as optional fields.

I was looking into serdes's defaults as an option, but the lack of literals support makes things pretty annoying.

I assume another option would be to merge in a Defaults struct that defines all but the optional fields.

Is there a recommended pattern? Is there another, simpler option that I'm missing?

slinkydeveloper commented 1 year ago

My pattern for doing this is to:

SergioBenitez commented 1 year ago

If you don't want to use serde's default attribute, nor create a secondary struct, the simplest option, albeit unsatisfying, is to merge the default values into a Figment manually:

Figment::new()
    .merge(Serialized::default("host", ...))
    .merge(Serialized::default("port", ...))

Extracting your type from this Figment (with the desired providers added) would then only require the required field(s).

You could also apply this idea in reserve by serializing your default struct with all of its field into a figment, setting the required fields to None, and then extracting from there.

Unfortunately I don't know of a better solution in Rust. There is unfortunately no way to say "give me a struct with the same fields as this struct expect exclude these," which is what we'd need here. You could theoretically write a procedural derive to accomplish this, though I'm not aware of a crate that does this, and a quick search turned up empty, but perhaps you'd have more luck.

jalaziz commented 1 year ago

You could also apply this idea in reserve by serializing your default struct with all of its field into a figment, setting the required fields to None, and then extracting from there.

For this approach, do you mean have default that sets all fields (potentially with some dummy values), then use Serialized::default("port", ...) to set the override and set required fields to None?

Unfortunately I don't know of a better solution in Rust.

Yeah, I tried searching myself and it's not an easy thing to search for. I'm also not convinced it would be any cleaner.

I ultimately went with serde's default feature. Even though it means I have to define functions for every default, it's a scalable pattern. I didn't use Serialized::defaults(...) though, since that would require all the fields to be set.

Thanks everyone for the input. Always good to confirm I'm not missing the obvious 😄.