1 use crate::EtherType; 2 3 /// Errors in an double vlan header encountered while decoding it. 4 #[derive(Clone, Debug, Eq, PartialEq, Hash)] 5 pub enum HeaderError { 6 /// Error when two vlan header were expected but the ether_type 7 /// value of the first vlan header is not an vlan header type. 8 NonVlanEtherType { 9 /// Non-VLAN ether type encountered in the outer vlan 10 /// header. 11 unexpected_ether_type: EtherType, 12 }, 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 NonVlanEtherType { unexpected_ether_type } => write!(f, "Double VLAN Error: Expected two VLAN headers but the outer VLAN header is followed by a non-VLAN header of ether type {:?}.", unexpected_ether_type), 20 } 21 } 22 } 23 24 #[cfg(feature = "std")] 25 #[cfg_attr(docsrs, doc(cfg(feature = "std")))] 26 impl std::error::Error for HeaderError { 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::HeaderError::*; 35 use crate::EtherType; 36 use alloc::format; 37 use std::{ 38 collections::hash_map::DefaultHasher, 39 error::Error, 40 hash::{Hash, Hasher}, 41 }; 42 43 #[test] debug()44 fn debug() { 45 assert_eq!( 46 format!( 47 "NonVlanEtherType {{ unexpected_ether_type: {:?} }}", 48 EtherType(1) 49 ), 50 format!( 51 "{:?}", 52 NonVlanEtherType { 53 unexpected_ether_type: 1.into() 54 } 55 ) 56 ); 57 } 58 59 #[test] clone_eq_hash()60 fn clone_eq_hash() { 61 let err = NonVlanEtherType { 62 unexpected_ether_type: 1.into(), 63 }; 64 assert_eq!(err, err.clone()); 65 let hash_a = { 66 let mut hasher = DefaultHasher::new(); 67 err.hash(&mut hasher); 68 hasher.finish() 69 }; 70 let hash_b = { 71 let mut hasher = DefaultHasher::new(); 72 err.clone().hash(&mut hasher); 73 hasher.finish() 74 }; 75 assert_eq!(hash_a, hash_b); 76 } 77 78 #[test] fmt()79 fn fmt() { 80 assert_eq!( 81 "Double VLAN Error: Expected two VLAN headers but the outer VLAN header is followed by a non-VLAN header of ether type 0x0001.", 82 format!("{}", NonVlanEtherType{ unexpected_ether_type: 1.into() }) 83 ); 84 } 85 86 #[cfg(feature = "std")] 87 #[test] source()88 fn source() { 89 assert!(NonVlanEtherType { 90 unexpected_ether_type: 1.into() 91 } 92 .source() 93 .is_none()); 94 } 95 } 96