protozer0/
error.rs

1use core::{
2	error,
3	fmt::{self, Display, Formatter},
4	result,
5	str::Utf8Error,
6};
7
8pub type Result<T> = result::Result<T, Error>;
9
10#[derive(Debug, PartialEq, Eq)]
11pub enum Error {
12	VarintTooLong,
13	UnknownWireType,
14	EndOfBuffer,
15	InvalidFieldNumber,
16	InvalidLength,
17	InvalidUtf8(Utf8Error),
18}
19
20impl From<Utf8Error> for Error {
21	fn from(value: Utf8Error) -> Self {
22		Self::InvalidUtf8(value)
23	}
24}
25
26impl Display for Error {
27	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
28		let s = match self {
29			Self::VarintTooLong => "varint too long",
30			Self::UnknownWireType => "unknown wire type",
31			Self::EndOfBuffer => "end of buffer",
32			Self::InvalidFieldNumber => "invalid field number",
33			Self::InvalidLength => "invalid length",
34			Self::InvalidUtf8(_) => "invalid utf8",
35		};
36		f.write_str(s)
37	}
38}
39
40impl error::Error for Error {
41	fn source(&self) -> Option<&(dyn error::Error + 'static)> {
42		match self {
43			Self::InvalidUtf8(e) => Some(e),
44			_ => None,
45		}
46	}
47}