1use super::*;
2
3pub enum Task
4{
5 Command(Command),
6 Fn(TaskFn)
7}
8
9impl From<Command> for Task
10{
11 fn from(c: Command) -> Self { Task::Command(c) }
12}
13
14impl From<TaskFn> for Task
15{
16 fn from(f: TaskFn) -> Self { Task::Fn(f) }
17}
18
19impl Task
20{
21 pub fn invoke(&mut self, task_name: &'static str, task_index: usize) -> Result<String, BuildPrettyError>
22 {
23 match self
24 {
25 Task::Command(c) => invoke_command(c, task_name, task_index),
26 Task::Fn(f) => invoke_fn(f, task_name, task_index)
27 }
28 }
29}
30
31fn invoke_command(c: &mut Command, task_name: &'static str, task_index: usize) -> Result<String, BuildPrettyError>
32{
33 let o = c.output().map_err(|error_source| {
34 BuildPrettyError::CommandWasFailed {
35 task_name,
36 task_index,
37 output: None,
38 error_source: Some(Box::new(error_source))
39 }
40 })?;
41
42 let stdout = String::from_utf8(o.clone().stdout).map_err(|error_source| {
43 BuildPrettyError::CommandWasFailed {
44 task_name,
45 task_index,
46 output: Some(o.clone()),
47 error_source: Some(Box::new(error_source))
48 }
49 })?;
50
51 match o.status.success()
62 {
63 true => Ok(stdout),
64 false =>
65 {
66 Err(BuildPrettyError::CommandWasFailed {
67 task_name,
68 task_index,
69 output: Some(o),
70 error_source: None
71 })
72 },
73 }
74}
75
76fn invoke_fn(f: &TaskFn, task_name: &'static str, task_index: usize) -> Result<String, BuildPrettyError>
77{
78 let mut s = String::new();
79 f(&mut s).map_err(|error_source| {
80 BuildPrettyError::FnWasFailed {
81 task_name,
82 task_index,
83 output: s.clone(),
84 error_source: Some(error_source)
85 }
86 })?;
87 Ok(s)
88}