use std::sync::Mutex;
use crate::error::MotorError;
use crate::motor::{Motor, MotorDriver};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MotorCommand {
SetSpeed {
motor: Motor,
speed: u16,
forward: bool,
},
Stop { motor: Motor },
}
pub struct MockMotorDriver {
commands: Mutex<Vec<MotorCommand>>,
}
impl MockMotorDriver {
#[must_use]
pub fn new() -> Self {
Self {
commands: Mutex::new(Vec::new()),
}
}
#[must_use]
pub fn commands(&self) -> Vec<MotorCommand> {
self.commands.lock().expect("mock mutex poisoned").clone()
}
#[must_use]
pub fn command_count(&self) -> usize {
self.commands.lock().expect("mock mutex poisoned").len()
}
pub fn clear(&self) {
self.commands.lock().expect("mock mutex poisoned").clear();
}
}
impl Default for MockMotorDriver {
fn default() -> Self {
Self::new()
}
}
impl MotorDriver for MockMotorDriver {
fn set_motor_speed(&self, motor: Motor, speed: u16, forward: bool) -> Result<(), MotorError> {
self.commands
.lock()
.expect("mock mutex poisoned")
.push(MotorCommand::SetSpeed {
motor,
speed,
forward,
});
Ok(())
}
fn stop_motor(&self, motor: Motor) -> Result<(), MotorError> {
self.commands
.lock()
.expect("mock mutex poisoned")
.push(MotorCommand::Stop { motor });
Ok(())
}
}