chore: update clap to v4 with derive API#37
Open
Allan Zhang (allan2) wants to merge 2 commits intomasterfrom
Open
chore: update clap to v4 with derive API#37Allan Zhang (allan2) wants to merge 2 commits intomasterfrom
Allan Zhang (allan2) wants to merge 2 commits intomasterfrom
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
```
Comment on lines
113
to
+150
| 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) | ||
| } | ||
|
|
Contributor
There was a problem hiding this comment.
I'd just use a serde alias here, much simpler. https://serde.rs/field-attrs.html#alias
Author
There was a problem hiding this comment.
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.
Contributor
There was a problem hiding this comment.
Allan Zhang (@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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.