use core::fmt::{Display, Formatter};
pub type Result<T> = ::core::result::Result<T, Error>;
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
RequestBufferFull,
ResponseBadHeader,
ResponseTooLarge,
ResponseBadLen,
ResponseUnknownCmd,
ResponseBadAck,
ResponseBadCrc,
SerializeBadEnum,
DeserializeUnexpectedEnd,
DeserializeBadBool,
DeserializeBadOption,
AccumulateBufferFull,
WontImplement,
NotYetImplemented,
SerdeSerCustom,
SerdeDeCustom,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> core::fmt::Result {
use Error::*;
write!(
f,
"{}",
match self {
RequestBufferFull => "The request buffer is full",
ResponseBadHeader => "Response header mismatch",
ResponseTooLarge => "Response length is more than the buffer size",
ResponseBadLen => "Response length is less than the minimum proper response length",
ResponseUnknownCmd => "Unknown response command",
ResponseBadAck => "Bad Ack response",
ResponseBadCrc => "Response CRC mismatch",
SerializeBadEnum => "Found an enum discriminant that was > u16::max_value()",
DeserializeUnexpectedEnd => "Hit the end of buffer, expected more data",
DeserializeBadBool => "Found a bool that wasn't 0 or 1",
DeserializeBadOption => "Found an Option discriminant that wasn't 0 or 1",
AccumulateBufferFull => "The accumulator buffer is full",
WontImplement => "dguscard will never implement this",
NotYetImplemented => "dguscard may support this",
SerdeSerCustom => "Serde Serialization Error",
SerdeDeCustom => "Serde Deserialization Error",
}
)
}
}
impl core::error::Error for Error {}
impl serde::ser::Error for Error {
fn custom<T>(_msg: T) -> Self
where
T: Display,
{
Error::SerdeSerCustom
}
}
impl serde::de::Error for Error {
fn custom<T>(_msg: T) -> Self
where
T: Display,
{
Error::SerdeDeCustom
}
}