-
Notifications
You must be signed in to change notification settings - Fork 3
/
base32.rs
72 lines (62 loc) · 1.6 KB
/
base32.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std::ffi::OsString;
use uutils_args::{
positional::{Opt, Unpack},
Arguments, Options,
};
#[derive(Clone, Arguments)]
enum Arg {
#[arg("-d", "--decode")]
Decode,
#[arg("-i", "--ignore-garbage")]
IgnoreGarbage,
#[arg("-w COLS", "--wrap=COLS")]
Wrap(usize),
}
struct Settings {
decode: bool,
ignore_garbage: bool,
wrap: Option<usize>,
}
impl Default for Settings {
fn default() -> Self {
Self {
wrap: Some(76),
decode: Default::default(),
ignore_garbage: Default::default(),
}
}
}
impl Options<Arg> for Settings {
fn apply(&mut self, arg: Arg) {
match arg {
Arg::Decode => self.decode = true,
Arg::IgnoreGarbage => self.ignore_garbage = true,
Arg::Wrap(0) => self.wrap = None,
Arg::Wrap(x) => self.wrap = Some(x),
}
}
}
fn parse<I>(args: I) -> Result<(Settings, Option<OsString>), uutils_args::Error>
where
I: IntoIterator,
I::Item: Into<OsString>,
{
let (s, ops) = Settings::default().parse(args)?;
let file = Opt("FILE").unpack(ops)?;
Ok((s, file))
}
#[test]
fn wrap() {
assert_eq!(parse(["base32"]).unwrap().0.wrap, Some(76));
assert_eq!(parse(["base32", "-w0"]).unwrap().0.wrap, None);
assert_eq!(parse(["base32", "-w100"]).unwrap().0.wrap, Some(100));
assert_eq!(parse(["base32", "--wrap=100"]).unwrap().0.wrap, Some(100));
}
#[test]
fn file() {
assert_eq!(parse(["base32"]).unwrap().1, None);
assert_eq!(
parse(["base32", "file"]).unwrap().1,
Some(OsString::from("file"))
);
}