added value names for args and implemented string conversion for bases

This commit is contained in:
ika4422 2026-04-13 10:56:37 +01:00
parent 5ac61418a9
commit b68815c29c
No known key found for this signature in database
GPG key ID: F7FBF981D2A33785
2 changed files with 86 additions and 10 deletions

View file

@ -40,10 +40,16 @@ fn main() {
}
if let Some(pattern) = args.generate {
commands::run_generate(&pattern, args.count, args.base, args.no_doll, args.text);
commands::run_generate(
&pattern,
args.count,
args.base.radix(),
args.no_doll,
args.text,
);
} else if let Some(input) = args.encode {
commands::run_encode(&input, args.base, args.text);
commands::run_encode(&input, args.base.radix(), args.text);
} else if let Some(input) = args.decode {
commands::run_decode(&input, args.base, args.text);
commands::run_decode(&input, args.base.radix(), args.text);
}
}

View file

@ -3,25 +3,25 @@ use clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about)]
pub struct Args {
#[arg(short, long)]
#[arg(short, long, value_name = "INPUT")]
/// Encodes the following integer or text into dollcode
pub encode: Option<String>,
#[arg(short, long)]
#[arg(short, long, value_name = "DOLLCODE")]
/// Decodes the following dollcode into an integer or text
pub decode: Option<String>,
#[arg(short, long)]
#[arg(short, long, value_name = "PATTERN")]
/// Generates codes from a user-defined pattern (see --tokens for token list)
pub generate: Option<String>,
#[arg(short, long, default_value_t = 1)]
#[arg(short, long, default_value_t = 1, value_name = "N")]
/// Specifies how many codes to generate
pub count: usize,
#[arg(short, long, default_value_t = 10)]
/// Input/output base for integers (2, 8, 10, 16)
pub base: u32,
#[arg(short, long, default_value_t = Base(10))]
/// Input/output base for integers (2-36, or bin/oct/dec/hex/..)
pub base: Base,
#[arg(long, default_value_t = false)]
/// Will generate codes without encoding it into dollcode
@ -39,3 +39,73 @@ pub struct Args {
/// Display the license for dolltool
pub license: bool,
}
#[derive(Debug, Clone, Copy)]
pub struct Base(u32);
impl Base {
pub fn radix(self) -> u32 {
self.0
}
}
impl std::str::FromStr for Base {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
// c'tu inutile? ouais
// chu-tu une grosse nerd? ouais aussi
let radix = match s {
"bin" | "binary" => 2,
"ter" | "ternary" | "trinary" => 3,
"quat" | "quaternary" => 4,
"quin" | "quinary" => 5,
"sen" | "senary" => 6,
"sep" | "septenary" => 7,
"oct" | "octal" => 8,
"non" | "nonary" => 9,
"dec" | "denary" | "decimal" => 10,
"und" | "undecimal" => 11,
"duo" | "duodecimal" | "dozenal" => 12,
"tridecimal" => 13,
"tetradecimal" => 14,
"pentadecimal" => 15,
"hex" | "hexadecimal" => 16,
"heptadecimal" => 17,
"octodecimal" => 18,
"nonadecimal" | "enneadecimal" => 19,
"vig" | "vigesimal" => 20,
"unvigesimal" => 21,
"duovigesimal" => 22,
"trivigesimal" => 23,
"tetravigesimal" => 24,
"pentavigesimal" => 25,
"hexavigesimal" => 26,
"heptavigesimal" => 27,
"octovigesimal" => 28,
"nonavigesimal" => 29,
"trigesimal" => 30,
"untrigesimal" => 31,
"dtg" | "duotrigesimal" => 32,
"tritrigesimal" => 33,
"tetratrigesimal" => 34,
"pentatrigesimal" => 35,
"htg" | "hexatrigesimal" => 36,
_ => s
.parse::<u32>()
.map_err(|_| format!("'{}' is not a valid base", s))?,
};
if (2..=32).contains(&radix) {
Ok(Base(radix))
} else {
Err(format!("base must be between 2 and 36, got {}", radix))
}
}
}
impl std::fmt::Display for Base {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}