use clap::{arg, Parser};
mod metrics_command;
mod object_command;
use crate::simulation::Simulation;
pub use metrics_command::MetricsCommand;
pub use object_command::ObjectCommand;
pub trait SimulationCommand {
fn execute(&self, simulation: &mut Simulation) -> Option<String>;
}
#[derive(Parser, Debug)]
#[command(name = "pause")]
pub struct PauseCommand {}
impl SimulationCommand for PauseCommand {
fn execute(&self, simulation: &mut Simulation) -> Option<String> {
simulation.is_paused = !simulation.is_paused;
let msg = if simulation.is_paused {
"Simulation paused"
} else {
"Simulation resumed"
};
Some(msg.to_string())
}
}
#[derive(Parser, Debug)]
#[command(name = "speedup")]
pub struct SetSpeedupCommand {
#[arg(default_value = "1.0")]
pub speedup_factor: f64,
}
impl SimulationCommand for SetSpeedupCommand {
fn execute(&self, simulation: &mut Simulation) -> Option<String> {
simulation.speedup_factor = self.speedup_factor;
Some(format!("Speedup factor set to {}", self.speedup_factor))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pause_command() {
let mut simulation = Simulation::new(Default::default());
let pause_command = PauseCommand {};
assert_eq!(simulation.is_paused, false);
let result = pause_command.execute(&mut simulation);
assert_eq!(simulation.is_paused, true);
assert_eq!(result, Some("Simulation paused".to_string()));
let result = pause_command.execute(&mut simulation);
assert_eq!(simulation.is_paused, false);
assert_eq!(result, Some("Simulation resumed".to_string()));
}
#[test]
fn test_set_speedup_command() {
let mut simulation = Simulation::new(Default::default());
let set_speedup_command_2 = SetSpeedupCommand {
speedup_factor: 2.0,
};
assert_eq!(simulation.speedup_factor, 1.0);
let result = set_speedup_command_2.execute(&mut simulation);
assert_eq!(simulation.speedup_factor, 2.0);
assert_eq!(result, Some("Speedup factor set to 2".to_string()));
let set_speedup_command_default = SetSpeedupCommand {
speedup_factor: 1.0,
};
let result = set_speedup_command_default.execute(&mut simulation);
assert_eq!(simulation.speedup_factor, 1.0);
assert_eq!(result, Some("Speedup factor set to 1".to_string()));
}
}