pamoja_actuators/error.rs
1//! The error the actuator drivers share.
2
3/// What can go wrong driving a part over a bus or a set of pins.
4///
5/// A driver returns this from every transfer sequence. It converts into the core
6/// [`Error`](pamoja_core::Error) so a driver used as an
7/// [`Actuator`](pamoja_core::Actuator) reports the way every other device does: a
8/// bus or pin fault becomes [`Error::Io`](pamoja_core::Error::Io), and a command the
9/// part cannot take becomes [`Error::Codec`](pamoja_core::Error::Codec).
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum DriverError<E> {
12 /// The bus or pin underneath the driver failed: a missing acknowledge, a bus
13 /// fault, a pin that could not be driven.
14 Bus(E),
15 /// The command is outside what the part accepts, such as a channel the part does
16 /// not have or a frequency its prescaler cannot reach.
17 Command(&'static str),
18}
19
20impl<E: core::fmt::Debug> core::fmt::Display for DriverError<E> {
21 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22 match self {
23 DriverError::Bus(error) => write!(f, "bus error: {error:?}"),
24 DriverError::Command(reason) => write!(f, "command refused: {reason}"),
25 }
26 }
27}
28
29impl<E: core::fmt::Debug> core::error::Error for DriverError<E> {}
30
31impl<E: core::fmt::Debug> From<DriverError<E>> for pamoja_core::Error {
32 fn from(error: DriverError<E>) -> Self {
33 match error {
34 DriverError::Bus(bus) => pamoja_core::Error::Io(alloc::format!("bus error: {bus:?}")),
35 DriverError::Command(reason) => {
36 pamoja_core::Error::Codec(alloc::format!("command refused: {reason}"))
37 }
38 }
39 }
40}