rust-cli / confy

🛋 Zero-boilerplate configuration management in Rust
Other
907 stars 61 forks source link

How to use non Caps or non Camelcase in variants #90

Closed adrianboston closed 10 months ago

adrianboston commented 10 months ago

In my properties i have the following enum for a datetime. Which uses camelcase

pub enum DateTimeFormat {
    Rfc3339,
    Rfc2822,
    Epochms
}

My application toml file sets the property as such

datetime_format = 'Rfc3339'

If I use non cap

datetime_format = 'rfc3339'

it fails at

confy::load_path(inpath)

Is there an acceptable way of rectifying this in a deserializer

deg4uss3r commented 10 months ago

This is possible with a serde variant attribute rename_all

Changing your DateTimeFormat enum to:

#[derive(Default, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DateTimeFormat {
    #[default]
    Rfc3339,
    Rfc2822,
    Epochms,
}

will allow a config like the one below to deserialize correctly:

version = 0
datetime_format = 'rfc3339'
adrianboston commented 10 months ago

Thanks for the good pointer. Instead, I went with lowercase and suppressed the warning using

#[allow(non_camel_case_types)]

This offers a better solution and I will revert back to CamelCase.