Skip to content

Commit

Permalink
feat: Add Display/FromStr to ColorChoice
Browse files Browse the repository at this point in the history
This matches up with `clap_complete::Shell`.  This makes it a bit more flexible.
  • Loading branch information
epage committed Nov 24, 2022
1 parent ed683ef commit 8d92f3e
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/util/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ impl Default for ColorChoice {
}
}

impl std::fmt::Display for ColorChoice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.to_possible_value()
.expect("no values are skipped")
.get_name()
.fmt(f)
}
}

impl std::str::FromStr for ColorChoice {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
for variant in Self::value_variants() {
if variant.to_possible_value().unwrap().matches(s, false) {
return Ok(*variant);
}
}
Err(format!("Invalid variant: {}", s))
}
}

impl ValueEnum for ColorChoice {
fn value_variants<'a>() -> &'a [Self] {
&[Self::Auto, Self::Always, Self::Never]
Expand Down

0 comments on commit 8d92f3e

Please sign in to comment.