use std::{
error::Error,
fmt::{self, Display, Formatter},
};
pub use crate::wire::WireLimit as DecodeLimit;
#[derive(Debug)]
#[non_exhaustive]
pub enum DecodeError {
MalformedJson(serde_json::Error),
RootNotObject,
LimitExceeded {
limit: DecodeLimit,
maximum: usize,
actual: usize,
},
}
impl Display for DecodeError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::MalformedJson(error) => write!(formatter, "malformed diagnostic JSON: {error}"),
Self::RootNotObject => formatter.write_str("diagnostic JSON root must be an object"),
Self::LimitExceeded {
limit,
maximum,
actual,
} => write!(
formatter,
"diagnostic {limit:?} limit exceeded: maximum {maximum}, actual {actual}"
),
}
}
}
impl Error for DecodeError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::MalformedJson(error) => Some(error),
Self::RootNotObject | Self::LimitExceeded { .. } => None,
}
}
}