Skip to content

Commit

Permalink
refactor: use enum_dispatch to reduce duplicated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
hedon954 committed May 9, 2024
1 parent 9d30d2f commit 328b168
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 44 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ clap = { version = "4.5.4", features = ["derive"] }
colored = "2.1.0"
csv = "1.3.0"
ed25519-dalek = { version = "2.1.1", features = ["rand_core"] }
enum_dispatch = "0.3.13"
rand = "0.8.5"
serde = { version = "1.0.198", features = ["derive"] }
serde_json = "1.0.116"
Expand Down
11 changes: 2 additions & 9 deletions src/cli/base64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::{Parser, ValueEnum};
use enum_dispatch::enum_dispatch;

use crate::{
process::{process_decode, process_encode},
Expand All @@ -8,6 +9,7 @@ use crate::{
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 @@ -47,15 +49,6 @@ pub enum Base64Format {
Urlsafe,
}

impl CmdExector for Base64SubCommand {
async fn execute(self) -> anyhow::Result<()> {
match self {
Base64SubCommand::Decode(opts) => opts.execute().await,
Base64SubCommand::Encode(opts) => opts.execute().await,
}
}
}

impl CmdExector for Base64DecodeOpts {
async fn execute(self) -> anyhow::Result<()> {
let encoded = process_decode(&self.input, self.format, self.no_padding)?;
Expand Down
10 changes: 2 additions & 8 deletions src/cli/http.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::{cli::verify_dir, process::process_http_serve, CmdExector};
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 @@ -16,14 +18,6 @@ pub struct HttpServeOpts {
pub port: u16,
}

impl CmdExector for HttpSubCommand {
async fn execute(self) -> anyhow::Result<()> {
match self {
HttpSubCommand::Serve(opts) => opts.execute().await,
}
}
}

impl CmdExector for HttpServeOpts {
async fn execute(self) -> anyhow::Result<()> {
process_http_serve(self.dir, self.port).await?;
Expand Down
18 changes: 10 additions & 8 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ pub mod http;
pub mod text;
pub mod time;

use std::path::{Path, PathBuf};

use self::http::HttpSubCommand;
use self::time::TimeOpts;
use clap::{Parser, Subcommand};
use enum_dispatch::enum_dispatch;

use std::path::{Path, PathBuf};

pub use self::base64::Base64SubCommand;
pub use self::csv::{CsvOpts, OutputFormat};
pub use self::gen_pass::GenPassOpts;
pub use self::text::{TextSignFormat, TextSignOpts, TextSubCommand, TextVerifyOpts};
pub use self::base64::*;
pub use self::csv::*;
pub use self::gen_pass::*;
pub use self::http::*;
pub use self::text::*;
pub use self::time::*;

#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
Expand All @@ -25,6 +26,7 @@ pub struct Opts {

/// rcli csv -i input.csv -o output.json --header -d ','
#[derive(Debug, Subcommand)]
#[enum_dispatch(CmdExector)]
pub enum SubCommand {
#[command(name = "csv", about = "Show CSV, or convert CSV to other formats")]
Csv(CsvOpts),
Expand Down
12 changes: 2 additions & 10 deletions src/cli/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{fs, path::PathBuf};
use anyhow::Ok;
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use clap::{Parser, ValueEnum};
use enum_dispatch::enum_dispatch;

use crate::{
cli::{verify_dir, verify_file},
Expand All @@ -11,6 +12,7 @@ use crate::{
};

#[derive(Debug, Parser)]
#[enum_dispatch(CmdExector)]
pub enum TextSubCommand {
#[command(about = "Sign a message with a private key")]
Sign(TextSignOpts),
Expand Down Expand Up @@ -63,16 +65,6 @@ pub struct GenKeyOpts {
pub output: PathBuf,
}

impl CmdExector for TextSubCommand {
async fn execute(self) -> anyhow::Result<()> {
match self {
TextSubCommand::GenKey(opts) => opts.execute().await,
TextSubCommand::Sign(opts) => opts.execute().await,
TextSubCommand::Verify(opts) => opts.execute().await,
}
}
}

impl CmdExector for TextSignOpts {
async fn execute(self) -> anyhow::Result<()> {
let sig = process_text_sign(&self.input, &self.key, self.format)?;
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ pub mod cli;
pub mod process;
pub mod utils;

pub use cli::TextSignFormat;
pub use cli::*;
use enum_dispatch::enum_dispatch;

#[allow(async_fn_in_trait)]
#[enum_dispatch]
pub trait CmdExector {
async fn execute(self) -> anyhow::Result<()>;
}
9 changes: 1 addition & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ use rcli::{cli, CmdExector};
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let opts = cli::Opts::parse();
match opts.cmd {
cli::SubCommand::Csv(opts) => opts.execute().await?,
cli::SubCommand::GenPass(opts) => opts.execute().await?,
cli::SubCommand::Base64(cmd) => cmd.execute().await?,
cli::SubCommand::Time(opts) => opts.execute().await?,
cli::SubCommand::Text(cmd) => cmd.execute().await?,
cli::SubCommand::Http(cmd) => cmd.execute().await?,
}
opts.cmd.execute().await?;
Ok(())
}

0 comments on commit 328b168

Please sign in to comment.