Skip to content

Commit

Permalink
refactor(shell): Allow reusing ColorChoice parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 20, 2024
1 parent cec39f1 commit f3627f0
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,10 @@ impl Shell {
..
} = self.output
{
let cfg = match color {
Some("always") => ColorChoice::Always,
Some("never") => ColorChoice::Never,

Some("auto") | None => ColorChoice::CargoAuto,

Some(arg) => anyhow::bail!(
"argument for --color must be auto, always, or \
never, but found `{}`",
arg
),
};
let cfg = color
.map(|c| c.parse())
.transpose()?
.unwrap_or(ColorChoice::CargoAuto);
*color_choice = cfg;
let stdout_choice = cfg.to_anstream_color_choice();
let stderr_choice = cfg.to_anstream_color_choice();
Expand Down Expand Up @@ -499,6 +491,25 @@ impl ColorChoice {
}
}

impl std::str::FromStr for ColorChoice {
type Err = anyhow::Error;
fn from_str(color: &str) -> Result<Self, Self::Err> {
let cfg = match color {
"always" => ColorChoice::Always,
"never" => ColorChoice::Never,

"auto" => ColorChoice::CargoAuto,

arg => anyhow::bail!(
"argument for --color must be auto, always, or \
never, but found `{}`",
arg
),
};
Ok(cfg)
}
}

fn supports_color(choice: anstream::ColorChoice) -> bool {
match choice {
anstream::ColorChoice::Always
Expand Down

0 comments on commit f3627f0

Please sign in to comment.