Skip to main content

#
typed_derive/
implicit.rs

    1use clap::Args;
    2use clap::ValueEnum;
    3
    4#[derive(Args, Debug)]
    5pub(crate) struct ImplicitParsers {
    6    /// Implicitly using `std::str::FromStr`
    7    #[arg(short = 'O')]
    8    optimization: Option<usize>,
    9
    10    /// Allow invalid UTF-8 paths
    11    #[arg(short = 'I', value_name = "DIR", value_hint = clap::ValueHint::DirPath)]
    12    include: Option<std::path::PathBuf>,
    13
    14    /// Handle IP addresses
    15    #[arg(long)]
    16    bind: Option<std::net::IpAddr>,
    17
    18    /// Allow human-readable durations
    19    #[arg(long)]
    20    sleep: Option<jiff::SignedDuration>,
    21
    22    /// Custom enums
    23    #[arg(long)]
    24    bump_level: Option<BumpLevel>,
    25}
    26
    27#[derive(Debug, Clone, Copy, ValueEnum)]
    28#[value(rename_all = "kebab-case")]
    29pub(crate) enum BumpLevel {
    30    /// Increase the major version (x.0.0)
    31    Major,
    32    /// Increase the minor version (x.y.0)
    33    Minor,
    34    /// Increase the patch version (x.y.z)
    35    Patch,
    36}
    37
    38impl std::fmt::Display for BumpLevel {
    39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    40        use clap::ValueEnum;
    41
    42        self.to_possible_value()
    43            .expect("no values are skipped")
    44            .get_name()
    45            .fmt(f)
    46    }
    47}
    48
    49impl std::str::FromStr for BumpLevel {
    50    type Err = String;
    51
    52    fn from_str(s: &str) -> Result<Self, Self::Err> {
    53        use clap::ValueEnum;
    54
    55        for variant in Self::value_variants() {
    56            if variant.to_possible_value().unwrap().matches(s, false) {
    57                return Ok(*variant);
    58            }
    59        }
    60        Err(format!("Invalid variant: {s}"))
    61    }
    62}