party_run/
party_command.rs

1//! Core party command.
2use core::fmt;
3
4use crate::parser::toml_command::Task;
5
6/// Struct holding a structured PartyCommand containing a command,
7/// its arguments as an array and wether it can be parallelised.
8pub struct PartyCommand {
9    /// Command to run
10    pub command: String,
11
12    /// Command arguments
13    pub args: Vec<String>,
14
15    /// Signals if command can be paralelised
16    pub is_parallel: bool,
17}
18
19impl PartyCommand {
20    /// Create a new PartyCommand
21    pub fn new(command: String, args: Vec<String>, is_parallel: bool) -> Self {
22        Self {
23            command,
24            args,
25            is_parallel,
26        }
27    }
28}
29
30impl From<Task> for PartyCommand {
31    fn from(mut task: Task) -> Self {
32        assert!(!task.command.is_empty());
33
34        let command = task.command.remove(0);
35        let args = task.command;
36
37        // A task is not parallel by default
38        let is_parallel = task.parallel.unwrap_or(false);
39
40        Self {
41            command,
42            args,
43            is_parallel,
44        }
45    }
46}
47
48impl fmt::Display for PartyCommand {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        let mut out = self.command.clone();
51        for arg in &self.args {
52            out += " ";
53            out += arg;
54        }
55
56        write!(f, "{}", out)
57    }
58}
59
60/// Create the default party commands.
61/// By default party-run runs the following commands sequentially:
62/// - cargo fmt
63/// - cargo clippy -- -Dwarnings
64/// - cargo test
65pub fn make_default_commands() -> Vec<PartyCommand> {
66    let cargo_fmt = PartyCommand::new("cargo".into(), vec!["fmt".into()], false);
67
68    let cargo_clippy = PartyCommand::new(
69        "cargo".into(),
70        vec!["clippy".into(), "--".into(), "-Dwarnings".into()],
71        false,
72    );
73
74    let cargo_test = PartyCommand::new("cargo".into(), vec!["test".into()], false);
75
76    vec![cargo_fmt, cargo_clippy, cargo_test]
77}
78
79/// Convert a vector of TOML tasks into structured Party commands
80pub fn convert_toml_tasks(tasks: Vec<Task>) -> Vec<PartyCommand> {
81    tasks.into_iter().map(PartyCommand::from).collect()
82}
83
84#[cfg(test)]
85mod test {
86    use crate::party_command::PartyCommand;
87
88    #[test]
89    fn test_display() {
90        // GIVEN
91        let cmd = PartyCommand::new(
92            "cargo".into(),
93            vec!["clippy".into(), "--".into(), "-Dwarnings".into()],
94            false,
95        );
96
97        // WHEN
98        let cmd_string = format!("{}", cmd);
99
100        // THEN
101        assert_eq!(cmd_string, "cargo clippy -- -Dwarnings");
102    }
103}