use rtc_hal::datetime::DateTimeError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error<I2cError>
where
I2cError: core::fmt::Debug,
{
I2c(I2cError),
InvalidAddress,
UnsupportedSqwFrequency,
DateTime(DateTimeError),
NvramOutOfBounds,
}
impl<I2cError> From<I2cError> for Error<I2cError>
where
I2cError: core::fmt::Debug,
{
fn from(value: I2cError) -> Self {
Error::I2c(value)
}
}
impl<I2cError> rtc_hal::error::Error for Error<I2cError>
where
I2cError: core::fmt::Debug,
{
fn kind(&self) -> rtc_hal::error::ErrorKind {
match self {
Error::I2c(_) => rtc_hal::error::ErrorKind::Bus,
Error::InvalidAddress => rtc_hal::error::ErrorKind::InvalidAddress,
Error::DateTime(_) => rtc_hal::error::ErrorKind::InvalidDateTime,
Error::NvramOutOfBounds => rtc_hal::error::ErrorKind::NvramOutOfBounds,
Error::UnsupportedSqwFrequency => rtc_hal::error::ErrorKind::UnsupportedSqwFrequency,
}
}
}
#[cfg(feature = "defmt")]
impl<I2cError> defmt::Format for Error<I2cError>
where
I2cError: core::fmt::Debug,
{
fn format(&self, f: defmt::Formatter) {
match self {
Error::I2c(e) => {
defmt::write!(f, "I2C communication error: {:?}", defmt::Debug2Format(e))
}
Error::InvalidAddress => defmt::write!(f, "Invalid NVRAM address"),
Error::DateTime(_) => defmt::write!(f, "Invalid date/time values"),
Error::NvramOutOfBounds => defmt::write!(f, "NVRAM operation out of bounds"),
Error::UnsupportedSqwFrequency => defmt::write!(f, "Unsupported Square Wave Frequency"),
}
}
}