use std::io;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("serial port {port}: {source}")]
Serial {
port: String,
#[source]
source: serialport::Error,
},
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("invalid motor ID 0x{0:02X} (must be 0x01..=0xFE)")]
InvalidId(u8),
#[error("invalid frame length {0} (need 9 or 10 bytes)")]
InvalidFrameLen(usize),
#[error("invalid slew rate {0} (must be finite and > 0)")]
InvalidSlewRate(f32),
}
impl Error {
pub fn is_permission_denied(&self) -> bool {
match self {
Error::Serial { source, .. } => matches!(
source.kind,
serialport::ErrorKind::Io(io::ErrorKind::PermissionDenied)
),
Error::Io(e) => e.kind() == io::ErrorKind::PermissionDenied,
_ => false,
}
}
}
pub type Result<T> = std::result::Result<T, Error>;