Skip to content

Commit

Permalink
Prevent typos and the need for tests
Browse files Browse the repository at this point in the history
delegate &str creation to the to_possible_value()
method provided by clap::ArgEnum. Since both arguments
are non-skipped, we can safely use expect()
  • Loading branch information
petr-tik committed Feb 17, 2022
1 parent 672dcde commit 5b1a975
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
30 changes: 15 additions & 15 deletions src/cargo/ops/cargo_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,6 @@ impl ConfigFormat {
}
}

impl AsRef<str> for ConfigFormat {
fn as_ref(&self) -> &str {
match self {
ConfigFormat::Toml => "toml",
ConfigFormat::Json => "json",
ConfigFormat::JsonValue => "json-value",
}
}
}

impl FromStr for ConfigFormat {
type Err = Error;
fn from_str(s: &str) -> CargoResult<Self> {
Expand All @@ -49,11 +39,21 @@ impl FromStr for ConfigFormat {

impl fmt::Display for ConfigFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ConfigFormat::Toml => write!(f, "toml"),
ConfigFormat::Json => write!(f, "json"),
ConfigFormat::JsonValue => write!(f, "json-value"),
}
write!(
f,
"{}",
self.to_possible_value()
.expect("No value is skipped")
.get_name()
)
}
}

impl AsRef<str> for ConfigFormat {
fn as_ref(&self) -> &str {
self.to_possible_value()
.expect("No value is skipped")
.get_name()
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/cargo/ops/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ impl FromStr for Prefix {

impl AsRef<str> for Prefix {
fn as_ref(&self) -> &str {
match self {
Prefix::None => "none",
Prefix::Indent => "indent",
Prefix::Depth => "depth",
}
self.to_possible_value()
.expect("No value is skipped")
.get_name()
}
}

Expand Down

0 comments on commit 5b1a975

Please sign in to comment.