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
use std::borrow::Cow;
use std::fmt;
use std::error;
use std::io;
use std::result;
use std::str::Utf8Error;
use std::string::FromUtf8Error;
use serde::de;
use serde::ser;
use crate::parser::Token;
use self::Error::*;
#[derive(Debug)]
pub enum Error {
Io(io::Error),
Encoding(Cow<'static, str>),
UnknownValue {
tag: u32,
value: u32
},
ParsingFinished,
TooLongLabel(usize),
Unexpected(&'static str, Token),
Deserialize(String),
Serialize(String),
}
pub type Result<T> = result::Result<T, Error>;
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
Io(ref err) => err.fmt(fmt),
Encoding(ref msg) => msg.fmt(fmt),
UnknownValue { tag, value } => write!(fmt, "Unknown field value (tag: {}, value: {})", tag, value),
ParsingFinished => write!(fmt, "Parsing finished"),
TooLongLabel(len) => write!(fmt, "Too long label: label can contain up to 16 bytes, but string contains {} bytes in UTF-8", len),
Unexpected(ref expected, ref actual) => write!(fmt, "Expected {}, but {:?} found", expected, actual),
Deserialize(ref msg) => msg.fmt(fmt),
Serialize(ref msg) => msg.fmt(fmt),
}
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
Io(ref err) => Some(err),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(value: io::Error) -> Self { Io(value) }
}
impl From<Cow<'static, str>> for Error {
fn from(value: Cow<'static, str>) -> Self { Encoding(value) }
}
impl From<Utf8Error> for Error {
fn from(value: Utf8Error) -> Self { Encoding(value.to_string().into()) }
}
impl From<FromUtf8Error> for Error {
fn from(value: FromUtf8Error) -> Self { Encoding(value.to_string().into()) }
}
impl de::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Deserialize(msg.to_string())
}
}
impl ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::Serialize(msg.to_string())
}
}