use crate::decode::{ErrorContext, ResourceLimit, SourceLocation};
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CodecError {
#[error("not the expected format: {0}")]
WrongFormat(String),
#[error("malformed container: {0}")]
Malformed(String),
#[error(
"truncated input during {} at space {} offset {}",
.context.operation, .location.space.index(), .location.offset
)]
Truncated {
location: SourceLocation,
context: ErrorContext,
},
#[error(
"resource limit on {:?}: {:?} (limit {}, used {}, requested {})",
.0.dimension, .0.reason, .0.limit, .0.used, .0.additional
)]
ResourceLimit(ResourceLimit),
#[error("not implemented yet: {0}")]
NotImplemented(String),
#[error("io error: {0}")]
Io(#[from] std::io::Error),
}
impl CodecError {
pub const fn truncated(location: SourceLocation, operation: &'static str) -> Self {
Self::Truncated {
location,
context: ErrorContext {
operation,
location: Some(location),
},
}
}
}