add actions derive macro

This commit is contained in:
timeshifter
2023-01-13 15:25:46 +01:00
parent 7792b98cb4
commit ce960a4e29
5 changed files with 229 additions and 138 deletions
+16 -11
View File
@@ -1,11 +1,16 @@
pub(crate) const CLEAN: &str = "clean";
pub(crate) const EDIT: &str = "edit";
pub(crate) const GROUPS: &str = "groups";
pub(crate) const IMPORT: &str = "import";
pub(crate) const NEW: &str = "new";
pub(crate) const REMOVE: &str = "remove";
pub(crate) const SEARCH: &str = "search";
pub(crate) const SHOW: &str = "show";
pub(crate) const SYNC: &str = "sync";
pub(crate) const UNMANAGED: &str = "unmanaged";
pub(crate) const VERSION: &str = "version";
use pacdef_macro::Action;
#[derive(Debug, Action)]
pub(crate) enum Actions {
Clean,
Edit,
Groups,
Import,
New,
Remove,
Search,
Show,
Sync,
Unmanaged,
Version,
}
+13 -19
View File
@@ -6,7 +6,7 @@ use std::path::PathBuf;
use anyhow::{ensure, Context, Result};
use clap::ArgMatches;
use crate::action;
use crate::action::*;
use crate::args;
use crate::backend::{Backend, Backends, ToDoPerBackend};
use crate::cmd::run_edit_command;
@@ -21,6 +21,7 @@ pub struct Pacdef {
groups: HashSet<Group>,
}
// TODO review
impl Pacdef {
#[must_use]
pub fn new(args: ArgMatches, groups: HashSet<Group>) -> Self {
@@ -29,27 +30,20 @@ impl Pacdef {
#[allow(clippy::unit_arg)]
pub fn run_action_from_arg(self) -> Result<()> {
// TODO review
match self.args.subcommand() {
Some((action::CLEAN, _)) => Ok(self.clean_packages()),
Some((action::EDIT, groups)) => {
self.edit_group_files(groups).context("editing group files")
}
Some((action::GROUPS, _)) => Ok(self.show_groups()),
Some((action::IMPORT, files)) => self.import_groups(files).context("importing groups"),
Some((action::NEW, files)) => {
self.new_groups(files).context("creating new group files")
}
Some((action::REMOVE, groups)) => self.remove_groups(groups).context("removing groups"),
Some((action::SHOW, groups)) => {
self.show_group_content(groups).context("showing groups")
}
Some((action::SEARCH, args)) => {
Some((CLEAN, _)) => Ok(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((SHOW, args)) => self.show_group_content(args).context("showing groups"),
Some((SEARCH, args)) => {
search::search_packages(args, &self.groups).context("searching packages")
}
Some((action::SYNC, _)) => Ok(self.install_packages()),
Some((action::UNMANAGED, _)) => Ok(self.show_unmanaged_packages()),
Some((action::VERSION, _)) => Ok(self.show_version()),
Some((SYNC, _)) => Ok(self.install_packages()),
Some((UNMANAGED, _)) => Ok(self.show_unmanaged_packages()),
Some((VERSION, _)) => Ok(self.show_version()),
Some((_, _)) => todo!(),
None => {
unreachable!("argument parser requires some subcommand to return an `ArgMatches`")