diff --git a/src/color.rs b/src/color.rs index f477d4e..ebd4a04 100644 --- a/src/color.rs +++ b/src/color.rs @@ -38,6 +38,7 @@ impl Color { } } + #[cfg(test)] pub(crate) fn always() -> Self { Self { use_color: UseColor::Always, @@ -133,6 +134,15 @@ impl Color { } } +impl From for Color { + fn from(value: UseColor) -> Self { + Self { + use_color: value, + ..Default::default() + } + } +} + impl Default for Color { fn default() -> Self { Self { diff --git a/src/config.rs b/src/config.rs index 4e843dc..6664657 100644 --- a/src/config.rs +++ b/src/config.rs @@ -109,10 +109,7 @@ mod arg { pub(crate) const WORKING_DIRECTORY: &str = "WORKING-DIRECTORY"; pub(crate) const YES: &str = "YES"; - pub(crate) const COLOR_ALWAYS: &str = "always"; pub(crate) const COLOR_AUTO: &str = "auto"; - pub(crate) const COLOR_NEVER: &str = "never"; - pub(crate) const COLOR_VALUES: &[&str] = &[COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER]; pub(crate) const COMMAND_COLOR_BLACK: &str = "black"; pub(crate) const COMMAND_COLOR_BLUE: &str = "blue"; @@ -182,7 +179,7 @@ impl Config { .long("color") .env("JUST_COLOR") .action(ArgAction::Set) - .value_parser(PossibleValuesParser::new(arg::COLOR_VALUES)) + .value_parser(clap::value_parser!(UseColor)) .default_value(arg::COLOR_AUTO) .help("Print colorful output"), ) @@ -529,23 +526,6 @@ impl Config { ) } - fn color_from_matches(matches: &ArgMatches) -> ConfigResult { - let value = matches - .get_one::(arg::COLOR) - .ok_or_else(|| ConfigError::Internal { - message: "`--color` had no value".to_string(), - })?; - - match value.as_str() { - arg::COLOR_AUTO => Ok(Color::auto()), - arg::COLOR_ALWAYS => Ok(Color::always()), - arg::COLOR_NEVER => Ok(Color::never()), - _ => Err(ConfigError::Internal { - message: format!("Invalid argument `{value}` to --color."), - }), - } - } - fn command_color_from_matches(matches: &ArgMatches) -> ConfigResult> { if let Some(value) = matches.get_one::(arg::COMMAND_COLOR) { match value.as_str() { @@ -727,7 +707,7 @@ impl Config { Ok(Self { check: matches.get_flag(arg::CHECK), - color: Self::color_from_matches(matches)?, + color: (*matches.get_one::(arg::COLOR).unwrap()).into(), command_color: Self::command_color_from_matches(matches)?, dotenv_filename: matches .get_one::(arg::DOTENV_FILENAME) diff --git a/src/use_color.rs b/src/use_color.rs index 131ec6d..6c8d93d 100644 --- a/src/use_color.rs +++ b/src/use_color.rs @@ -1,4 +1,4 @@ -#[derive(Copy, Clone, Debug, PartialEq)] +#[derive(Copy, Clone, Debug, PartialEq, clap::ValueEnum)] pub(crate) enum UseColor { Auto, Always,