1 use crate::*; 2 3 /// Error while converting an [`crate::ArpPacket`] to an [`crate::ArpEthIpv4Packet`]. 4 #[derive(Debug, Clone, Eq, PartialEq, Hash)] 5 pub enum ArpEthIpv4FromError { 6 /// Error if `hw_addr_type` is not [`crate::ArpHardwareId::ETHERNET`]. 7 NonMatchingHwType(ArpHardwareId), 8 9 /// Error if `proto_addr_type` is not [`crate::EtherType::IPV4`]. 10 NonMatchingProtocolType(EtherType), 11 12 /// Error if `hw_addr_size` is not `6` 13 NonMatchingHwAddrSize(u8), 14 15 /// Error if `hw_addr_size` is not `6` 16 NonMatchingProtoAddrSize(u8), 17 } 18 19 impl core::fmt::Display for ArpEthIpv4FromError { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result20 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 21 match self { 22 ArpEthIpv4FromError::NonMatchingHwType(t) => 23 write!(f, "Hardware address type is expected to have the type '1 (Ethernet)' but is '{t:?}'"), 24 ArpEthIpv4FromError::NonMatchingProtocolType(t) => 25 write!(f, "Protocol address type is expected to have the type '0x0800 (Internet Protocol version 4 (IPv4))' but is '{t:?}'"), 26 ArpEthIpv4FromError::NonMatchingHwAddrSize(len) => 27 write!(f, "Hardware address size is expected to be 6 but is {len}"), 28 ArpEthIpv4FromError::NonMatchingProtoAddrSize(len) => 29 write!(f, "Protocol address size is expected to be 4 but is {len}"), 30 } 31 } 32 } 33 34 #[cfg(feature = "std")] 35 #[cfg_attr(docsrs, doc(cfg(feature = "std")))] 36 impl std::error::Error for ArpEthIpv4FromError { source(&self) -> Option<&(dyn std::error::Error + 'static)>37 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { 38 None 39 } 40 } 41 42 #[cfg(test)] 43 mod tests { 44 use super::{ArpEthIpv4FromError::*, ArpHardwareId, EtherType}; 45 use alloc::format; 46 use std::{ 47 collections::hash_map::DefaultHasher, 48 error::Error, 49 hash::{Hash, Hasher}, 50 }; 51 52 #[test] debug()53 fn debug() { 54 assert_eq!( 55 "NonMatchingProtoAddrSize(3)", 56 format!("{:?}", NonMatchingProtoAddrSize(3)) 57 ); 58 } 59 60 #[test] clone_eq_hash()61 fn clone_eq_hash() { 62 let err = NonMatchingProtoAddrSize(3); 63 assert_eq!(err, err.clone()); 64 let hash_a = { 65 let mut hasher = DefaultHasher::new(); 66 err.hash(&mut hasher); 67 hasher.finish() 68 }; 69 let hash_b = { 70 let mut hasher = DefaultHasher::new(); 71 err.clone().hash(&mut hasher); 72 hasher.finish() 73 }; 74 assert_eq!(hash_a, hash_b); 75 } 76 77 #[test] fmt()78 fn fmt() { 79 let tests = [ 80 (NonMatchingHwType(ArpHardwareId::CHAOS), "Hardware address type is expected to have the type '1 (Ethernet)' but is '5 (Chaosnet)'"), 81 (NonMatchingProtocolType(EtherType::IPV6), "Protocol address type is expected to have the type '0x0800 (Internet Protocol version 4 (IPv4))' but is '0x86DD (Internet Protocol Version 6 (IPV6))'"), 82 (NonMatchingHwAddrSize(21), "Hardware address size is expected to be 6 but is 21"), 83 (NonMatchingProtoAddrSize(22), "Protocol address size is expected to be 4 but is 22") 84 ]; 85 for test in tests { 86 assert_eq!(format!("{}", test.0), test.1); 87 } 88 } 89 90 #[cfg(feature = "std")] 91 #[test] source()92 fn source() { 93 assert!(NonMatchingProtoAddrSize(3).source().is_none()); 94 } 95 } 96