party_run/
party_command.rs1use core::fmt;
3
4use crate::parser::toml_command::Task;
5
6pub struct PartyCommand {
9 pub command: String,
11
12 pub args: Vec<String>,
14
15 pub is_parallel: bool,
17}
18
19impl PartyCommand {
20 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 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
60pub 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
79pub 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 let cmd = PartyCommand::new(
92 "cargo".into(),
93 vec!["clippy".into(), "--".into(), "-Dwarnings".into()],
94 false,
95 );
96
97 let cmd_string = format!("{}", cmd);
99
100 assert_eq!(cmd_string, "cargo clippy -- -Dwarnings");
102 }
103}