1 /// Errors that can be encountered while decoding an IP 2 /// authentication header. 3 #[derive(Clone, Debug, Eq, PartialEq, Hash)] 4 pub enum HeaderError { 5 /// Error when the payload length is zero and therefor 6 /// too small to contain the minimum fields of the IP 7 /// authentication itself. 8 ZeroPayloadLen, 9 } 10 11 impl core::fmt::Display for HeaderError { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result12 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 13 use HeaderError::*; 14 match self { 15 ZeroPayloadLen => write!(f, "IP Authentication Header Error: Payload Length too small (0). The payload length must be at least 1."), 16 } 17 } 18 } 19 20 #[cfg(feature = "std")] 21 #[cfg_attr(docsrs, doc(cfg(feature = "std")))] 22 impl std::error::Error for HeaderError { source(&self) -> Option<&(dyn std::error::Error + 'static)>23 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { 24 None 25 } 26 } 27 28 #[cfg(test)] 29 mod tests { 30 use super::HeaderError::*; 31 use alloc::format; 32 use std::{ 33 collections::hash_map::DefaultHasher, 34 error::Error, 35 hash::{Hash, Hasher}, 36 }; 37 38 #[test] debug()39 fn debug() { 40 assert_eq!("ZeroPayloadLen", format!("{:?}", ZeroPayloadLen)); 41 } 42 43 #[test] clone_eq_hash()44 fn clone_eq_hash() { 45 let err = ZeroPayloadLen; 46 assert_eq!(err, err.clone()); 47 let hash_a = { 48 let mut hasher = DefaultHasher::new(); 49 err.hash(&mut hasher); 50 hasher.finish() 51 }; 52 let hash_b = { 53 let mut hasher = DefaultHasher::new(); 54 err.clone().hash(&mut hasher); 55 hasher.finish() 56 }; 57 assert_eq!(hash_a, hash_b); 58 } 59 60 #[test] fmt()61 fn fmt() { 62 assert_eq!( 63 "IP Authentication Header Error: Payload Length too small (0). The payload length must be at least 1.", 64 format!("{}", ZeroPayloadLen) 65 ); 66 } 67 68 #[cfg(feature = "std")] 69 #[test] source()70 fn source() { 71 assert!(ZeroPayloadLen.source().is_none()); 72 } 73 } 74