1 use crate::ArpHardwareId; 2 3 /// Errors in an Linux Cooked Capture header encountered while decoding it. 4 #[derive(Clone, Debug, Eq, PartialEq, Hash)] 5 pub enum HeaderError { 6 /// Error when the "packet byte" field is not one of the known ones 7 UnsupportedPacketTypeField { 8 // The unexpected packet type number in the SLL header 9 packet_type: u16, 10 }, 11 /// Error when the arp hardware type field is not one of the known ones 12 UnsupportedArpHardwareId { arp_hardware_type: ArpHardwareId }, 13 } 14 15 impl core::fmt::Display for HeaderError { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result16 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 17 use HeaderError::*; 18 match self { 19 UnsupportedPacketTypeField { packet_type } => write!(f, "Linux cooked capture v1 (SLL) Header Error: Encountered '{}' as the packet type, but its not supported.", packet_type), 20 UnsupportedArpHardwareId { arp_hardware_type } => write!(f, "Linux cooked capture v1 (SLL) Header Error: Encountered '{:?}' as the ARP harware type, but its not supported.", arp_hardware_type), 21 } 22 } 23 } 24 25 #[cfg(feature = "std")] 26 #[cfg_attr(docsrs, doc(cfg(feature = "std")))] 27 impl std::error::Error for HeaderError { source(&self) -> Option<&(dyn std::error::Error + 'static)>28 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { 29 use HeaderError::*; 30 match self { 31 UnsupportedPacketTypeField { packet_type: _ } => None, 32 UnsupportedArpHardwareId { 33 arp_hardware_type: _, 34 } => None, 35 } 36 } 37 } 38 39 #[cfg(test)] 40 mod tests { 41 use super::{HeaderError::*, *}; 42 use alloc::format; 43 use std::{ 44 collections::hash_map::DefaultHasher, 45 error::Error, 46 hash::{Hash, Hasher}, 47 }; 48 49 #[test] debug()50 fn debug() { 51 assert_eq!( 52 "UnsupportedPacketTypeField { packet_type: 6 }", 53 format!("{:?}", UnsupportedPacketTypeField { packet_type: 6 }) 54 ); 55 } 56 57 #[test] clone_eq_hash()58 fn clone_eq_hash() { 59 let err = HeaderError::UnsupportedPacketTypeField { packet_type: 6 }; 60 assert_eq!(err, err.clone()); 61 let hash_a = { 62 let mut hasher = DefaultHasher::new(); 63 err.hash(&mut hasher); 64 hasher.finish() 65 }; 66 let hash_b = { 67 let mut hasher = DefaultHasher::new(); 68 err.clone().hash(&mut hasher); 69 hasher.finish() 70 }; 71 assert_eq!(hash_a, hash_b); 72 } 73 74 #[test] fmt()75 fn fmt() { 76 assert_eq!( 77 "Linux cooked capture v1 (SLL) Header Error: Encountered '6' as the packet type, but its not supported.", 78 format!("{}", UnsupportedPacketTypeField{ packet_type: 6 }) 79 ); 80 assert_eq!( 81 "Linux cooked capture v1 (SLL) Header Error: Encountered '1 (Ethernet)' as the ARP harware type, but its not supported.", 82 format!("{}", UnsupportedArpHardwareId{ arp_hardware_type: ArpHardwareId::ETHERNET }) 83 ); 84 } 85 86 #[cfg(feature = "std")] 87 #[test] source()88 fn source() { 89 let values = [ 90 UnsupportedPacketTypeField { packet_type: 6 }, 91 UnsupportedArpHardwareId { 92 arp_hardware_type: ArpHardwareId::ETHERNET, 93 }, 94 ]; 95 for v in values { 96 assert!(v.source().is_none()); 97 } 98 } 99 } 100