From 6f917e1d90f5f8f2e12d199ddbe985e3bafc8d4e Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 01:55:56 +0200 Subject: [PATCH] feat: add base32 and use fast32 as dep --- Cargo.toml | 2 +- src/format.rs | 8 ++++++-- src/main.rs | 7 ++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2e4b40a..44f64bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/format.rs b/src/format.rs index 909af1e..549d40f 100644 --- a/src/format.rs +++ b/src/format.rs @@ -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 } diff --git a/src/main.rs b/src/main.rs index babac9e..443fe4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::, 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 {