extracted logic from main module into separate modules

This commit is contained in:
ika4422 2026-04-13 09:50:10 +01:00
parent aeb3cfb71e
commit 5ac61418a9
No known key found for this signature in database
GPG key ID: F7FBF981D2A33785
3 changed files with 151 additions and 133 deletions

103
src/commands.rs Normal file
View file

@ -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<String> = 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<String> = 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);
}
}
}
}

View file

@ -15,66 +15,23 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
mod commands;
mod dollcode; mod dollcode;
mod models;
mod pattern_gen; mod pattern_gen;
use std::process; use std::process;
use clap::Parser; use clap::Parser;
use num_bigint::BigUint;
use num_traits::ToPrimitive;
use crate::{ use crate::models::Args;
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<String>,
#[arg(short, long)]
/// Decodes the following dollcode into an integer or text
decode: Option<String>,
#[arg(short, long)]
/// Generates codes from a user-defined pattern (see --tokens for token list)
generate: Option<String>,
#[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,
}
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
if args.license { if args.license {
println!("{}", include_str!("../LICENSE")); println!("{}", include_str!("../LICENSE"));
process::exit(1); process::exit(0);
} }
if args.tokens { if args.tokens {
@ -83,93 +40,10 @@ fn main() {
} }
if let Some(pattern) = args.generate { if let Some(pattern) = args.generate {
let mut generated_count = 0; commands::run_generate(&pattern, args.count, args.base, args.no_doll, args.text);
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<String> = generated
.chars()
.map(|c| to_dollcode(BigUint::from(c as u64)).unwrap())
.collect();
println!("{} :: {}", generated, doll.join(" "));
generated_count += 1;
}
}
}
} else if let Some(input) = args.encode { } else if let Some(input) = args.encode {
let encode_as_text = commands::run_encode(&input, args.base, args.text);
args.text || BigUint::parse_bytes(input.as_bytes(), args.base).is_none();
if encode_as_text {
let result: Vec<String> = 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);
}
}
}
} else if let Some(input) = args.decode { } else if let Some(input) = args.decode {
let decode_as_text = args.text || input.split_whitespace().count() > 1; commands::run_decode(&input, args.base, args.text);
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);
}
}
}
} }
} }

41
src/models.rs Normal file
View file

@ -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<String>,
#[arg(short, long)]
/// Decodes the following dollcode into an integer or text
pub decode: Option<String>,
#[arg(short, long)]
/// Generates codes from a user-defined pattern (see --tokens for token list)
pub generate: Option<String>,
#[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,
}