use crate::motor::MotorKind;
use core::fmt;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Error {
EmptyProgram,
MotorKindMismatch {
expected: MotorKind,
got: MotorKind,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyProgram => f.write_str("program has no instructions"),
Self::MotorKindMismatch { expected, got } => {
write!(
f,
"motor kind mismatch: program is {expected}, runner is {got}"
)
}
}
}
}
impl core::error::Error for Error {}
impl fmt::Display for MotorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Erm => "ERM",
Self::Lra => "LRA",
})
}
}
#[cfg(test)]
#[allow(clippy::std_instead_of_alloc)]
mod tests {
use super::*;
use std::string::ToString;
#[test]
fn errors_render_readable_messages() {
assert_eq!(
Error::EmptyProgram.to_string(),
"program has no instructions"
);
assert_eq!(
Error::MotorKindMismatch {
expected: MotorKind::Erm,
got: MotorKind::Lra,
}
.to_string(),
"motor kind mismatch: program is ERM, runner is LRA"
);
}
}