Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use clap value_enum replace some trait #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 5 additions & 38 deletions src/cli/base64.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::{fmt, str::FromStr};

use clap::Parser;
use clap::{Parser, ValueEnum};
use enum_dispatch::enum_dispatch;

use crate::CmdExector;
Expand All @@ -20,53 +18,22 @@ pub enum Base64SubCommand {
pub struct Base64EncodeOpts {
#[arg(short, long, value_parser = verify_file, default_value = "-")]
pub input: String,
#[arg(long, value_parser = parse_base64_format, default_value = "standard")]
#[arg(value_enum, long, default_value_t=Base64Format::Standard)]
pub format: Base64Format,
}

#[derive(Debug, Parser)]
pub struct Base64DecodeOpts {
#[arg(short, long, value_parser = verify_file, default_value = "-")]
pub input: String,
#[arg(long, value_parser = parse_base64_format, default_value = "standard")]
#[arg(long, value_enum, long, default_value_t=Base64Format::Standard)]
pub format: Base64Format,
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum Base64Format {
Standard,
UrlSafe,
}

fn parse_base64_format(format: &str) -> Result<Base64Format, anyhow::Error> {
format.parse()
}

impl FromStr for Base64Format {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"standard" => Ok(Base64Format::Standard),
"urlsafe" => Ok(Base64Format::UrlSafe),
_ => Err(anyhow::anyhow!("Invalid format")),
}
}
}

impl From<Base64Format> for &'static str {
fn from(format: Base64Format) -> Self {
match format {
Base64Format::Standard => "standard",
Base64Format::UrlSafe => "urlsafe",
}
}
}

impl fmt::Display for Base64Format {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", Into::<&str>::into(*self))
}
Urlsafe,
}

impl CmdExector for Base64EncodeOpts {
Expand Down
39 changes: 9 additions & 30 deletions src/cli/csv.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::CmdExector;

use super::verify_file;
use clap::Parser;
use std::{fmt, str::FromStr};
use clap::{Parser, ValueEnum};
use std::fmt;

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum OutputFormat {
Json,
Yaml,
Expand All @@ -18,7 +18,7 @@ pub struct CsvOpts {
#[arg(short, long)] // "output.json".into()
pub output: Option<String>,

#[arg(long, value_parser = parse_format, default_value = "json")]
#[arg(long,value_enum, default_value_t=OutputFormat::Json)]
pub format: OutputFormat,

#[arg(short, long, default_value_t = ',')]
Expand All @@ -39,33 +39,12 @@ impl CmdExector for CsvOpts {
}
}

fn parse_format(format: &str) -> Result<OutputFormat, anyhow::Error> {
format.parse()
}

impl From<OutputFormat> for &'static str {
fn from(format: OutputFormat) -> Self {
match format {
OutputFormat::Json => "json",
OutputFormat::Yaml => "yaml",
}
}
}

impl FromStr for OutputFormat {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"json" => Ok(OutputFormat::Json),
"yaml" => Ok(OutputFormat::Yaml),
_ => Err(anyhow::anyhow!("Invalid format")),
}
}
}

impl fmt::Display for OutputFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", Into::<&str>::into(*self))
let lowercase = match self {
OutputFormat::Json => "json",
OutputFormat::Yaml => "yaml",
};
write!(f, "{}", lowercase)
}
}
43 changes: 6 additions & 37 deletions src/cli/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::{

use super::{verify_file, verify_path};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use clap::Parser;
use clap::{Parser, ValueEnum};
use enum_dispatch::enum_dispatch;
use std::{fmt, path::PathBuf, str::FromStr};
use std::path::PathBuf;
use tokio::fs;

#[derive(Debug, Parser)]
Expand All @@ -27,7 +27,7 @@ pub struct TextSignOpts {
pub input: String,
#[arg(short, long, value_parser = verify_file)]
pub key: String,
#[arg(long, default_value = "blake3", value_parser = parse_text_sign_format)]
#[arg(value_enum, long, default_value_t=TextSignFormat::Blake3)]
pub format: TextSignFormat,
}

Expand All @@ -39,55 +39,24 @@ pub struct TextVerifyOpts {
pub key: String,
#[arg(long)]
pub sig: String,
#[arg(long, default_value = "blake3", value_parser = parse_text_sign_format)]
#[arg(value_enum, long, default_value_t=TextSignFormat::Blake3)]
pub format: TextSignFormat,
}

#[derive(Debug, Parser)]
pub struct KeyGenerateOpts {
#[arg(long, default_value = "blake3", value_parser = parse_text_sign_format)]
#[arg(value_enum, long, default_value_t=TextSignFormat::Blake3)]
pub format: TextSignFormat,
#[arg(short, long, value_parser = verify_path)]
pub output_path: PathBuf,
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum TextSignFormat {
Blake3,
Ed25519,
}

fn parse_text_sign_format(format: &str) -> Result<TextSignFormat, anyhow::Error> {
format.parse()
}

impl FromStr for TextSignFormat {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"blake3" => Ok(TextSignFormat::Blake3),
"ed25519" => Ok(TextSignFormat::Ed25519),
_ => Err(anyhow::anyhow!("Invalid format")),
}
}
}

impl From<TextSignFormat> for &'static str {
fn from(format: TextSignFormat) -> Self {
match format {
TextSignFormat::Blake3 => "blake3",
TextSignFormat::Ed25519 => "ed25519",
}
}
}

impl fmt::Display for TextSignFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", Into::<&str>::into(*self))
}
}

impl CmdExector for TextSignOpts {
async fn execute(self) -> anyhow::Result<()> {
let mut reader = get_reader(&self.input)?;
Expand Down
6 changes: 3 additions & 3 deletions src/process/b64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn process_encode(reader: &mut dyn Read, format: Base64Format) -> Result<Str
reader.read_to_end(&mut buf)?;
let encoded = match format {
Base64Format::Standard => STANDARD.encode(&buf),
Base64Format::UrlSafe => URL_SAFE_NO_PAD.encode(&buf),
Base64Format::Urlsafe => URL_SAFE_NO_PAD.encode(&buf),
};

Ok(encoded)
Expand All @@ -25,7 +25,7 @@ pub fn process_decode(reader: &mut dyn Read, format: Base64Format) -> Result<Str

let decoded = match format {
Base64Format::Standard => STANDARD.decode(buf)?,
Base64Format::UrlSafe => URL_SAFE_NO_PAD.decode(buf)?,
Base64Format::Urlsafe => URL_SAFE_NO_PAD.decode(buf)?,
};
// TODO: decoded data might not be string (but for this example, we assume it is)
Ok(String::from_utf8(decoded)?)
Expand All @@ -49,7 +49,7 @@ mod tests {
fn test_process_decode() -> Result<()> {
let input = "fixtures/b64.txt";
let mut reader = get_reader(input)?;
let format = Base64Format::UrlSafe;
let format = Base64Format::Urlsafe;
process_decode(&mut reader, format)?;

Ok(())
Expand Down