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
36
Cargo.lock
generated
36
Cargo.lock
generated
|
|
@ -58,6 +58,12 @@ version = "1.0.101"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5f0e0fee31ef5ed1ba1316088939cea399010ed7731dba877ed44aeb407a75ea"
|
checksum = "5f0e0fee31ef5ed1ba1316088939cea399010ed7731dba877ed44aeb407a75ea"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "2.10.0"
|
version = "2.10.0"
|
||||||
|
|
@ -141,6 +147,8 @@ name = "dolltool"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"num-bigint",
|
||||||
|
"num-traits",
|
||||||
"rand",
|
"rand",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -245,6 +253,34 @@ version = "2.8.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-bigint"
|
||||||
|
version = "0.4.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9"
|
||||||
|
dependencies = [
|
||||||
|
"num-integer",
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-integer"
|
||||||
|
version = "0.1.46"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-traits"
|
||||||
|
version = "0.2.19"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell_polyfill"
|
name = "once_cell_polyfill"
|
||||||
version = "1.70.2"
|
version = "1.70.2"
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,6 @@ edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.57", features = ["derive"] }
|
clap = { version = "4.5.57", features = ["derive"] }
|
||||||
|
num-bigint = "0.4.6"
|
||||||
|
num-traits = "0.2.19"
|
||||||
rand = "0.10.0"
|
rand = "0.10.0"
|
||||||
|
|
|
||||||
116
src/main.rs
116
src/main.rs
|
|
@ -1,4 +1,8 @@
|
||||||
|
use std::process;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use num_bigint::BigUint;
|
||||||
|
use num_traits::{One, ToPrimitive, Zero};
|
||||||
use rand::RngExt;
|
use rand::RngExt;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
|
|
@ -26,36 +30,40 @@ struct Args {
|
||||||
no_doll: bool,
|
no_doll: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_dollcode(mut n: u64) -> String {
|
fn to_dollcode(mut n: BigUint) -> Result<String, String> {
|
||||||
if n == 0 {
|
if n.is_zero() {
|
||||||
return String::from("value must be greater than zero :c");
|
return Err("Value of n must be greater than zero!".to_string());
|
||||||
}
|
}
|
||||||
let symbols = ['▖', '▘', '▌'];
|
let symbols = ['▖', '▘', '▌'];
|
||||||
|
let three = BigUint::from(3u8);
|
||||||
let mut result = String::new();
|
let mut result = String::new();
|
||||||
|
|
||||||
while n > 0 {
|
while n > BigUint::zero() {
|
||||||
n -= 1;
|
n -= BigUint::one();
|
||||||
let remainder = (n % 3) as usize;
|
let remainder = (&n % &three).to_usize().unwrap();
|
||||||
n /= 3;
|
n /= &three;
|
||||||
result.insert(0, symbols[remainder]);
|
result.insert(0, symbols[remainder]);
|
||||||
}
|
}
|
||||||
result
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_dollcode(code: &str) -> u64 {
|
fn from_dollcode(code: &str) -> Result<BigUint, String> {
|
||||||
code.chars()
|
let mut total = BigUint::zero();
|
||||||
.rev()
|
let three = BigUint::from(3u8);
|
||||||
.enumerate()
|
let mut multiplier = BigUint::one();
|
||||||
.map(|(power, c)| {
|
|
||||||
let val = match c {
|
for c in code.chars().rev() {
|
||||||
'▖' => 1,
|
let val = match c {
|
||||||
'▘' => 2,
|
'▖' => BigUint::from(1u8),
|
||||||
'▌' => 3,
|
'▘' => BigUint::from(2u8),
|
||||||
_ => panic!("invalid dollcode character: {} :c", c),
|
'▌' => BigUint::from(3u8),
|
||||||
};
|
_ => return Err(format!("Invalid dollcode character: '{}'", c)),
|
||||||
val * 3u64.pow(power as u32)
|
};
|
||||||
})
|
|
||||||
.sum()
|
total += val * &multiplier;
|
||||||
|
multiplier *= &three;
|
||||||
|
}
|
||||||
|
Ok(total)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_from_pattern(pattern: &str, upper: bool) -> String {
|
fn generate_from_pattern(pattern: &str, upper: bool) -> String {
|
||||||
|
|
@ -98,31 +106,71 @@ fn main() {
|
||||||
if args.no_doll {
|
if args.no_doll {
|
||||||
println!("{}", generated);
|
println!("{}", generated);
|
||||||
} else {
|
} else {
|
||||||
let doll: Vec<String> = generated.chars().map(|c| to_dollcode(c as u64)).collect();
|
match BigUint::parse_bytes(generated.as_bytes(), 10) {
|
||||||
println!("{} :: {}", generated, doll.join(" "));
|
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 let Some(input) = args.encode {
|
||||||
if args.text {
|
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(" "));
|
println!("{}", result.join(" "));
|
||||||
} else {
|
} else {
|
||||||
match input.parse::<u64>() {
|
match input.parse::<BigUint>() {
|
||||||
Ok(num) => println!("{}", to_dollcode(num)),
|
Ok(num) => match to_dollcode(num) {
|
||||||
Err(_) => println!("input must be a positive integer :c"),
|
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 {
|
} else if let Some(input) = args.decode {
|
||||||
if args.text {
|
if args.text {
|
||||||
let result: String = input
|
let mut decoded_chars = String::new();
|
||||||
.split_whitespace()
|
for part in input.split_whitespace() {
|
||||||
.map(|s| char::from_u32(from_dollcode(s) as u32).unwrap_or('?'))
|
match from_dollcode(part) {
|
||||||
.collect();
|
Ok(val) => {
|
||||||
println!("{}", result);
|
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 {
|
} 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