diff --git a/src/cli/base64.rs b/src/cli/base64.rs index 3d24cfd..4967390 100644 --- a/src/cli/base64.rs +++ b/src/cli/base64.rs @@ -1,6 +1,4 @@ -use std::{fmt, str::FromStr}; - -use clap::Parser; +use clap::{Parser, ValueEnum}; use enum_dispatch::enum_dispatch; use crate::CmdExector; @@ -20,7 +18,7 @@ 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, } @@ -28,45 +26,14 @@ pub struct Base64EncodeOpts { 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 { - format.parse() -} - -impl FromStr for Base64Format { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - match s { - "standard" => Ok(Base64Format::Standard), - "urlsafe" => Ok(Base64Format::UrlSafe), - _ => Err(anyhow::anyhow!("Invalid format")), - } - } -} - -impl From 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 { diff --git a/src/cli/csv.rs b/src/cli/csv.rs index 7d2437e..4b6fa1d 100644 --- a/src/cli/csv.rs +++ b/src/cli/csv.rs @@ -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, @@ -18,7 +18,7 @@ pub struct CsvOpts { #[arg(short, long)] // "output.json".into() pub output: Option, - #[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 = ',')] @@ -39,33 +39,12 @@ impl CmdExector for CsvOpts { } } -fn parse_format(format: &str) -> Result { - format.parse() -} - -impl From 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 { - 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) } } diff --git a/src/cli/text.rs b/src/cli/text.rs index 3a3fc87..9cbfc70 100644 --- a/src/cli/text.rs +++ b/src/cli/text.rs @@ -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)] @@ -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, } @@ -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 { - format.parse() -} - -impl FromStr for TextSignFormat { - type Err = anyhow::Error; - - fn from_str(s: &str) -> Result { - match s { - "blake3" => Ok(TextSignFormat::Blake3), - "ed25519" => Ok(TextSignFormat::Ed25519), - _ => Err(anyhow::anyhow!("Invalid format")), - } - } -} - -impl From 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)?; diff --git a/src/process/b64.rs b/src/process/b64.rs index 4936ce1..6b82174 100644 --- a/src/process/b64.rs +++ b/src/process/b64.rs @@ -11,7 +11,7 @@ pub fn process_encode(reader: &mut dyn Read, format: Base64Format) -> Result STANDARD.encode(&buf), - Base64Format::UrlSafe => URL_SAFE_NO_PAD.encode(&buf), + Base64Format::Urlsafe => URL_SAFE_NO_PAD.encode(&buf), }; Ok(encoded) @@ -25,7 +25,7 @@ pub fn process_decode(reader: &mut dyn Read, format: Base64Format) -> Result 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)?) @@ -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(())