use super::tag::{Tag, TdfType};
use std::{error::Error, fmt::Display, str::Utf8Error};
#[derive(Debug)]
pub enum DecodeError {
MissingTag {
tag: Tag,
ty: TdfType,
},
InvalidTagType {
tag: Tag,
expected: TdfType,
actual: TdfType,
},
InvalidType {
expected: TdfType,
actual: TdfType,
},
UnknownType {
ty: u8,
},
UnexpectedEof {
cursor: usize,
wanted: usize,
remaining: usize,
},
InvalidUtf8Value(Utf8Error),
Other(&'static str),
}
pub type DecodeResult<T> = Result<T, DecodeError>;
impl Error for DecodeError {}
impl Display for DecodeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DecodeError::MissingTag { tag, ty } => {
write!(f, "Missing tag '{}' (type: {:?})", tag, ty)
}
DecodeError::InvalidTagType {
tag,
expected,
actual,
} => {
write!(
f,
"Invalid tag type for '{}' (expected: {:?}, got: {:?})",
tag, expected, actual
)
}
DecodeError::InvalidType { expected, actual } => {
write!(
f,
"Unexpected tag type (expected: {:?}, got: {:?})",
expected, actual
)
}
DecodeError::UnknownType { ty } => {
write!(f, "Unknown tag type: {}", ty)
}
DecodeError::UnexpectedEof {
cursor,
wanted,
remaining,
} => {
write!(
f,
"Unexpected end of file (cursor: {}, wanted: {}, remaining: {})",
cursor, wanted, remaining
)
}
DecodeError::InvalidUtf8Value(err) => err.fmt(f),
DecodeError::Other(err) => f.write_str(err),
}
}
}