-
Notifications
You must be signed in to change notification settings - Fork 1
chore: update clap to v4 with derive API #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
The settings module is updated. More info is shown in `--help`. For example, the default value for format is now shown. The log level stays lowercase in the config file by using a custom serializer/deserializer. ``` Usage: prux [OPTIONS] Options: -c, --config <CONFIGFILE> -l, --level <LOGLEVEL> -p, --port <LISTENER_PORT> -u, --uri <SERVER_URI> -i, --maxmindid <MAXMIND_ID> -s, --maxmindpass <MAXMIND_PASSWORD> --save-config <SAVE_CONFIG> --format <FORMAT> [default: TOML] [possible values: TOML, YAML, JSON] --show-config -h, --help Print help -V, --version Print version ```
pub struct Settings { | ||
pub loglevel: String, | ||
/// The log level. | ||
/// | ||
/// It is saved in the configuration file as lowercase. | ||
#[serde( | ||
serialize_with = "ser_lowercase", | ||
deserialize_with = "de_capitalize_first_letter" | ||
)] | ||
pub loglevel: LevelFilter, | ||
pub server: Server, | ||
pub listener: Listener, | ||
} | ||
|
||
/// Serializes a value to a lowercase string. | ||
fn ser_lowercase<S, T>(value: &T, serializer: S) -> result::Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
T: fmt::Display, | ||
{ | ||
let s = value.to_string().to_lowercase(); | ||
serializer.serialize_str(&s) | ||
} | ||
|
||
fn de_capitalize_first_letter<'de, D, T>(deserializer: D) -> result::Result<T, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
T: FromStr, | ||
T::Err: fmt::Display, | ||
{ | ||
let s = String::deserialize(deserializer)?; | ||
let mut chars = s.chars(); | ||
let normalized = match chars.next() { | ||
Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(), | ||
None => return Err(serde::de::Error::custom("empty string")), | ||
}; | ||
normalized.parse().map_err(serde::de::Error::custom) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd just use a serde alias here, much simpler. https://serde.rs/field-attrs.html#alias
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's about lowercasing the value, not the field name. The current version stores it as a lowercase string like info
so I wanted to keep it the way it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@allan2 In that case if you want to preserve the previous behavior while using a custom serializer, you should lowercase the rest of the string. As currently we would always toLower the whole string, you could mix and match casing it would work. This would theoretically introduce a breaking change.
The settings module is updated. More info is shown in
--help
.For example, the default value for format is now shown.
The log level stays lowercase in the config file by using a custom serializer/deserializer.