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
//! Structured parse errors for diagnosable packet parsing APIs.
use core::fmt;
/// Structured error returned by `try_from_*` parsing APIs.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseError {
/// The input buffer was shorter than the protocol minimum.
BufferTooShort {
/// Human-readable parse context.
context: &'static str,
/// Minimum required number of bytes.
minimum: usize,
/// Actual number of bytes available.
actual: usize,
},
/// A length-like field contained an invalid value.
InvalidLength {
/// Human-readable parse context.
context: &'static str,
/// Parsed value that failed validation.
value: usize,
},
/// The packet contains a malformed header field.
Malformed {
/// Human-readable parse context.
context: &'static str,
},
/// The packet payload was truncated relative to its header lengths.
Truncated {
/// Human-readable parse context.
context: &'static str,
/// Expected number of bytes.
expected: usize,
/// Actual number of bytes available.
actual: usize,
},
/// Parsing failed because a compression loop or excessive indirection was detected.
CompressionLoop {
/// Human-readable parse context.
context: &'static str,
},
/// Parsing failed because an unsupported or invalid pointer/compression form was encountered.
InvalidCompression {
/// Human-readable parse context.
context: &'static str,
},
/// A UTF-8 conversion failed while parsing text-like data.
InvalidUtf8 {
/// Human-readable parse context.
context: &'static str,
},
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParseError::BufferTooShort {
context,
minimum,
actual,
} => write!(
f,
"{context}: buffer too short (expected at least {minimum} bytes, got {actual})"
),
ParseError::InvalidLength { context, value } => {
write!(f, "{context}: invalid length value {value}")
}
ParseError::Malformed { context } => write!(f, "{context}: malformed packet data"),
ParseError::Truncated {
context,
expected,
actual,
} => write!(
f,
"{context}: truncated payload (expected {expected} bytes, got {actual})"
),
ParseError::CompressionLoop { context } => {
write!(f, "{context}: compression pointer loop detected")
}
ParseError::InvalidCompression { context } => {
write!(f, "{context}: invalid compression pointer")
}
ParseError::InvalidUtf8 { context } => {
write!(f, "{context}: invalid UTF-8 sequence")
}
}
}
}
impl std::error::Error for ParseError {}