From 791d390e636c1c29af23b343edb66279b791b121 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Mon, 29 Apr 2024 23:50:45 +0200 Subject: [PATCH] feat: Support `rm` and `ls` aliases (#22) Reviewed-on: https://git.4rs.nl/awiteb/lprs/pulls/22 Fixes: https://git.4rs.nl/awiteb/lprs/issues/18 Co-authored-by: Awiteb Co-committed-by: Awiteb --- src/cli/mod.rs | 37 +++++++++++++++++++++++++------------ src/macros.rs | 36 +++++++++++++----------------------- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index a5c2424..571ee34 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -18,7 +18,7 @@ use std::path::PathBuf; use clap::Parser; -use crate::{utils, vault::Vaults, LprsCommand, LprsResult}; +use crate::{impl_commands, utils, vault::Vaults, LprsCommand, LprsResult}; pub mod add_command; pub mod clean_command; @@ -29,17 +29,30 @@ pub mod import_command; pub mod list_command; pub mod remove_command; -crate::create_commands!( - enum Commands - "Add new vault", Add => add_command::Add - "Remove vault", Remove => remove_command::Remove - "List your vaults and search", List => list_command::List - "Clean the vaults file", Clean => clean_command::Clean - "Edit the vault content", Edit => edit_command::Edit - "Generate a password", Gen => gen_command::Gen - "Export the vaults", Export => export_command::Export - "Import vaults", Import => import_command::Import -); +/// The lprs commands +#[derive(Debug, clap::Subcommand)] +pub enum Commands { + /// Add new vault + Add(add_command::Add), + /// Remove vault [alias `rm`] + #[command(alias = "rm")] + Remove(remove_command::Remove), + /// List your vaults and search [alias `ls`] + #[command(alias = "ls")] + List(list_command::List), + /// Clean the vaults file + Clean(clean_command::Clean), + /// Edit the vault content + Edit(edit_command::Edit), + /// Generate a password + Gen(gen_command::Gen), + /// Export the vaults + Export(export_command::Export), + /// Import vaults + Import(import_command::Import), +} + +impl_commands!(Commands, Add Remove List Clean Edit Gen Export Import); #[derive(Parser, Debug)] #[command(author, version, about, long_about = None)] diff --git a/src/macros.rs b/src/macros.rs index 69f2777..1b19e23 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -/// Creates commands macro, to create the `Commands` enum and impl `LprsCommand` to it. +/// Impl `LprsCommand` to the given subcommand. /// /// ### Notes: /// The `$command` must impl `LprsCommand` trait @@ -22,21 +22,12 @@ /// ### Example: /// ```rust /// create_commands!( -/// enum TestCommands -/// "Test command", Test => TestArgs -/// "Do something", Some => SomeArgs +/// TestCommands, +/// Test Some /// ); /// ``` /// #### Output /// ```rust -/// ///The lprs commands -/// pub enum TestCommands { -/// ///Test command -/// Test(TestArgs), -/// ///Do something -/// Some(SomeArgs), -/// } -/// /// impl crate::LprsCommand for TestCommands { /// fn run( /// &self, @@ -47,20 +38,19 @@ /// Self::Some(command) => command.run(vault_manager), /// } /// } +/// +/// fn validate_args(&self) -> crate::LprsResult<()> { +/// match self { +/// Self::Test(command) => command.validate_args(), +/// Self::Some(command) => command.validate_args(), +/// } +/// } /// } + /// ``` #[macro_export] -macro_rules! create_commands { - (enum $enum_name: ident $($doc:tt, $varint: ident => $command: ty)+) => { - #[doc = "The lprs commands"] - #[derive(Debug, clap::Subcommand)] - pub enum $enum_name { - $( - #[doc = $doc] - $varint($command), - )+ - } - +macro_rules! impl_commands { + ($enum_name: ident, $($varint: ident)+) => { #[automatically_derived] impl $crate::LprsCommand for $enum_name{ fn run(self, vault_manager: $crate::vault::Vaults<$crate::vault::vault_state::Plain>) -> $crate::LprsResult<()> {