From 9e20e17b3bb21812d14ddd2688e9af1ac550a206 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Tue, 30 Apr 2024 00:40:48 +0300 Subject: [PATCH 1/2] refactor: Change the way of clap commands --- src/cli/mod.rs | 35 +++++++++++++++++++++++------------ src/macros.rs | 36 +++++++++++++----------------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index a5c2424..09c9270 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,28 @@ 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 + 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), +} + +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<()> { -- 2.45.2 From d4768934acd7d72a31fcf8b6d526e22db31a2801 Mon Sep 17 00:00:00 2001 From: Awiteb Date: Tue, 30 Apr 2024 00:41:14 +0300 Subject: [PATCH 2/2] feat: Support `rm` and `ls` aliases Fixes: https://git.4rs.nl/awiteb/lprs/issues/18 --- src/cli/mod.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 09c9270..571ee34 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -34,9 +34,11 @@ pub mod remove_command; pub enum Commands { /// Add new vault Add(add_command::Add), - /// Remove vault + /// Remove vault [alias `rm`] + #[command(alias = "rm")] Remove(remove_command::Remove), - /// List your vaults and search + /// List your vaults and search [alias `ls`] + #[command(alias = "ls")] List(list_command::List), /// Clean the vaults file Clean(clean_command::Clean), -- 2.45.2