pub trait DecodeNumber: Sized {
fn decode_number(bytes: &[u8]) -> Result<(Self, usize), DecodeError>;
}
pub trait DecodeNumberInclusive: Sized {
fn decode_usize_inclusive(bytes: &[u8]) -> Result<(Self, usize), DecodeError>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DecodeError {
UnexpectedEof { expected: usize, got: usize },
InvalidSizeMarker(u8),
InvalidTypeMarker(u8),
ValueOutOfRange,
InvalidInclusive,
InvalidTensor(String),
InvalidSpirix { f: u8, e: u8 },
Other(String),
}
impl std::fmt::Display for DecodeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DecodeError::UnexpectedEof { expected, got } => {
write!(f, "Unexpected end of file: expected {} bytes, got {}", expected, got)
}
DecodeError::InvalidSizeMarker(m) => {
write!(f, "Invalid size marker: {}", m)
}
DecodeError::InvalidTypeMarker(m) => {
write!(f, "Invalid type marker: {}", *m as char)
}
DecodeError::ValueOutOfRange => {
write!(f, "Value out of range for target type")
}
DecodeError::InvalidInclusive => {
write!(f, "Invalid inclusive encoding")
}
DecodeError::InvalidTensor(msg) => {
write!(f, "Invalid tensor: {}", msg)
}
DecodeError::InvalidSpirix { f: frac, e: exp } => {
write!(f, "Invalid Spirix type: F{}E{}", frac, exp)
}
DecodeError::Other(msg) => {
write!(f, "{}", msg)
}
}
}
}
impl std::error::Error for DecodeError {}