refactor: Change the way of clap commands
This commit is contained in:
parent
c8139a1bbc
commit
9e20e17b3b
2 changed files with 36 additions and 35 deletions
|
@ -18,7 +18,7 @@ use std::path::PathBuf;
|
||||||
|
|
||||||
use clap::Parser;
|
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 add_command;
|
||||||
pub mod clean_command;
|
pub mod clean_command;
|
||||||
|
@ -29,17 +29,28 @@ pub mod import_command;
|
||||||
pub mod list_command;
|
pub mod list_command;
|
||||||
pub mod remove_command;
|
pub mod remove_command;
|
||||||
|
|
||||||
crate::create_commands!(
|
/// The lprs commands
|
||||||
enum Commands
|
#[derive(Debug, clap::Subcommand)]
|
||||||
"Add new vault", Add => add_command::Add
|
pub enum Commands {
|
||||||
"Remove vault", Remove => remove_command::Remove
|
/// Add new vault
|
||||||
"List your vaults and search", List => list_command::List
|
Add(add_command::Add),
|
||||||
"Clean the vaults file", Clean => clean_command::Clean
|
/// Remove vault
|
||||||
"Edit the vault content", Edit => edit_command::Edit
|
Remove(remove_command::Remove),
|
||||||
"Generate a password", Gen => gen_command::Gen
|
/// List your vaults and search
|
||||||
"Export the vaults", Export => export_command::Export
|
List(list_command::List),
|
||||||
"Import vaults", Import => import_command::Import
|
/// 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)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.
|
||||||
|
|
||||||
/// Creates commands macro, to create the `Commands` enum and impl `LprsCommand` to it.
|
/// Impl `LprsCommand` to the given subcommand.
|
||||||
///
|
///
|
||||||
/// ### Notes:
|
/// ### Notes:
|
||||||
/// The `$command` must impl `LprsCommand` trait
|
/// The `$command` must impl `LprsCommand` trait
|
||||||
|
@ -22,21 +22,12 @@
|
||||||
/// ### Example:
|
/// ### Example:
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// create_commands!(
|
/// create_commands!(
|
||||||
/// enum TestCommands
|
/// TestCommands,
|
||||||
/// "Test command", Test => TestArgs
|
/// Test Some
|
||||||
/// "Do something", Some => SomeArgs
|
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
/// #### Output
|
/// #### Output
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// ///The lprs commands
|
|
||||||
/// pub enum TestCommands {
|
|
||||||
/// ///Test command
|
|
||||||
/// Test(TestArgs),
|
|
||||||
/// ///Do something
|
|
||||||
/// Some(SomeArgs),
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// impl crate::LprsCommand for TestCommands {
|
/// impl crate::LprsCommand for TestCommands {
|
||||||
/// fn run(
|
/// fn run(
|
||||||
/// &self,
|
/// &self,
|
||||||
|
@ -47,20 +38,19 @@
|
||||||
/// Self::Some(command) => command.run(vault_manager),
|
/// 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_export]
|
||||||
macro_rules! create_commands {
|
macro_rules! impl_commands {
|
||||||
(enum $enum_name: ident $($doc:tt, $varint: ident => $command: ty)+) => {
|
($enum_name: ident, $($varint: ident)+) => {
|
||||||
#[doc = "The lprs commands"]
|
|
||||||
#[derive(Debug, clap::Subcommand)]
|
|
||||||
pub enum $enum_name {
|
|
||||||
$(
|
|
||||||
#[doc = $doc]
|
|
||||||
$varint($command),
|
|
||||||
)+
|
|
||||||
}
|
|
||||||
|
|
||||||
#[automatically_derived]
|
#[automatically_derived]
|
||||||
impl $crate::LprsCommand for $enum_name{
|
impl $crate::LprsCommand for $enum_name{
|
||||||
fn run(self, vault_manager: $crate::vault::Vaults<$crate::vault::vault_state::Plain>) -> $crate::LprsResult<()> {
|
fn run(self, vault_manager: $crate::vault::Vaults<$crate::vault::vault_state::Plain>) -> $crate::LprsResult<()> {
|
||||||
|
|
Loading…
Reference in a new issue