serde_brief/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! Crate errors.

use ::core::fmt::Display;

use crate::format::Type;

/// Error when (de-)serializing.
#[derive(Debug)]
pub enum Error {
	/// Expected more data but encountered the end of the input.
	UnexpectedEnd,
	/// Excess data appeared at the end of the input.
	ExcessData,
	/// Buffer was too small.
	BufferTooSmall,
	/// Allocation failure.
	Allocation,
	/// Usize overflow.
	UsizeOverflow,
	/// Configured size limit reached.
	LimitReached,

	/// Invalid data type designator encountered.
	InvalidType(u8),
	/// VarInt too large for the given expected type.
	VarIntTooLarge,
	/// Wrong data type encountered (found, expected).
	WrongType(Type, &'static [Type]),
	/// String is not exactly one character.
	NotOneChar,

	/// Formatting error. Happens serializing a `core::fmt::Display` value and could be due to an
	/// output writing failure.
	Format(::core::fmt::Error),
	/// Parsed string is not valid UTF-8.
	StringNotUtf8(::core::str::Utf8Error),
	/// IO error.
	#[cfg(feature = "std")]
	Io(::std::io::Error),

	/// **no-std + no-alloc**: Generic error message that can be created by data structures through
	/// the `ser::Error` and `de::Error` traits.
	Custom,
	/// **alloc**: Generic error message that can be created by data structures through the
	/// `ser::Error` and `de::Error` traits.
	#[cfg(feature = "alloc")]
	Message(::alloc::string::String),
}

impl Display for Error {
	fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
		match self {
			Error::UnexpectedEnd => {
				write!(f, "Expected more data but encountered the end of the input")
			}
			Error::ExcessData => write!(f, "Excess data appeared at the end of the input"),
			Error::BufferTooSmall => write!(f, "Output or scratch buffer was too small"),
			Error::Allocation => write!(f, "Allocator failed on allocating more space"),
			Error::UsizeOverflow => write!(f, "Tried using more bytes than usize allows for"),
			Error::LimitReached => write!(f, "Configured size limit reached"),

			Error::InvalidType(v) => {
				write!(f, "Invalid data type designator encountered: {v:#02X}")
			}
			Error::VarIntTooLarge => write!(f, "VarInt too large for the given expected type"),
			Error::WrongType(found, expected) => write!(
				f,
				"Wrong data type encountered. Found `{found:?}`, but expected one of `{expected:?}`"
			),
			Error::NotOneChar => write!(f, "String is not exactly one character"),

			Error::Format(err) => write!(f, "Value formatting error: {err:#}"),
			Error::StringNotUtf8(err) => write!(f, "String is not valid UTF-8: {err:#}"),
			#[cfg(feature = "std")]
			Error::Io(err) => write!(f, "IO error: {err:#}"),

			Error::Custom => write!(f, "Unknown custom error"),
			#[cfg(feature = "alloc")]
			Error::Message(msg) => write!(f, "Custom error: {msg}"),
		}
	}
}

impl ::core::error::Error for Error {
	fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
		match self {
			Error::Format(err) => Some(err),
			Error::StringNotUtf8(err) => Some(err),
			#[cfg(feature = "std")]
			Error::Io(err) => Some(err),
			_ => None,
		}
	}
}

impl From<::core::fmt::Error> for Error {
	#[inline]
	fn from(err: ::core::fmt::Error) -> Self {
		Self::Format(err)
	}
}

impl From<::core::str::Utf8Error> for Error {
	#[inline]
	fn from(err: ::core::str::Utf8Error) -> Self {
		Self::StringNotUtf8(err)
	}
}

#[cfg(feature = "std")]
impl From<::std::io::Error> for Error {
	#[inline]
	fn from(err: ::std::io::Error) -> Self {
		Self::Io(err)
	}
}

impl ::serde::ser::Error for Error {
	#[cfg(not(feature = "alloc"))]
	#[inline]
	fn custom<T>(_msg: T) -> Self
	where
		T: Display,
	{
		Self::Custom
	}

	#[cfg(feature = "alloc")]
	#[inline]
	fn custom<T>(msg: T) -> Self
	where
		T: Display,
	{
		Self::Message(::alloc::format!("{msg}"))
	}
}

impl ::serde::de::Error for Error {
	#[cfg(not(feature = "alloc"))]
	#[inline]
	fn custom<T>(_msg: T) -> Self
	where
		T: Display,
	{
		Self::Custom
	}

	#[cfg(feature = "alloc")]
	#[inline]
	fn custom<T>(msg: T) -> Self
	where
		T: Display,
	{
		Self::Message(::alloc::format!("{msg}"))
	}
}