use std::{
fmt,
io,
};
#[derive(Debug)]
pub enum FormatError {
MalformedDocument {
format: &'static str,
detail: String,
},
PayloadTooLarge,
Read {
detail: String,
},
Encoding {
format: &'static str,
detail: String,
},
}
impl fmt::Display for FormatError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MalformedDocument { format, detail } => {
write!(f, "malformed {format} document: {detail}")
}
Self::PayloadTooLarge => f.write_str("payload exceeded the configured size limit"),
Self::Read { detail } => write!(f, "body read failed: {detail}"),
Self::Encoding { format, detail } => {
write!(f, "{format} encoding failed: {detail}")
}
}
}
}
impl std::error::Error for FormatError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PayloadLimitExceeded;
impl fmt::Display for PayloadLimitExceeded {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("payload exceeded the configured size limit")
}
}
impl std::error::Error for PayloadLimitExceeded {}
impl FormatError {
#[must_use]
pub fn from_read_error(error: &io::Error) -> Self {
if matches!(error.get_ref(), Some(source) if source.is::<PayloadLimitExceeded>()) {
Self::PayloadTooLarge
} else {
Self::Read {
detail: error.to_string(),
}
}
}
}