use super::error::SourceLocation;
use crate::CodecError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
pub location: SourceLocation,
pub kind: ParseErrorKind,
pub operation: &'static str,
}
impl ParseError {
#[must_use]
pub fn during(mut self, operation: &'static str) -> Self {
self.operation = operation;
self
}
}
impl From<ParseError> for CodecError {
fn from(error: ParseError) -> Self {
match error.kind {
ParseErrorKind::UnexpectedEof { .. } => {
CodecError::truncated(error.location, error.operation)
}
ParseErrorKind::InvalidValue => CodecError::Malformed(format!(
"invalid value during {} at space {} offset {}",
error.operation,
error.location.space.index(),
error.location.offset
)),
ParseErrorKind::InvalidFraming => CodecError::Malformed(format!(
"invalid framing during {} at space {} offset {}",
error.operation,
error.location.space.index(),
error.location.offset
)),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParseErrorKind {
UnexpectedEof {
needed: u64,
remaining: u64,
},
InvalidValue,
InvalidFraming,
}