-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathctc.rs
43 lines (37 loc) · 882 Bytes
/
ctc.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn dtc(arg: String, digits: Vec<u32>, piped: bool) {
if piped {
print!("{arg}: ");
}
for digit in digits {
print!("{} ", char::from_u32(digit).unwrap());
}
print!("\n");
}
fn ctd(word: String, piped: bool) {
let chars = word.chars();
if piped {
chars.for_each(|c|{ println!("{}", c as u32) })
} else {
chars.for_each(|c|{ println!("{c}: {}", c as u32) })
}
}
//TODO: read from stdin
fn main() -> Result<(), String> {
let mut args: Vec<String> = std::env::args().skip(1).collect();
// piped
let piped = args.first() == Some(&"-1".to_owned());
// remove "-1" arg
if piped {args.drain(0..1);}
for arg in args {
if arg.chars().all(|c|c.is_digit(10)||" ,".contains(c)) {
let digits = arg
.split_terminator(&[' ', ','])
.map(|dgt| dgt.parse().unwrap() )
.collect();
dtc(arg, digits, piped);
} else {
ctd(arg, piped);
}
}
Ok(())
}