From b68815c29c7d0a58188c3a312f716c3caa73d7c6 Mon Sep 17 00:00:00 2001 From: ika4422 Date: Mon, 13 Apr 2026 10:56:37 +0100 Subject: [PATCH] added value names for args and implemented string conversion for bases --- src/main.rs | 12 ++++++-- src/models.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index b129c88..cb2a4e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); } } diff --git a/src/models.rs b/src/models.rs index cdafb70..43cfe07 100644 --- a/src/models.rs +++ b/src/models.rs @@ -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, - #[arg(short, long)] + #[arg(short, long, value_name = "DOLLCODE")] /// Decodes the following dollcode into an integer or text pub decode: Option, - #[arg(short, long)] + #[arg(short, long, value_name = "PATTERN")] /// Generates codes from a user-defined pattern (see --tokens for token list) pub generate: Option, - #[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 { + // 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::() + .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) + } +}