generated from PlexSheep/rs-base
Merge pull request 'feat/other-formats' (#7) from feat/other-formats into master
cargo devel CI / cargo CI (push) Successful in 1m12s
Details
cargo devel CI / cargo CI (push) Successful in 1m12s
Details
Reviewed-on: #7
This commit is contained in:
commit
e186dd67f8
|
@ -37,7 +37,7 @@ jobs:
|
||||||
- name: cargo fmt
|
- name: cargo fmt
|
||||||
run: cargo fmt --all
|
run: cargo fmt --all
|
||||||
- name: cargo test
|
- 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
|
- name: commit back to repository
|
||||||
uses: https://github.com/stefanzweifel/git-auto-commit-action@v5
|
uses: https://github.com/stefanzweifel/git-auto-commit-action@v5
|
||||||
with:
|
with:
|
||||||
|
|
|
@ -38,7 +38,7 @@ jobs:
|
||||||
- name: cargo fmt
|
- name: cargo fmt
|
||||||
run: cargo fmt --all
|
run: cargo fmt --all
|
||||||
- name: cargo test
|
- 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
|
- name: commit back to repository
|
||||||
uses: stefanzweifel/git-auto-commit-action@v5
|
uses: stefanzweifel/git-auto-commit-action@v5
|
||||||
with:
|
with:
|
||||||
|
|
|
@ -16,4 +16,6 @@ keywords = ["cli"]
|
||||||
anyhow = "1.0.83"
|
anyhow = "1.0.83"
|
||||||
clap = { version = "4.5.4", features = ["derive"] }
|
clap = { version = "4.5.4", features = ["derive"] }
|
||||||
clap-num = "1.1.1"
|
clap-num = "1.1.1"
|
||||||
|
fast32 = "1.0.2"
|
||||||
|
libpt = { version = "0.5.0", features = ["bintols"]}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
use libpt::bintols::split;
|
||||||
|
|
||||||
pub type Num = u128;
|
pub type Num = u128;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
@ -6,15 +8,26 @@ pub enum Format {
|
||||||
Hex,
|
Hex,
|
||||||
Bin,
|
Bin,
|
||||||
Octal,
|
Octal,
|
||||||
|
Base64,
|
||||||
|
Base32,
|
||||||
}
|
}
|
||||||
|
|
||||||
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",
|
||||||
|
// no idea, I made this up
|
||||||
|
Format::Base32 => "032s",
|
||||||
}
|
}
|
||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
@ -36,6 +49,8 @@ impl Format {
|
||||||
Format::Dec => {
|
Format::Dec => {
|
||||||
buf += &format!("{num}");
|
buf += &format!("{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
|
buf
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
12
src/main.rs
12
src/main.rs
|
@ -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", "base32"]),
|
||||||
))]
|
))]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
|
@ -31,6 +31,12 @@ 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,
|
||||||
|
#[arg(short = 'z', long)]
|
||||||
|
/// format to base32
|
||||||
|
base32: 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 +52,10 @@ 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.base32 {
|
||||||
|
Format::Base32
|
||||||
} else if self.hex {
|
} else if self.hex {
|
||||||
Format::Hex
|
Format::Hex
|
||||||
} else {
|
} else {
|
||||||
|
|
Reference in New Issue