use core::{array::TryFromSliceError, fmt::Display};
pub type Result<T> = core::result::Result<T, TypedStreamError>;
#[derive(Debug)]
pub enum TypedStreamError {
InvalidObject,
OutOfBounds(usize, usize),
SliceError(TryFromSliceError),
StringParseError(core::str::Utf8Error),
InvalidHeader,
InvalidPointer(u8),
InvalidArray(usize),
EmptyString,
}
impl Display for TypedStreamError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
TypedStreamError::InvalidObject => {
write!(f, "Invalid object encountered in typedstream!")
}
TypedStreamError::OutOfBounds(n, len) => write!(
f,
"Out of bounds access: tried to access byte {n} in a stream of length {len}"
),
TypedStreamError::SliceError(try_from_slice_error) => {
write!(f, "Slice conversion error: {try_from_slice_error}")
}
TypedStreamError::StringParseError(utf8_error) => {
write!(f, "String parsing error: {utf8_error}")
}
TypedStreamError::InvalidHeader => write!(f, "Invalid header in typedstream!"),
TypedStreamError::InvalidPointer(pointer) => {
write!(f, "Invalid pointer: {pointer:x}")
}
TypedStreamError::InvalidArray(offset) => {
write!(f, "Invalid array at index: {offset:x}")
}
TypedStreamError::EmptyString => write!(f, "Empty string encountered in typedstream"),
}
}
}
impl From<TryFromSliceError> for TypedStreamError {
fn from(error: TryFromSliceError) -> Self {
TypedStreamError::SliceError(error)
}
}
impl From<core::str::Utf8Error> for TypedStreamError {
fn from(error: core::str::Utf8Error) -> Self {
TypedStreamError::StringParseError(error)
}
}
impl core::error::Error for TypedStreamError {}