From b80fb1e879a78de0abebe32e6bd2ab5727972d79 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Wed, 26 Jun 2024 20:29:10 -0700 Subject: [PATCH] Use clap ValueParser for dump format --- src/config.rs | 26 +++++--------------------- src/dump_format.rs | 2 +- 2 files changed, 6 insertions(+), 22 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0e8f3d4..4e843dc 100644 --- a/src/config.rs +++ b/src/config.rs @@ -131,9 +131,7 @@ mod arg { COMMAND_COLOR_YELLOW, ]; - pub(crate) const DUMP_FORMAT_JSON: &str = "json"; pub(crate) const DUMP_FORMAT_JUST: &str = "just"; - pub(crate) const DUMP_FORMAT_VALUES: &[&str] = &[DUMP_FORMAT_JUST, DUMP_FORMAT_JSON]; } impl Config { @@ -225,7 +223,7 @@ impl Config { .long("dump-format") .env("JUST_DUMP_FORMAT") .action(ArgAction::Set) - .value_parser(PossibleValuesParser::new(arg::DUMP_FORMAT_VALUES)) + .value_parser(clap::value_parser!(DumpFormat)) .default_value(arg::DUMP_FORMAT_JUST) .value_name("FORMAT") .help("Dump justfile as "), @@ -567,23 +565,6 @@ impl Config { } } - fn dump_format_from_matches(matches: &ArgMatches) -> ConfigResult { - let value = - matches - .get_one::(arg::DUMP_FORMAT) - .ok_or_else(|| ConfigError::Internal { - message: "`--dump-format` had no value".to_string(), - })?; - - match value.as_str() { - arg::DUMP_FORMAT_JSON => Ok(DumpFormat::Json), - arg::DUMP_FORMAT_JUST => Ok(DumpFormat::Just), - _ => Err(ConfigError::Internal { - message: format!("Invalid argument `{value}` to --dump-format."), - }), - } - } - fn parse_module_path(values: ValuesRef) -> ConfigResult { let path = values.clone().map(|s| (*s).as_str()).collect::>(); @@ -753,7 +734,10 @@ impl Config { .map(Into::into), dotenv_path: matches.get_one::(arg::DOTENV_PATH).map(Into::into), dry_run: matches.get_flag(arg::DRY_RUN), - dump_format: Self::dump_format_from_matches(matches)?, + dump_format: matches + .get_one::(arg::DUMP_FORMAT) + .unwrap() + .clone(), highlight: !matches.get_flag(arg::NO_HIGHLIGHT), invocation_directory: env::current_dir().context(config_error::CurrentDirContext)?, list_heading: matches diff --git a/src/dump_format.rs b/src/dump_format.rs index e560ae0..0a912de 100644 --- a/src/dump_format.rs +++ b/src/dump_format.rs @@ -1,4 +1,4 @@ -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone, clap::ValueEnum)] pub(crate) enum DumpFormat { Json, Just,