added proper error checking and implemented bigint :3
This commit is contained in:
parent
a5afe12931
commit
419735c5f2
3 changed files with 120 additions and 34 deletions
116
src/main.rs
116
src/main.rs
|
|
@ -1,4 +1,8 @@
|
|||
use std::process;
|
||||
|
||||
use clap::Parser;
|
||||
use num_bigint::BigUint;
|
||||
use num_traits::{One, ToPrimitive, Zero};
|
||||
use rand::RngExt;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
|
|
@ -26,36 +30,40 @@ struct Args {
|
|||
no_doll: bool,
|
||||
}
|
||||
|
||||
fn to_dollcode(mut n: u64) -> String {
|
||||
if n == 0 {
|
||||
return String::from("value must be greater than zero :c");
|
||||
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 > 0 {
|
||||
n -= 1;
|
||||
let remainder = (n % 3) as usize;
|
||||
n /= 3;
|
||||
while n > BigUint::zero() {
|
||||
n -= BigUint::one();
|
||||
let remainder = (&n % &three).to_usize().unwrap();
|
||||
n /= &three;
|
||||
result.insert(0, symbols[remainder]);
|
||||
}
|
||||
result
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn from_dollcode(code: &str) -> u64 {
|
||||
code.chars()
|
||||
.rev()
|
||||
.enumerate()
|
||||
.map(|(power, c)| {
|
||||
let val = match c {
|
||||
'▖' => 1,
|
||||
'▘' => 2,
|
||||
'▌' => 3,
|
||||
_ => panic!("invalid dollcode character: {} :c", c),
|
||||
};
|
||||
val * 3u64.pow(power as u32)
|
||||
})
|
||||
.sum()
|
||||
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 {
|
||||
|
|
@ -98,31 +106,71 @@ fn main() {
|
|||
if args.no_doll {
|
||||
println!("{}", generated);
|
||||
} else {
|
||||
let doll: Vec<String> = generated.chars().map(|c| to_dollcode(c as u64)).collect();
|
||||
println!("{} :: {}", generated, doll.join(" "));
|
||||
match BigUint::parse_bytes(generated.as_bytes(), 10) {
|
||||
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(" "));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(input) = args.encode {
|
||||
if args.text {
|
||||
let result: Vec<String> = input.chars().map(|c| to_dollcode(c as u64)).collect();
|
||||
let result: Vec<String> = input
|
||||
.chars()
|
||||
.map(|c| to_dollcode(BigUint::from(c as u64)).unwrap())
|
||||
.collect();
|
||||
println!("{}", result.join(" "));
|
||||
} else {
|
||||
match input.parse::<u64>() {
|
||||
Ok(num) => println!("{}", to_dollcode(num)),
|
||||
Err(_) => println!("input must be a positive integer :c"),
|
||||
match input.parse::<BigUint>() {
|
||||
Ok(num) => match to_dollcode(num) {
|
||||
Ok(code) => println!("{}", code),
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
eprintln!("Error: input must be a positive integer!");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if let Some(input) = args.decode {
|
||||
if args.text {
|
||||
let result: String = input
|
||||
.split_whitespace()
|
||||
.map(|s| char::from_u32(from_dollcode(s) as u32).unwrap_or('?'))
|
||||
.collect();
|
||||
println!("{}", result);
|
||||
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 {
|
||||
println!("{}", from_dollcode(&input));
|
||||
match from_dollcode(&input) {
|
||||
Ok(val) => println!("{}", val),
|
||||
Err(e) => {
|
||||
eprintln!("Error: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue