feat: add base32 and use fast32 as dep #4
cargo devel CI / cargo CI (push) Successful in 1m18s Details

This commit is contained in:
Christoph J. Scherr 2024-05-12 01:55:56 +02:00
parent 00304ddff0
commit cb0c56107a
3 changed files with 13 additions and 4 deletions

View File

@ -14,7 +14,7 @@ keywords = ["cli"]
[dependencies]
anyhow = "1.0.83"
base64 = "0.22.1"
clap = { version = "4.5.4", features = ["derive"] }
clap-num = "1.1.1"
fast32 = "1.0.2"

View File

@ -1,4 +1,4 @@
use base64::prelude::*;
use fast32;
pub type Num = u128;
@ -9,6 +9,7 @@ pub enum Format {
Bin,
Octal,
Base64,
Base32,
}
impl Format {
@ -25,6 +26,8 @@ impl Format {
Format::Octal => "0o",
// perl and a few other programs seem to use this too
Format::Base64 => "0s",
// no idea, I made this up
Format::Base32 => "032s",
}
.to_string()
}
@ -46,7 +49,8 @@ impl Format {
Format::Dec => {
buf += &format!("{num}");
}
Format::Base64 => buf += &BASE64_STANDARD.encode(u128_to_u8_slice(num)),
Format::Base64 => buf += &fast32::base64::RFC4648.encode(&u128_to_u8_slice(num)),
Format::Base32 => buf += &fast32::base32::RFC4648.encode(&u128_to_u8_slice(num)),
}
buf
}

View File

@ -13,7 +13,7 @@ use format::*;
#[clap(author, version, about, long_about = None)]
#[clap(group(
ArgGroup::new("format")
.args(&["hex", "bin", "oct", "dec", "base64"]),
.args(&["hex", "bin", "oct", "dec", "base64", "base32"]),
))]
struct Cli {
#[arg(short, long)]
@ -34,6 +34,9 @@ struct Cli {
#[arg(short = 's', long)]
/// format to base64
base64: bool,
#[arg(short = 'z', long)]
/// format to base32
base32: bool,
#[clap(value_parser=maybe_hex::<Num>, required=true)]
/// at least one number that should be formatted
///
@ -51,6 +54,8 @@ impl Cli {
Format::Dec
} else if self.base64 {
Format::Base64
} else if self.base32 {
Format::Base32
} else if self.hex {
Format::Hex
} else {