dolltool/src/main.rs

151 lines
4.7 KiB
Rust

/*
* Copyright (C) 2026 ika4422
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
mod dollcode;
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<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 (n = number; l = letter; L = uppercase letter; a = alphanumeric; A = uppercase alphanumeric)
generate: Option<String>,
#[arg(short, long, default_value_t = 1)]
/// Specifies how many codes to generate
count: usize,
#[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, not as an integer
text: bool,
#[arg(long)]
/// Display the license for dolltool
license: bool,
}
fn main() {
let args = Args::parse();
if args.license {
println!("{}", include_str!("../LICENSE"));
process::exit(1);
}
if let Some(pattern) = args.generate {
for _ in 0..args.count {
let generated = generate_from_pattern(&pattern);
if args.no_doll {
println!("{}", generated);
} else {
let as_num = if args.text {
None
} else {
BigUint::parse_bytes(generated.as_bytes(), 10)
};
match as_num {
Some(num) => {
println!("{} :: {}", generated, to_dollcode(num).unwrap());
}
None => {
let doll: Vec<String> = generated
.chars()
.map(|c| to_dollcode(BigUint::from(c as u64)).unwrap())
.collect();
println!("{} :: {}", generated, doll.join(" "));
}
}
}
}
} else if let Some(input) = args.encode {
let encode_as_text = args.text || input.parse::<BigUint>().is_err();
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 = input.parse::<BigUint>().unwrap();
match to_dollcode(num) {
Ok(code) => println!("{}", code),
Err(e) => {
eprintln!("Error: {}", e);
process::exit(1);
}
}
}
} 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),
Err(e) => {
eprintln!("Error: {}", e);
process::exit(1);
}
}
}
}
}