use core::fmt;
use crate::{parser::toml_command::Task, util::OptionEnv};
#[derive(Debug)]
pub struct PartyCommand {
pub command: String,
pub env: OptionEnv,
pub is_parallel: bool,
}
impl PartyCommand {
pub fn new(command: String, is_parallel: bool, env: OptionEnv) -> Self {
Self {
command,
is_parallel,
env,
}
}
}
impl From<Task> for PartyCommand {
fn from(task: Task) -> Self {
assert!(!task.command.is_empty());
let is_parallel = task.parallel.unwrap_or(false);
Self {
command: task.command,
env: task.env,
is_parallel,
}
}
}
impl fmt::Display for PartyCommand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.command)
}
}
pub fn make_default_commands() -> Vec<PartyCommand> {
let cargo_fmt = PartyCommand::new("cargo fmt".to_string(), false, None);
let cargo_clippy = PartyCommand::new("cargo clippy -- -Dwarnings".to_string(), false, None);
let cargo_test = PartyCommand::new("cargo test".to_string(), false, None);
vec![cargo_fmt, cargo_clippy, cargo_test]
}
pub fn convert_toml_tasks(tasks: Vec<Task>) -> Vec<PartyCommand> {
tasks.into_iter().map(PartyCommand::from).collect()
}
#[cfg(test)]
mod test {
use crate::party_command::PartyCommand;
#[test]
fn test_display() {
let cmd = PartyCommand::new("cargo clippy -- -Dwarnings".to_string(), false, None);
let cmd_string = format!("{}", cmd);
assert_eq!(cmd_string, "cargo clippy -- -Dwarnings");
}
}