Skip to main content

nex_packet/builder/
error.rs

1use core::fmt;
2
3/// Error returned when packet builder inputs cannot be encoded safely.
4#[derive(Clone, Debug, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum BuildError {
7    /// A packet field cannot represent the supplied number of bytes.
8    LengthOverflow {
9        /// Protocol field or section that overflowed.
10        context: &'static str,
11        /// Largest representable value.
12        maximum: usize,
13        /// Supplied or calculated value.
14        actual: usize,
15    },
16    /// A numeric protocol field is outside its representable range.
17    ValueOutOfRange {
18        /// Protocol field containing the value.
19        context: &'static str,
20        /// Largest accepted value.
21        maximum: usize,
22        /// Supplied value.
23        actual: usize,
24    },
25    /// A variable-size field is shorter than the protocol minimum.
26    LengthTooShort {
27        /// Protocol field or section that is too short.
28        context: &'static str,
29        /// Smallest accepted length.
30        minimum: usize,
31        /// Supplied length.
32        actual: usize,
33    },
34    /// A fixed-size field has an unexpected length.
35    InvalidFieldLength {
36        /// Protocol field with the invalid length.
37        context: &'static str,
38        /// Required number of bytes.
39        expected: usize,
40        /// Supplied number of bytes.
41        actual: usize,
42    },
43    /// Source and destination addresses cannot form a checksum pseudo-header.
44    AddressFamilyMismatch {
45        /// Transport protocol requiring the pseudo-header.
46        context: &'static str,
47    },
48    /// An internally assembled packet could not be decoded.
49    SerializationFailed {
50        /// Packet section that failed to round-trip.
51        context: &'static str,
52    },
53}
54
55impl fmt::Display for BuildError {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        match self {
58            Self::LengthOverflow {
59                context,
60                maximum,
61                actual,
62            } => write!(
63                f,
64                "{context} is too long: maximum is {maximum} bytes, got {actual}"
65            ),
66            Self::ValueOutOfRange {
67                context,
68                maximum,
69                actual,
70            } => write!(
71                f,
72                "{context} is out of range: maximum is {maximum}, got {actual}"
73            ),
74            Self::LengthTooShort {
75                context,
76                minimum,
77                actual,
78            } => write!(
79                f,
80                "{context} is too short: minimum is {minimum} bytes, got {actual}"
81            ),
82            Self::InvalidFieldLength {
83                context,
84                expected,
85                actual,
86            } => write!(
87                f,
88                "{context} has an invalid length: expected {expected} bytes, got {actual}"
89            ),
90            Self::AddressFamilyMismatch { context } => write!(
91                f,
92                "{context} checksum requires source and destination addresses from the same family"
93            ),
94            Self::SerializationFailed { context } => {
95                write!(f, "failed to serialize {context}")
96            }
97        }
98    }
99}
100
101impl std::error::Error for BuildError {}
102
103#[cfg(test)]
104mod tests {
105    use super::BuildError;
106
107    fn assert_error_contract<T: std::error::Error + Send + Sync + 'static>() {}
108
109    #[test]
110    fn build_error_implements_public_error_contract() {
111        assert_error_contract::<BuildError>();
112    }
113}