From 5f4a0caacf3121393110d9839e2caea7e4de74d7 Mon Sep 17 00:00:00 2001 From: steven-omaha <35634100+steven-omaha@users.noreply.github.com> Date: Sat, 18 Feb 2023 18:38:53 +0100 Subject: [PATCH] update action match function --- TODO.md | 1 + crates/pacdef_core/src/action.rs | 2 +- crates/pacdef_core/src/args.rs | 14 ++++----- crates/pacdef_core/src/core.rs | 50 ++++++++++++++++++++------------ 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/TODO.md b/TODO.md index 5c8a7a9..23de761 100644 --- a/TODO.md +++ b/TODO.md @@ -6,3 +6,4 @@ - make table use section header with brackets - tutorial - remove "Arch" references +- update README for subcommands "group", "package" diff --git a/crates/pacdef_core/src/action.rs b/crates/pacdef_core/src/action.rs index 03ab9e5..92805d3 100644 --- a/crates/pacdef_core/src/action.rs +++ b/crates/pacdef_core/src/action.rs @@ -4,8 +4,8 @@ use pacdef_macros::Action; pub enum Actions { Clean, Edit, - Groups, Import, + List, New, Remove, Review, diff --git a/crates/pacdef_core/src/args.rs b/crates/pacdef_core/src/args.rs index 399bbb9..41e902c 100644 --- a/crates/pacdef_core/src/args.rs +++ b/crates/pacdef_core/src/args.rs @@ -21,23 +21,18 @@ fn get_arg_parser() -> Command { } fn get_group_cmd() -> Command { - let remove = Command::new(REMOVE) - .about("remove one or more previously imported groups") - .arg_required_else_help(true) - .arg(Arg::new("groups").num_args(1..)); - let edit = Command::new(EDIT) .about("edit one or more existing group files") .arg_required_else_help(true) .arg(Arg::new("group").num_args(1..)); - let list = Command::new(GROUPS).about("list names of imported groups"); - let import = Command::new(IMPORT) .about("import one or more group files") .arg_required_else_help(true) .arg(Arg::new("files").num_args(1..)); + let list = Command::new(LIST).about("list names of imported groups"); + let new = Command::new(NEW) .about("create new group files") .arg_required_else_help(true) @@ -50,6 +45,11 @@ fn get_group_cmd() -> Command { ) .arg(Arg::new("groups").num_args(1..)); + let remove = Command::new(REMOVE) + .about("remove one or more previously imported groups") + .arg_required_else_help(true) + .arg(Arg::new("groups").num_args(1..)); + let show = Command::new(SHOW) .about("show packages under an imported group") .arg_required_else_help(true) diff --git a/crates/pacdef_core/src/core.rs b/crates/pacdef_core/src/core.rs index 6a53335..4562402 100644 --- a/crates/pacdef_core/src/core.rs +++ b/crates/pacdef_core/src/core.rs @@ -19,6 +19,9 @@ use crate::ui::get_user_confirmation; use crate::Config; use crate::Group; +const UNREACHABLE_ARM: &str = "argument parser requires some subcommand to return an `ArgMatches`"; +const ACTION_NOT_MATCHED: &str = "could not match action"; + /// Most data that is required during runtime of the program. pub struct Pacdef { args: ArgMatches, @@ -53,25 +56,36 @@ impl Pacdef { #[allow(clippy::unit_arg)] pub fn run_action_from_arg(mut self) -> Result<()> { match self.args.subcommand() { - Some((CLEAN, _)) => self.clean_packages(), - Some((EDIT, args)) => self.edit_group_files(args).context("editing group files"), - Some((GROUPS, _)) => Ok(self.show_groups()), - Some((IMPORT, args)) => self.import_groups(args).context("importing groups"), - Some((NEW, args)) => self.new_groups(args).context("creating new group files"), - Some((REMOVE, args)) => self.remove_groups(args).context("removing groups"), - Some((REVIEW, _)) => review::review(self.get_unmanaged_packages(), self.groups) - .context("review unmanaged packages"), - Some((SHOW, args)) => self.show_group_content(args).context("showing groups"), - Some((SEARCH, args)) => { - search::search_packages(args, &self.groups).context("searching packages") - } - Some((SYNC, _)) => self.install_packages(), - Some((UNMANAGED, _)) => self.show_unmanaged_packages(), + Some(("group", args)) => match args.subcommand() { + Some((EDIT, args)) => self.edit_group_files(args).context("editing group files"), + Some((IMPORT, args)) => self.import_groups(args).context("importing groups"), + Some((LIST, _)) => Ok(self.show_groups()), + Some((NEW, args)) => self.new_groups(args).context("creating new group files"), + Some((REMOVE, args)) => self.remove_groups(args).context("removing groups"), + Some((SHOW, args)) => self.show_group_content(args).context("showing groups"), + + Some((_, _)) => panic!("{ACTION_NOT_MATCHED}"), + None => unreachable!("{UNREACHABLE_ARM}"), + }, + + Some(("package", args)) => match args.subcommand() { + Some((CLEAN, _)) => self.clean_packages(), + Some((REVIEW, _)) => review::review(self.get_unmanaged_packages(), self.groups) + .context("review unmanaged packages"), + Some((SEARCH, args)) => { + search::search_packages(args, &self.groups).context("searching packages") + } + Some((SYNC, _)) => self.install_packages(), + Some((UNMANAGED, _)) => self.show_unmanaged_packages(), + + Some((_, _)) => panic!("{ACTION_NOT_MATCHED}"), + None => unreachable!("{UNREACHABLE_ARM}"), + }, + Some((VERSION, _)) => Ok(self.show_version()), - Some((_, _)) => panic!(), - None => { - unreachable!("argument parser requires some subcommand to return an `ArgMatches`") - } + + Some((_, _)) => panic!("{ACTION_NOT_MATCHED}"), + None => unreachable!("{UNREACHABLE_ARM}"), } }