From a4c900c9a7c0f17375dd5034734d617fa37cf9bc Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Wed, 22 Feb 2023 12:29:02 +0100 Subject: [PATCH] add error NoGroupFilesInArguments --- TODO.md | 3 --- crates/main/main.rs | 8 +++++++- crates/pacdef_core/src/args.rs | 3 ++- crates/pacdef_core/src/core.rs | 5 +++-- crates/pacdef_core/src/errors.rs | 3 +++ 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/TODO.md b/TODO.md index f17891c..9aeadfa 100644 --- a/TODO.md +++ b/TODO.md @@ -1,9 +1,6 @@ # To Do - make transition for existing user easier -- create an outer error type for main - change version names (1.0.0-beta.10) - tutorial - update completion for new subcommands (WIP) -- outer error types: - - no group files provided (pacdef group new --edit) diff --git a/crates/main/main.rs b/crates/main/main.rs index 5b4f357..1afc302 100644 --- a/crates/main/main.rs +++ b/crates/main/main.rs @@ -27,11 +27,17 @@ fn main() -> ExitCode { /// Skip printing the error chain when searching packages yields no results, otherwise report error /// chain. +#[allow(clippy::option_if_let_else)] fn handle_final_result(result: Result<()>) -> ExitCode { match result { Ok(_) => ExitCode::SUCCESS, Err(ref e) => { - if e.root_cause().to_string() == pacdef_core::Error::NoPackagesFound.to_string() { + if let Some(pacdef_err) = e.root_cause().downcast_ref::() { + match pacdef_err { + pacdef_core::Error::NoPackagesFound => (), + pacdef_core::Error::NoGroupFilesInArguments => eprintln!("{pacdef_err}"), + _ => eprintln!("unexpected error"), + }; ExitCode::FAILURE } else { result.report() diff --git a/crates/pacdef_core/src/args.rs b/crates/pacdef_core/src/args.rs index f49059c..3843b1f 100644 --- a/crates/pacdef_core/src/args.rs +++ b/crates/pacdef_core/src/args.rs @@ -45,7 +45,8 @@ fn get_group_cmd() -> Command { .short('e') .long("edit") .help("edit the new group files after creation") - .action(clap::ArgAction::SetTrue), + .action(clap::ArgAction::SetTrue) + .num_args(0), ) .arg(Arg::new("groups").num_args(1..)) .visible_alias("n"); diff --git a/crates/pacdef_core/src/core.rs b/crates/pacdef_core/src/core.rs index 4562402..f61d341 100644 --- a/crates/pacdef_core/src/core.rs +++ b/crates/pacdef_core/src/core.rs @@ -315,8 +315,9 @@ fn get_group_file_paths_matching_args<'a>( groups: &'a HashSet, ) -> Result> { let file_names: Vec<_> = arg_match - .get_many::("group") - .context("getting groups from args")? + .get_many::("groups") + .context("getting groups from args") + .map_err(|_| crate::errors::Error::NoGroupFilesInArguments)? .collect(); let name_group_map: HashMap<&str, &Group> = diff --git a/crates/pacdef_core/src/errors.rs b/crates/pacdef_core/src/errors.rs index e597007..0f19be0 100644 --- a/crates/pacdef_core/src/errors.rs +++ b/crates/pacdef_core/src/errors.rs @@ -7,12 +7,15 @@ use std::fmt::Display; pub enum Error { /// Package search yields no results. NoPackagesFound, + /// No group files were provided as arguments. + NoGroupFilesInArguments, } impl Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::NoPackagesFound => f.write_str("no packages matching query"), + Self::NoGroupFilesInArguments => f.write_str("no group files in arguments"), } } }