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
|
pub(crate) struct Task {
pub name: &'static str,
pub exec: fn(&[&str]) -> Result<String, String>,
}
pub(crate) const TASKS: &[Task] = &[
Task {
name: "nop",
exec: no_op,
},
Task {
name: "exec",
exec: exec,
},
];
fn no_op(_: &[&str]) -> Result<String, String> {
Ok("".to_string())
}
fn exec(cmds: &[&str]) -> Result<String, String> {
use std::process::Command;
// TODO: fmt and add exit code, and add support for both stdout and stderr, and support for no output
match Command::new(cmds[0]).args(&cmds[1..]).output() {
Ok(o) => {
if o.status.success() {
Ok(String::from_utf8(o.stdout)
.unwrap_or("Failed decoding vec<u8> -> String".to_string())
.to_string())
} else {
Err(String::from_utf8(o.stderr)
.unwrap_or("Failed decoding vec<u8> -> String".to_string())
.to_string())
}
}
Err(e) => Err(e.to_string()),
}
}
pub(crate) enum IntoTaskErr {
NotFound,
}
pub(crate) trait IntoTask {
fn into_task<'a>(self) -> Result<&'a Task, IntoTaskErr>;
}
impl IntoTask for String {
fn into_task<'a>(self) -> Result<&'a Task, IntoTaskErr> {
for task in TASKS {
if task.name == self {
return Ok(task);
}
}
Err(IntoTaskErr::NotFound)
}
}
impl IntoTask for &str {
fn into_task<'a>(self) -> Result<&'a Task, IntoTaskErr> {
for task in TASKS {
if task.name == self {
return Ok(task);
}
}
Err(IntoTaskErr::NotFound)
}
}
|