std_ext/
command.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
pub trait CommandExt {
    fn run(&mut self) -> std::io::Result<()>;
    fn check_output(&mut self) -> std::io::Result<std::process::Output>;
}

pub trait OutputExt {
    fn stdout(&self) -> &str;
    fn stderr(&self) -> &str;
    fn success(&self) -> bool;
}

impl CommandExt for std::process::Command {
    fn run(&mut self) -> std::io::Result<()> {
        let status = self.status()?;
        if !status.success() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!("Command exited with code: {}", status.code().unwrap()),
            ));
        }
        Ok(())
    }

    fn check_output(&mut self) -> std::io::Result<std::process::Output> {
        let output = self.output()?;
        if output.status.success() {
            Ok(output)
        } else {
            Err(std::io::Error::new(
                std::io::ErrorKind::Other,
                format!(
                    "Command exited with code: {}",
                    output.status.code().unwrap()
                ),
            ))
        }
    }
}

impl OutputExt for std::process::Output {
    fn stdout(&self) -> &str {
        std::str::from_utf8(&self.stdout).expect("Failed to convert stdout to str")
    }

    fn stderr(&self) -> &str {
        std::str::from_utf8(&self.stderr).expect("Failed to convert stderr to str")
    }
    fn success(&self) -> bool {
        self.status.success()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_output() -> std::io::Result<()> {
        let output = std::process::Command::new("echo").arg("hello").output()?;
        let s = output.stdout();
        assert_eq!(s, "hello\n");
        Ok(())
    }

    #[test]
    fn test_run() -> std::io::Result<()> {
        std::process::Command::new("echo").arg("hello").run()?;
        Ok(())
    }
}