atcoder_auto_tester/
command.rs

1use std::fmt;
2use std::process;
3
4use crate::error::ErrorHandleable;
5use crate::message::Messageable;
6
7#[derive(Debug)]
8pub struct Command {
9    program: String,
10    args: Vec<String>,
11}
12
13impl Command {
14    pub fn new(program: &str) -> Self {
15        Command {
16            program: String::from(program),
17            args: vec![],
18        }
19    }
20
21    pub fn arg(mut self, arg: &str) -> Self {
22        self.args.push(String::from(arg));
23        self
24    }
25
26    pub fn run(&self) -> process::ExitStatus {
27        process::Command::new(&self.program)
28            .args(&self.args)
29            .spawn()
30            .handle_error("Failed to execute child process")
31            .wait()
32            .handle_error("Failed to wait child process")
33    }
34
35    pub fn show(self) -> Self {
36        format!("{}", self).command_message().show();
37        self
38    }
39}
40
41impl fmt::Display for Command {
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        write!(
44            f,
45            "{}",
46            self.args
47                .iter()
48                .map(|arg| format!("{:?}", arg))
49                .fold(format!("{:?}", self.program), |acc, arg| acc + " " + &arg)
50        )
51    }
52}