Skip to content

Commit

Permalink
refactor: use enum_dispatch to reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
skymacro111666 committed Jun 20, 2024
1 parent d594de5 commit 1ee67c9
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 65 deletions.
11 changes: 2 additions & 9 deletions src/cli/base64.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::{fmt, str::FromStr};

use clap::Parser;
use enum_dispatch::enum_dispatch;

use crate::CmdExector;

use super::verify_file;

#[derive(Debug, Parser)]
#[enum_dispatch(CmdExector)]
pub enum Base64SubCommand {
#[command(name = "encode", about = "Encode a string to base64")]
Encode(Base64EncodeOpts),
Expand Down Expand Up @@ -83,12 +85,3 @@ impl CmdExector for Base64DecodeOpts {
Ok(())
}
}

impl CmdExector for Base64SubCommand {
async fn execute(self) -> anyhow::Result<()> {
match self {
Base64SubCommand::Encode(opts) => opts.execute().await,
Base64SubCommand::Decode(opts) => opts.execute().await,
}
}
}
13 changes: 0 additions & 13 deletions src/cli/csv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,3 @@ impl fmt::Display for OutputFormat {
write!(f, "{}", Into::<&'static str>::into(*self))
}
}

// impl TryFrom<&str> for OutputFormat {
// type Error = anyhow::Error;

// fn try_from(format: &str) -> Result<Self, Self::Error> {
// match format.to_lowercase().as_str() {
// "json" => Ok(OutputFormat::Json),
// "yaml" => Ok(OutputFormat::Yaml),
// "toml" => Ok(OutputFormat::Toml),
// v => anyhow::bail!("Unsupported format {}", v),
// }
// }
// }
10 changes: 2 additions & 8 deletions src/cli/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use crate::{process_http_serve, CmdExector};

use super::verify_path;
use clap::Parser;
use enum_dispatch::enum_dispatch;
use std::path::PathBuf;

#[derive(Debug, Parser)]
#[enum_dispatch(CmdExector)]
pub enum HttpSubCommand {
#[command(about = "Serve a directory over HTTP")]
Serve(HttpServeOpts),
Expand All @@ -23,11 +25,3 @@ impl CmdExector for HttpServeOpts {
process_http_serve(self.dir, self.port).await
}
}

impl CmdExector for HttpSubCommand {
async fn execute(self) -> anyhow::Result<()> {
match self {
HttpSubCommand::Serve(opts) => opts.execute().await,
}
}
}
25 changes: 4 additions & 21 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@ mod csv;
mod genpass;
mod http;
mod text;
use crate::CmdExector;
use clap::Parser;
use enum_dispatch::enum_dispatch;
use std::path::{Path, PathBuf};

use self::csv::CsvOpts;
pub use self::{
base64::{Base64Format, Base64SubCommand},
csv::OutputFormat,
genpass::GenPassOpts,
http::HttpSubCommand,
text::{TextSignFormat, TextSubCommand},
};
// use self::csv::CsvOpts;
pub use self::{base64::*, csv::*, genpass::*, http::*, text::*};

#[derive(Debug, Parser)]
#[command(name = "rcli", version, author, about, long_about = None)]
Expand All @@ -24,6 +18,7 @@ pub struct Opts {
}

#[derive(Debug, Parser)]
#[enum_dispatch(CmdExector)]
pub enum Subcommand {
#[command(name = "csv", about = "Show CSV, or convert CSV to other formats")]
Csv(CsvOpts),
Expand All @@ -37,18 +32,6 @@ pub enum Subcommand {
Http(HttpSubCommand),
}

impl CmdExector for Subcommand {
async fn execute(self) -> anyhow::Result<()> {
match self {
Subcommand::Csv(opts) => opts.execute().await,
Subcommand::GenPass(opts) => opts.execute().await,
Subcommand::Base64(cmd) => cmd.execute().await,
Subcommand::Text(cmd) => cmd.execute().await,
Subcommand::Http(cmd) => cmd.execute().await,
}
}
}

fn verify_file(filename: &str) -> Result<String, String> {
if filename == "-" || Path::new(filename).exists() {
Ok(filename.into())
Expand Down
12 changes: 2 additions & 10 deletions src/cli/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use crate::{
};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use clap::Parser;
use enum_dispatch::enum_dispatch;
use std::{fmt, path::PathBuf, str::FromStr};
use tokio::fs;

#[derive(Debug, Parser)]
#[enum_dispatch(CmdExector)]
pub enum TextSubCommand {
#[command(name = "sign", about = "Sign a message with a private/shared key")]
Sign(TextSignOpts),
Expand Down Expand Up @@ -121,13 +123,3 @@ impl CmdExector for KeyGenerateOpts {
Ok(())
}
}

impl CmdExector for TextSubCommand {
async fn execute(self) -> anyhow::Result<()> {
match self {
TextSubCommand::Sign(opts) => opts.execute().await,
TextSubCommand::Verify(opts) => opts.execute().await,
TextSubCommand::Generate(opts) => opts.execute().await,
}
}
}
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
mod cli;
mod process;
mod utils;
pub use cli::{
Base64Format, Base64SubCommand, HttpSubCommand, Opts, Subcommand, TextSignFormat,
TextSubCommand,
};
pub use cli::*;
use enum_dispatch::enum_dispatch;
pub use process::*;
pub use utils::*;

#[allow(async_fn_in_trait)]
#[enum_dispatch]
pub trait CmdExector {
async fn execute(self) -> anyhow::Result<()>;
}

0 comments on commit 1ee67c9

Please sign in to comment.