diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..9ecfec3 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,103 @@ +use std::process; + +use num_bigint::BigUint; +use num_traits::ToPrimitive; + +use crate::{ + dollcode::{from_dollcode, to_dollcode}, + pattern_gen::generate_from_pattern, +}; + +pub fn run_generate(pattern: &str, count: usize, base: u32, no_doll: bool, text: bool) { + let mut generated_count = 0; + + while generated_count < count { + let generated = generate_from_pattern(pattern); + + if no_doll { + println!("{}", generated); + generated_count += 1; + continue; + } + + let num = if text { + None + } else { + BigUint::parse_bytes(generated.as_bytes(), base) + }; + + match num { + Some(num) => match to_dollcode(num) { + Ok(code) => { + println!("{} :: {}", generated, code); + generated_count += 1; + } + Err(_) => continue, + }, + None => { + let doll: Vec = generated + .chars() + .map(|c| to_dollcode(BigUint::from(c as u64)).unwrap()) + .collect(); + + println!("{} :: {}", generated, doll.join(" ")); + generated_count += 1; + } + } + } +} + +pub fn run_encode(input: &str, base: u32, text: bool) { + let encode_as_text = text || BigUint::parse_bytes(input.as_bytes(), base).is_none(); + + if encode_as_text { + let result: Vec = input + .chars() + .map(|c| to_dollcode(BigUint::from(c as u64)).unwrap()) + .collect(); + println!("{}", result.join(" ")); + } else { + let num = BigUint::parse_bytes(input.as_bytes(), base).unwrap(); + + match to_dollcode(num) { + Ok(code) => println!("{}", code), + Err(e) => { + eprintln!("Error: {}", e); + process::exit(1); + } + } + } +} + +pub fn run_decode(input: &str, base: u32, text: bool) { + let decode_as_text = text || input.split_whitespace().count() > 1; + + if decode_as_text { + let mut decoded_chars = String::new(); + + for part in input.split_whitespace() { + match from_dollcode(part) { + Ok(val) => { + if let Some(u) = val.to_u32() { + decoded_chars.push(char::from_u32(u).unwrap_or('?')); + } else { + decoded_chars.push('?'); + } + } + Err(e) => { + eprintln!("Error: {}", e); + process::exit(1); + } + } + } + println!("{}", decoded_chars); + } else { + match from_dollcode(input) { + Ok(val) => println!("{}", val.to_str_radix(base)), + Err(e) => { + eprintln!("Error: {}", e); + process::exit(1); + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 00b26aa..b129c88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,66 +15,23 @@ * along with this program. If not, see . */ +mod commands; mod dollcode; +mod models; mod pattern_gen; use std::process; use clap::Parser; -use num_bigint::BigUint; -use num_traits::ToPrimitive; -use crate::{ - dollcode::{from_dollcode, to_dollcode}, - pattern_gen::generate_from_pattern, -}; - -#[derive(Parser, Debug)] -#[command(author, version, about)] -struct Args { - #[arg(short, long)] - /// Encodes the following integer or text into dollcode - encode: Option, - - #[arg(short, long)] - /// Decodes the following dollcode into an integer or text - decode: Option, - - #[arg(short, long)] - /// Generates codes from a user-defined pattern (see --tokens for token list) - generate: Option, - - #[arg(short, long, default_value_t = 1)] - /// Specifies how many codes to generate - count: usize, - - #[arg(short, long, default_value_t = 10)] - /// Input/output base for integers (2, 8, 10, 16) - base: u32, - - #[arg(long, default_value_t = false)] - /// Will generate codes without encoding it into dollcode - no_doll: bool, - - #[arg(short, long, default_value_t = false)] - /// Specifies the encoding/decoding method as text, instead of an integer - text: bool, - - #[arg(long, default_value_t = false)] - /// List all available pattern tokens - tokens: bool, - - #[arg(long)] - /// Display the license for dolltool - license: bool, -} +use crate::models::Args; fn main() { let args = Args::parse(); if args.license { println!("{}", include_str!("../LICENSE")); - process::exit(1); + process::exit(0); } if args.tokens { @@ -83,93 +40,10 @@ fn main() { } if let Some(pattern) = args.generate { - let mut generated_count = 0; - - while generated_count < args.count { - let generated = generate_from_pattern(&pattern); - - if args.no_doll { - println!("{}", generated); - generated_count += 1; - continue; - } - - let num = if args.text { - None - } else { - BigUint::parse_bytes(generated.as_bytes(), args.base) - }; - - match num { - Some(num) => match to_dollcode(num) { - Ok(code) => { - println!("{} :: {}", generated, code); - generated_count += 1; - } - Err(_) => continue, - }, - None => { - let doll: Vec = generated - .chars() - .map(|c| to_dollcode(BigUint::from(c as u64)).unwrap()) - .collect(); - - println!("{} :: {}", generated, doll.join(" ")); - generated_count += 1; - } - } - } + commands::run_generate(&pattern, args.count, args.base, args.no_doll, args.text); } else if let Some(input) = args.encode { - let encode_as_text = - args.text || BigUint::parse_bytes(input.as_bytes(), args.base).is_none(); - - if encode_as_text { - let result: Vec = input - .chars() - .map(|c| to_dollcode(BigUint::from(c as u64)).unwrap()) - .collect(); - println!("{}", result.join(" ")); - } else { - let num = BigUint::parse_bytes(input.as_bytes(), args.base).unwrap(); - - match to_dollcode(num) { - Ok(code) => println!("{}", code), - Err(e) => { - eprintln!("Error: {}", e); - process::exit(1); - } - } - } + commands::run_encode(&input, args.base, args.text); } else if let Some(input) = args.decode { - let decode_as_text = args.text || input.split_whitespace().count() > 1; - - if decode_as_text { - let mut decoded_chars = String::new(); - - for part in input.split_whitespace() { - match from_dollcode(part) { - Ok(val) => { - if let Some(u) = val.to_u32() { - decoded_chars.push(char::from_u32(u).unwrap_or('?')); - } else { - decoded_chars.push('?'); - } - } - Err(e) => { - eprintln!("Error: {}", e); - process::exit(1); - } - } - } - println!("{}", decoded_chars); - } else { - match from_dollcode(&input) { - Ok(val) => println!("{}", val.to_str_radix(args.base)), - Err(e) => { - eprintln!("Error: {}", e); - process::exit(1); - } - } - } + commands::run_decode(&input, args.base, args.text); } } diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..cdafb70 --- /dev/null +++ b/src/models.rs @@ -0,0 +1,41 @@ +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(author, version, about)] +pub struct Args { + #[arg(short, long)] + /// Encodes the following integer or text into dollcode + pub encode: Option, + + #[arg(short, long)] + /// Decodes the following dollcode into an integer or text + pub decode: Option, + + #[arg(short, long)] + /// Generates codes from a user-defined pattern (see --tokens for token list) + pub generate: Option, + + #[arg(short, long, default_value_t = 1)] + /// 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(long, default_value_t = false)] + /// Will generate codes without encoding it into dollcode + pub no_doll: bool, + + #[arg(short, long, default_value_t = false)] + /// Specifies the encoding/decoding method as text, instead of an integer + pub text: bool, + + #[arg(long, default_value_t = false)] + /// List all available pattern tokens + pub tokens: bool, + + #[arg(long)] + /// Display the license for dolltool + pub license: bool, +}