1 use super::{ArpHwAddrError, ArpProtoAddrError}; 2 3 /// Error while creating a new [`crate::ArpPacket`]. 4 #[derive(Debug, Clone, Eq, PartialEq, Hash)] 5 pub enum ArpNewError { 6 /// Error in the given hardware addresses. 7 HwAddr(ArpHwAddrError), 8 9 /// Error in the given protocol addresses. 10 ProtoAddr(ArpProtoAddrError), 11 } 12 13 impl core::fmt::Display for ArpNewError { 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 ArpNewError::HwAddr(err) => err.fmt(f), 17 ArpNewError::ProtoAddr(err) => err.fmt(f), 18 } 19 } 20 } 21 22 #[cfg(feature = "std")] 23 #[cfg_attr(docsrs, doc(cfg(feature = "std")))] 24 impl std::error::Error for ArpNewError { source(&self) -> Option<&(dyn std::error::Error + 'static)>25 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { 26 None 27 } 28 } 29 30 #[cfg(test)] 31 mod tests { 32 use crate::err::arp::{ArpHwAddrError, ArpProtoAddrError}; 33 34 use super::ArpNewError::*; 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!( 45 "HwAddr(LenTooBig(300))", 46 format!("{:?}", HwAddr(ArpHwAddrError::LenTooBig(300))) 47 ); 48 } 49 50 #[test] clone_eq_hash()51 fn clone_eq_hash() { 52 let err = HwAddr(ArpHwAddrError::LenTooBig(300)); 53 assert_eq!(err, err.clone()); 54 let hash_a = { 55 let mut hasher = DefaultHasher::new(); 56 err.hash(&mut hasher); 57 hasher.finish() 58 }; 59 let hash_b = { 60 let mut hasher = DefaultHasher::new(); 61 err.clone().hash(&mut hasher); 62 hasher.finish() 63 }; 64 assert_eq!(hash_a, hash_b); 65 } 66 67 #[test] fmt()68 fn fmt() { 69 let tests = [ 70 (HwAddr(ArpHwAddrError::LenTooBig(300)), "ARP Hardware Address Error: Given hardware address has a length of 300 which is greater then the maximum of 255."), 71 (ProtoAddr(ArpProtoAddrError::LenTooBig(301)), "ARP Protocol Address Error: Given protocol address has a length of 301 which is greater then the maximum of 255."), 72 (HwAddr(ArpHwAddrError::LenNonMatching(21, 22)), "ARP Hardware Address Error: Given sender & target hardware addresses have differing lengths of 21 & 22 (must be matching)."), 73 (ProtoAddr(ArpProtoAddrError::LenNonMatching(23, 24)), "ARP Protocol Address Error: Given sender & target protocol addresses have differing lengths of 23 & 24 (must be matching).") 74 ]; 75 for test in tests { 76 assert_eq!(format!("{}", test.0), test.1); 77 } 78 } 79 80 #[cfg(feature = "std")] 81 #[test] source()82 fn source() { 83 assert!(HwAddr(ArpHwAddrError::LenTooBig(300)).source().is_none()); 84 } 85 } 86