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
use std::error;
use std::fmt;
#[derive(PartialEq, Clone, Debug)]
pub enum TlvError {
InvalidInput,
TagIsRFU,
ParseIntError,
TruncatedInput,
Inconsistant,
InvalidLength,
}
impl fmt::Display for TlvError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match self {
TlvError::InvalidInput => "Invalid tag encountered",
TlvError::TagIsRFU => "Tag is reserved for future usage",
TlvError::ParseIntError => "Error parsing input as int",
TlvError::TruncatedInput => "Error input too short",
TlvError::Inconsistant => "Inconsistant (tag, value) pair",
TlvError::InvalidLength => "Read invalid length value",
};
write!(f, "{}", s)
}
}
impl error::Error for TlvError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
_ => None,
}
}
}
impl From<std::num::ParseIntError> for TlvError {
fn from(_: std::num::ParseIntError) -> Self {
TlvError::ParseIntError
}
}
impl From<untrusted::EndOfInput> for TlvError {
fn from(_: untrusted::EndOfInput) -> Self {
TlvError::TruncatedInput
}
}