From 00304ddff01370b26cbd5e39ac64fd2123a9522f Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 01:49:11 +0200 Subject: [PATCH 1/6] feat: add base64 format #4 This commit adds the option to output base64. Note that this converts numbers to base64, not strings. formatting 65 will output "QQ==", the same as using 'echo -ne "A" | base64' would (A is 65 in ASCII). --- Cargo.toml | 1 + src/format.rs | 23 +++++++++++++++++++++++ src/main.rs | 7 ++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 9f77ac8..2e4b40a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +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" diff --git a/src/format.rs b/src/format.rs index baaea0b..909af1e 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,3 +1,5 @@ +use base64::prelude::*; + pub type Num = u128; #[derive(Copy, Clone, Debug)] @@ -6,15 +8,23 @@ pub enum Format { Hex, Bin, Octal, + Base64, } impl Format { pub fn prefix(&self) -> String { match self { + // apperently used nowhere, sometimes 0 is used as a prefix but I + // think this makes it more clear that this is decimal Format::Dec => "0d", + // very common Format::Hex => "0x", + // very common Format::Bin => "0b", + // somewhat common Format::Octal => "0o", + // perl and a few other programs seem to use this too + Format::Base64 => "0s", } .to_string() } @@ -36,7 +46,20 @@ impl Format { Format::Dec => { buf += &format!("{num}"); } + Format::Base64 => buf += &BASE64_STANDARD.encode(u128_to_u8_slice(num)), } buf } } + +fn u128_to_u8_slice(mut num: u128) -> Vec { + if num == 0 { + return vec![0]; + } + let mut buf: Vec = Vec::new(); + while num > 0 { + buf.push(num as u8); + num >>= 8; + } + buf +} diff --git a/src/main.rs b/src/main.rs index 7f5595e..babac9e 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"]), + .args(&["hex", "bin", "oct", "dec", "base64"]), ))] struct Cli { #[arg(short, long)] @@ -31,6 +31,9 @@ struct Cli { #[arg(short, long)] /// format to octal oct: bool, + #[arg(short = 's', long)] + /// format to base64 + base64: bool, #[clap(value_parser=maybe_hex::, required=true)] /// at least one number that should be formatted /// @@ -46,6 +49,8 @@ impl Cli { Format::Bin } else if self.dec { Format::Dec + } else if self.base64 { + Format::Base64 } else if self.hex { Format::Hex } else { -- 2.40.1 From cb0c56107a4c65ff916be59c28032916f0a9cc19 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 01:55:56 +0200 Subject: [PATCH 2/6] feat: add base32 and use fast32 as dep #4 --- 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 { -- 2.40.1 From d4331a74926e1fd036bdcf8516adf9a8a77c39b2 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 11 May 2024 23:57:29 +0000 Subject: [PATCH 3/6] automatic cargo CI changes --- src/format.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/format.rs b/src/format.rs index 549d40f..4aaca53 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,5 +1,3 @@ -use fast32; - pub type Num = u128; #[derive(Copy, Clone, Debug)] -- 2.40.1 From 66144dcb3c5d7a44ab974092446607cba847c6e9 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 17:27:43 +0200 Subject: [PATCH 4/6] ci add cargo test of docs --- .gitea/workflows/cargo.yaml | 2 +- .github/workflows/cargo.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitea/workflows/cargo.yaml b/.gitea/workflows/cargo.yaml index b755c81..6b9afbb 100644 --- a/.gitea/workflows/cargo.yaml +++ b/.gitea/workflows/cargo.yaml @@ -37,7 +37,7 @@ jobs: - name: cargo fmt run: cargo fmt --all - name: cargo test - run: cargo test --all-features --all-targets --workspace + run: cargo test --all-features --all-targets --workspace && cargo test --all-features --workspace --doc - name: commit back to repository uses: https://github.com/stefanzweifel/git-auto-commit-action@v5 with: diff --git a/.github/workflows/cargo.yaml b/.github/workflows/cargo.yaml index 29abfde..d2c9936 100644 --- a/.github/workflows/cargo.yaml +++ b/.github/workflows/cargo.yaml @@ -38,7 +38,7 @@ jobs: - name: cargo fmt run: cargo fmt --all - name: cargo test - run: cargo test --all-features --all-targets --workspace + run: cargo test --all-features --all-targets --workspace && cargo test --all-features --workspace --doc - name: commit back to repository uses: stefanzweifel/git-auto-commit-action@v5 with: -- 2.40.1 From 24bf6cd97e418809883d758bb42d24d583ef7037 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 18:36:36 +0200 Subject: [PATCH 5/6] fix: use a dependency to split the int instead of a builtin function #8 --- Cargo.toml | 1 + src/format.rs | 18 ++++-------------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 44f64bf..4598dd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,4 +17,5 @@ anyhow = "1.0.83" clap = { version = "4.5.4", features = ["derive"] } clap-num = "1.1.1" fast32 = "1.0.2" +libpt = { version = "0.5.0", features = ["bintols"]} diff --git a/src/format.rs b/src/format.rs index 4aaca53..fe425df 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,3 +1,5 @@ +use libpt::bintols::split; + pub type Num = u128; #[derive(Copy, Clone, Debug)] @@ -47,21 +49,9 @@ impl Format { Format::Dec => { buf += &format!("{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)), + Format::Base64 => buf += &fast32::base64::RFC4648.encode(&split::unsigned_to_vec(num)), + Format::Base32 => buf += &fast32::base32::RFC4648.encode(&split::unsigned_to_vec(num)), } buf } } - -fn u128_to_u8_slice(mut num: u128) -> Vec { - if num == 0 { - return vec![0]; - } - let mut buf: Vec = Vec::new(); - while num > 0 { - buf.push(num as u8); - num >>= 8; - } - buf -} -- 2.40.1 From ec4aa0953887e12c99eb5d7b0f867eb2f1aba728 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 18:43:44 +0200 Subject: [PATCH 6/6] feat: add lib.rs #10 --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..bcab8de --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,10 @@ +//! Format numbers +//! +//! This crate contains several utility functions for formatting numbers +//! into other systems, such as converting decimal numbers to hexadecimal. +//! +//! See [Format] for supported formats. +//! +//! Note that this crate is primarily used as a executable. + +pub mod format; -- 2.40.1