first working version :3
This commit is contained in:
parent
aae960ce07
commit
a2030b8b71
3 changed files with 258 additions and 2 deletions
73
src/main.rs
73
src/main.rs
|
|
@ -1,3 +1,72 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about = "Dollcode Encoder/Decoder")]
|
||||
struct Args {
|
||||
#[arg(short, long)]
|
||||
encode: Option<String>,
|
||||
|
||||
#[arg(short, long)]
|
||||
decode: Option<String>,
|
||||
|
||||
#[arg(short, long, default_value_t = false)]
|
||||
text: bool,
|
||||
}
|
||||
|
||||
fn to_dollcode(mut n: u64) -> String {
|
||||
if n == 0 {
|
||||
return String::from("value must be greater than zero :c");
|
||||
}
|
||||
let symbols = ['▖', '▘', '▌'];
|
||||
let mut result = String::new();
|
||||
|
||||
while n > 0 {
|
||||
n -= 1;
|
||||
let remainder = (n % 3) as usize;
|
||||
n /= 3;
|
||||
result.insert(0, symbols[remainder]);
|
||||
}
|
||||
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 main() {
|
||||
let args = Args::parse();
|
||||
|
||||
if let Some(input) = args.encode {
|
||||
if args.text {
|
||||
let result: Vec<String> = input.chars().map(|c| to_dollcode(c as u64)).collect();
|
||||
println!("{}", result.join(" "));
|
||||
} else {
|
||||
match input.parse::<u64>() {
|
||||
Ok(num) => println!("{}", to_dollcode(num)),
|
||||
Err(_) => println!("input must be a positive integer :c"),
|
||||
}
|
||||
}
|
||||
} 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);
|
||||
} else {
|
||||
println!("{}", from_dollcode(&input));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue