• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /// Error in the protocol addresses when creating an [`crate::ArpPacket`] or
2 /// changing the protocol addresses in an [`crate::ArpPacket`].
3 #[derive(Debug, Clone, Eq, PartialEq, Hash)]
4 pub enum ArpProtoAddrError {
5     /// Error if the given protocol address is longer than
6     /// the maximum of 255 bytes/octets.
7     LenTooBig(usize),
8 
9     /// Protocol address lengths of sender and target differ.
10     LenNonMatching(usize, usize),
11 }
12 
13 impl core::fmt::Display for ArpProtoAddrError {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result14     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
15         match self {
16             ArpProtoAddrError::LenTooBig(len) =>
17                 write!(f, "ARP Protocol Address Error: Given protocol address has a length of {len} which is greater then the maximum of 255."),
18             ArpProtoAddrError::LenNonMatching(len_sender, len_target) =>
19                 write!(f, "ARP Protocol Address Error: Given sender & target protocol addresses have differing lengths of {len_sender} & {len_target} (must be matching)."),
20         }
21     }
22 }
23 
24 #[cfg(feature = "std")]
25 #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
26 impl std::error::Error for ArpProtoAddrError {
source(&self) -> Option<&(dyn std::error::Error + 'static)>27     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
28         None
29     }
30 }
31 
32 #[cfg(test)]
33 mod tests {
34     use super::ArpProtoAddrError::*;
35     use alloc::format;
36     use std::{
37         collections::hash_map::DefaultHasher,
38         error::Error,
39         hash::{Hash, Hasher},
40     };
41 
42     #[test]
debug()43     fn debug() {
44         assert_eq!("LenTooBig(300)", format!("{:?}", LenTooBig(300)));
45     }
46 
47     #[test]
clone_eq_hash()48     fn clone_eq_hash() {
49         let err = LenTooBig(300);
50         assert_eq!(err, err.clone());
51         let hash_a = {
52             let mut hasher = DefaultHasher::new();
53             err.hash(&mut hasher);
54             hasher.finish()
55         };
56         let hash_b = {
57             let mut hasher = DefaultHasher::new();
58             err.clone().hash(&mut hasher);
59             hasher.finish()
60         };
61         assert_eq!(hash_a, hash_b);
62     }
63 
64     #[test]
fmt()65     fn fmt() {
66         let tests = [
67             (LenTooBig(301), "ARP Protocol Address Error: Given protocol address has a length of 301 which is greater then the maximum of 255."),
68             (LenNonMatching(23, 24), "ARP Protocol Address Error: Given sender & target protocol addresses have differing lengths of 23 & 24 (must be matching).")
69         ];
70         for test in tests {
71             assert_eq!(format!("{}", test.0), test.1);
72         }
73     }
74 
75     #[cfg(feature = "std")]
76     #[test]
source()77     fn source() {
78         assert!(LenTooBig(300).source().is_none());
79     }
80 }
81