reorganized code to be neater in separate modules :3

This commit is contained in:
ika4422 2026-02-09 22:06:21 +00:00
parent b79885e062
commit 0576dc0ea4
No known key found for this signature in database
3 changed files with 78 additions and 68 deletions

38
src/dollcode.rs Normal file
View file

@ -0,0 +1,38 @@
use num_bigint::BigUint;
use num_traits::{One, ToPrimitive, Zero};
pub fn to_dollcode(mut n: BigUint) -> Result<String, String> {
if n.is_zero() {
return Err("Value of n must be greater than zero!".to_string());
}
let symbols = ['▖', '▘', '▌'];
let three = BigUint::from(3u8);
let mut result = String::new();
while n > BigUint::zero() {
n -= BigUint::one();
let remainder = (&n % &three).to_usize().unwrap();
n /= &three;
result.insert(0, symbols[remainder]);
}
Ok(result)
}
pub fn from_dollcode(code: &str) -> Result<BigUint, String> {
let mut total = BigUint::zero();
let three = BigUint::from(3u8);
let mut multiplier = BigUint::one();
for c in code.chars().rev() {
let val = match c {
'▖' => BigUint::from(1u8),
'▘' => BigUint::from(2u8),
'▌' => BigUint::from(3u8),
_ => return Err(format!("Invalid dollcode character: '{}'", c)),
};
total += val * &multiplier;
multiplier *= &three;
}
Ok(total)
}

View file

@ -15,12 +15,19 @@
* 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 dollcode;
mod pattern_gen;
use std::process; use std::process;
use clap::Parser; use clap::Parser;
use num_bigint::BigUint; use num_bigint::BigUint;
use num_traits::{One, ToPrimitive, Zero}; use num_traits::ToPrimitive;
use rand::RngExt;
use crate::{
dollcode::{from_dollcode, to_dollcode},
pattern_gen::generate_from_pattern,
};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about)] #[command(author, version, about)]
@ -50,72 +57,6 @@ struct Args {
license: bool, license: bool,
} }
fn to_dollcode(mut n: BigUint) -> Result<String, String> {
if n.is_zero() {
return Err("Value of n must be greater than zero!".to_string());
}
let symbols = ['▖', '▘', '▌'];
let three = BigUint::from(3u8);
let mut result = String::new();
while n > BigUint::zero() {
n -= BigUint::one();
let remainder = (&n % &three).to_usize().unwrap();
n /= &three;
result.insert(0, symbols[remainder]);
}
Ok(result)
}
fn from_dollcode(code: &str) -> Result<BigUint, String> {
let mut total = BigUint::zero();
let three = BigUint::from(3u8);
let mut multiplier = BigUint::one();
for c in code.chars().rev() {
let val = match c {
'▖' => BigUint::from(1u8),
'▘' => BigUint::from(2u8),
'▌' => BigUint::from(3u8),
_ => return Err(format!("Invalid dollcode character: '{}'", c)),
};
total += val * &multiplier;
multiplier *= &three;
}
Ok(total)
}
fn generate_from_pattern(pattern: &str, upper: bool) -> String {
let mut rng = rand::rng();
let mut output = String::new();
for c in pattern.chars() {
match c {
'n' => output.push(rng.random_range('0'..='9')),
'l' => {
let letter = rng.random_range('a'..='z');
output.push(if upper {
letter.to_ascii_uppercase()
} else {
letter
});
}
'a' => {
let pool = if upper {
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
} else {
"0123456789abcdefghijklmnopqrstuvwxyz"
};
let idx = rng.random_range(0..pool.len());
output.push(pool.chars().nth(idx).unwrap());
}
_ => output.push(c),
}
}
output
}
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();

31
src/pattern_gen.rs Normal file
View file

@ -0,0 +1,31 @@
use rand::RngExt;
pub fn generate_from_pattern(pattern: &str, upper: bool) -> String {
let mut rng = rand::rng();
let mut output = String::new();
for c in pattern.chars() {
match c {
'n' => output.push(rng.random_range('0'..='9')),
'l' => {
let letter = rng.random_range('a'..='z');
output.push(if upper {
letter.to_ascii_uppercase()
} else {
letter
});
}
'a' => {
let pool = if upper {
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
} else {
"0123456789abcdefghijklmnopqrstuvwxyz"
};
let idx = rng.random_range(0..pool.len());
output.push(pool.chars().nth(idx).unwrap());
}
_ => output.push(c),
}
}
output
}