use crate::{MilliAmps, MilliVolts};
pub trait Error: core::fmt::Debug {
fn kind(&self) -> ErrorKind;
}
impl Error for core::convert::Infallible {
#[inline]
fn kind(&self) -> ErrorKind {
match *self {}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum ErrorKind {
CommError,
Other,
}
impl Error for ErrorKind {
#[inline]
fn kind(&self) -> ErrorKind {
*self
}
}
impl core::fmt::Display for ErrorKind {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::CommError => write!(f, "Error communicating with charger"),
Self::Other => write!(
f,
"A different error occurred. The original error may contain more information"
),
}
}
}
pub trait ErrorType {
type Error: Error;
}
impl<T: ErrorType + ?Sized> ErrorType for &mut T {
type Error = T::Error;
}
pub trait Charger: ErrorType {
fn charging_current(&mut self, current: MilliAmps) -> Result<MilliAmps, Self::Error>;
fn charging_voltage(&mut self, voltage: MilliVolts) -> Result<MilliVolts, Self::Error>;
}
impl<T: Charger + ?Sized> Charger for &mut T {
#[inline]
fn charging_current(&mut self, current: MilliAmps) -> Result<MilliAmps, Self::Error> {
T::charging_current(self, current)
}
#[inline]
fn charging_voltage(&mut self, voltage: MilliVolts) -> Result<MilliVolts, Self::Error> {
T::charging_voltage(self, voltage)
}
}