+99
upload/src/config.rs
+99
upload/src/config.rs
···
···
1
+
pub enum Id {
2
+
Handle(String),
3
+
Did(String),
4
+
}
5
+
6
+
pub struct Config {
7
+
pub user: Id,
8
+
pub pword: String,
9
+
pub dir: std::path::PathBuf,
10
+
}
11
+
12
+
pub enum ConfigErr {
13
+
TooManyDirectories,
14
+
NotEnoughArgs(String),
15
+
UnknownArg(String),
16
+
NotADirectory,
17
+
}
18
+
19
+
pub fn extract() -> Result<Config, ConfigErr> {
20
+
let mut user: Option<String> = None;
21
+
let mut pword: Option<String> = None;
22
+
let mut dir: Option<String> = None;
23
+
24
+
let mut args = std::env::args().into_iter();
25
+
// skip first arg
26
+
args.next();
27
+
while let Some(arg) = args.next() {
28
+
match arg.as_str() {
29
+
"--user" => user = args.next(),
30
+
"--password" => pword = args.next(),
31
+
_ => {
32
+
if arg.starts_with("--") {
33
+
return Err(ConfigErr::UnknownArg(arg));
34
+
}
35
+
if dir.is_some() {
36
+
return Err(ConfigErr::TooManyDirectories);
37
+
}
38
+
dir = Some(arg);
39
+
}
40
+
}
41
+
}
42
+
43
+
if let Some(user) = user.clone()
44
+
&& let Some(pword) = pword.clone()
45
+
&& let Some(dir) = dir.clone()
46
+
{
47
+
// we do not validate that the did or handle is valid rn
48
+
// but if it contains a : then its definitely a did
49
+
let user = if user.contains(":") {
50
+
Id::Did(user)
51
+
} else {
52
+
Id::Handle(user)
53
+
};
54
+
55
+
let dir = std::path::Path::new(&dir);
56
+
if !dir.is_dir() {
57
+
return Err(ConfigErr::NotADirectory);
58
+
};
59
+
60
+
return Ok(Config {
61
+
user,
62
+
pword,
63
+
dir: dir.to_owned(),
64
+
});
65
+
}
66
+
67
+
return Err(ConfigErr::NotEnoughArgs(String::from(
68
+
// xxx
69
+
if user.is_none() && pword.is_none() && dir.is_none() {
70
+
"--user, --password, and <dir>"
71
+
}
72
+
// yxx
73
+
else if user.is_some() && pword.is_none() && dir.is_none() {
74
+
"--password and <dir>"
75
+
}
76
+
// xyx
77
+
else if user.is_none() && pword.is_some() && dir.is_none() {
78
+
"--user and <dir>"
79
+
}
80
+
// yyx
81
+
else if user.is_some() && pword.is_some() && dir.is_none() {
82
+
"<dir>"
83
+
}
84
+
// xxy
85
+
else if user.is_none() && pword.is_none() && dir.is_some() {
86
+
"--user and --password"
87
+
}
88
+
// yxy
89
+
else if user.is_some() && pword.is_none() && dir.is_some() {
90
+
"--password"
91
+
}
92
+
// xyy
93
+
else if user.is_none() && pword.is_some() && dir.is_some() {
94
+
"--user"
95
+
} else {
96
+
"Invalid State, as all values are provided here"
97
+
},
98
+
)));
99
+
}
+15
-2
upload/src/main.rs
+15
-2
upload/src/main.rs
···
1
+
mod config;
2
+
3
+
fn main() -> Result<(), ()> {
4
+
let config = config::extract();
5
+
if let Err(err) = config {
6
+
match err {
7
+
config::ConfigErr::TooManyDirectories => println!("Too many directories"),
8
+
config::ConfigErr::NotEnoughArgs(args) => println!("Missing {}", args),
9
+
config::ConfigErr::UnknownArg(arg) => println!("Unknown arg {}", arg),
10
+
config::ConfigErr::NotADirectory => println!("Not a directory"),
11
+
}
12
+
return Err(());
13
+
}
14
+
15
+
Ok(())
16
}