Skip to content

chore: update clap to v4 with derive API#37

Open
Allan Zhang (allan2) wants to merge 2 commits intomasterfrom
clap-v4
Open

chore: update clap to v4 with derive API#37
Allan Zhang (allan2) wants to merge 2 commits intomasterfrom
clap-v4

Conversation

@allan2
Copy link

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

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)
}

Copy link
Contributor

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

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants