nex_packet/builder/
error.rs1use core::fmt;
2
3#[derive(Clone, Debug, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum BuildError {
7 LengthOverflow {
9 context: &'static str,
11 maximum: usize,
13 actual: usize,
15 },
16 ValueOutOfRange {
18 context: &'static str,
20 maximum: usize,
22 actual: usize,
24 },
25 LengthTooShort {
27 context: &'static str,
29 minimum: usize,
31 actual: usize,
33 },
34 InvalidFieldLength {
36 context: &'static str,
38 expected: usize,
40 actual: usize,
42 },
43 AddressFamilyMismatch {
45 context: &'static str,
47 },
48 SerializationFailed {
50 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}