1use core::fmt::Display;
2
3#[derive(Debug)]
4pub enum Error {
5 InvalidMarker(u8),
6 InvalidChar(u32),
7 InvalidUtf8(core::str::Utf8Error),
8 BufferTooSmall,
9 CannotBorrow,
10 ArrayLengthMismatch {
11 expected: usize,
12 actual: usize,
13 },
14 MapLengthMismatch {
15 expected: usize,
16 actual: usize,
17 },
18 UnknownKey(alloc::string::String),
19 KeyNotFound(alloc::string::String),
20 KeyDuplicated(alloc::string::String),
21 InvalidTimestamp,
22 DepthLimitExceeded {
23 max: usize,
24 },
25 #[cfg(feature = "std")]
26 IoError(std::io::Error),
27}
28
29pub type Result<T> = core::result::Result<T, Error>;
30
31impl Display for Error {
32 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
33 match self {
34 Error::InvalidMarker(byte) => write!(f, "Invalid marker: {}", byte),
35 Error::InvalidChar(code) => write!(f, "Invalid char code: {}", code),
36 Error::BufferTooSmall => write!(f, "Buffer too small"),
37 Error::InvalidUtf8(err) => write!(f, "Invalid UTF-8: {}", err),
38 Error::CannotBorrow => write!(f, "Cannot borrow data from original buffer"),
39 Error::ArrayLengthMismatch { expected, actual } => {
40 write!(
41 f,
42 "Array length mismatch: expected {}, actual {}",
43 expected, actual
44 )
45 }
46 Error::MapLengthMismatch { expected, actual } => {
47 write!(
48 f,
49 "Map length mismatch: expected {}, actual {}",
50 expected, actual
51 )
52 }
53 Error::UnknownKey(key) => {
54 write!(f, "Unknown key '{}'", key)
55 }
56 Error::KeyNotFound(key) => {
57 write!(f, "Key '{}' not found", key)
58 }
59 Error::KeyDuplicated(key) => {
60 write!(f, "Key '{}' is duplicated", key)
61 }
62 Error::InvalidTimestamp => write!(f, "Invalid timestamp value"),
63 Error::DepthLimitExceeded { max } => {
64 write!(f, "Maximum deserialization depth exceeded (max: {})", max)
65 }
66 #[cfg(feature = "std")]
67 Error::IoError(err) => err.fmt(f),
68 }
69 }
70}
71
72impl core::error::Error for Error {}