feat/other-formats #7

Merged
PlexSheep merged 6 commits from feat/other-formats into master 2024-05-12 18:46:11 +02:00
3 changed files with 30 additions and 1 deletions
Showing only changes of commit 00304ddff0 - Show all commits

View File

@ -14,6 +14,7 @@ keywords = ["cli"]
[dependencies] [dependencies]
anyhow = "1.0.83" anyhow = "1.0.83"
base64 = "0.22.1"
clap = { version = "4.5.4", features = ["derive"] } clap = { version = "4.5.4", features = ["derive"] }
clap-num = "1.1.1" clap-num = "1.1.1"
PlexSheep marked this conversation as resolved
Review

seems okay

seems okay

View File

@ -1,3 +1,5 @@
use base64::prelude::*;
pub type Num = u128; pub type Num = u128;
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
@ -6,15 +8,23 @@ pub enum Format {
Hex, Hex,
Bin, Bin,
Octal, Octal,
Base64,
} }
impl Format { impl Format {
pub fn prefix(&self) -> String { pub fn prefix(&self) -> String {
match self { 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", Format::Dec => "0d",
// very common
Format::Hex => "0x", Format::Hex => "0x",
// very common
Format::Bin => "0b", Format::Bin => "0b",
// somewhat common
Format::Octal => "0o", Format::Octal => "0o",
// perl and a few other programs seem to use this too
Format::Base64 => "0s",
} }
.to_string() .to_string()
} }
@ -36,7 +46,20 @@ impl Format {
Format::Dec => { Format::Dec => {
buf += &format!("{num}"); buf += &format!("{num}");
} }
Format::Base64 => buf += &BASE64_STANDARD.encode(u128_to_u8_slice(num)),
} }
buf buf
} }
} }
fn u128_to_u8_slice(mut num: u128) -> Vec<u8> {
if num == 0 {
return vec![0];
PlexSheep marked this conversation as resolved Outdated

This seems like a fairly common thing to do, perhaps we can use a dependency for this?

If it does not exist, it might be a good fit for libpt-bintols

This seems like a fairly common thing to do, perhaps we can use a dependency for this? If it does not exist, it might be a good fit for `libpt-bintols`

Issue for pt was created: PlexSheep/pt#76

Issue for pt was created: https://git.cscherr.de/PlexSheep/pt/issues/76
}
let mut buf: Vec<u8> = Vec::new();
while num > 0 {
buf.push(num as u8);
num >>= 8;
}
buf
}

View File

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