use std::fmt::Display; use thiserror::Error; use crate::serde::T; pub type Result = std::result::Result; // This is a bare-bones implementation. A real library would provide additional // information in its error type, for example the line and column at which the // error occurred, the byte offset into the input, or the current key being // processed. #[derive(Debug, Error)] pub enum Error { #[error("{0}")] Message(String), #[error(transparent)] Io(#[from] std::io::Error), #[error("expected {expected:?}, found {found}")] Expected { expected: T, found: u8 }, #[error(transparent)] FromUtf8Error(#[from] std::string::FromUtf8Error), #[error(transparent)] Utf8Error(#[from] std::str::Utf8Error), #[error("a length is required for most things")] LenLess, #[error("char {0} was not in the valid range of chars")] NotChar(u32), #[error("out of bounds index")] OOB, #[error("{0} is not a tag")] NotTag(u8), #[error("out of range")] Overflow, #[error( "the length provided in the data stream {0} is different from the hint serde provided {1}" )] IncoherentLen(usize, usize), #[error(transparent)] TryFromIntError(#[from] std::num::TryFromIntError), } impl serde::ser::Error for Error { fn custom(msg: T) -> Self where T: Display, { Self::Message(msg.to_string()) } } impl serde::de::Error for Error { fn custom(msg: T) -> Self where T: Display, { Self::Message(msg.to_string()) } }