Skip to content

Commit ecdf8ec

Browse files
committed
refactor(shell): Allow reusing ColorChoice parsing
1 parent 637770f commit ecdf8ec

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/cargo/core/shell.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,10 @@ impl Shell {
236236
..
237237
} = self.output
238238
{
239-
let cfg = match color {
240-
Some("always") => ColorChoice::Always,
241-
Some("never") => ColorChoice::Never,
242-
243-
Some("auto") | None => ColorChoice::CargoAuto,
244-
245-
Some(arg) => anyhow::bail!(
246-
"argument for --color must be auto, always, or \
247-
never, but found `{}`",
248-
arg
249-
),
250-
};
239+
let cfg = color
240+
.map(|c| c.parse())
241+
.transpose()?
242+
.unwrap_or(ColorChoice::CargoAuto);
251243
*color_choice = cfg;
252244
let stdout_choice = cfg.to_anstream_color_choice();
253245
let stderr_choice = cfg.to_anstream_color_choice();
@@ -499,6 +491,25 @@ impl ColorChoice {
499491
}
500492
}
501493

494+
impl std::str::FromStr for ColorChoice {
495+
type Err = anyhow::Error;
496+
fn from_str(color: &str) -> Result<Self, Self::Err> {
497+
let cfg = match color {
498+
"always" => ColorChoice::Always,
499+
"never" => ColorChoice::Never,
500+
501+
"auto" => ColorChoice::CargoAuto,
502+
503+
arg => anyhow::bail!(
504+
"argument for --color must be auto, always, or \
505+
never, but found `{}`",
506+
arg
507+
),
508+
};
509+
Ok(cfg)
510+
}
511+
}
512+
502513
fn supports_color(choice: anstream::ColorChoice) -> bool {
503514
match choice {
504515
anstream::ColorChoice::Always

0 commit comments

Comments
 (0)