1 /// Error if a slice can not be used as options data in 2 /// [`crate::Ipv4Options`] as then length is non compatible. 3 /// 4 /// The length for options in an IPv4 header 5 #[derive(Clone, Debug, Eq, PartialEq, Hash)] 6 pub struct BadOptionsLen { 7 /// Invalid length. 8 pub bad_len: usize, 9 } 10 11 impl core::fmt::Display for BadOptionsLen { fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result12 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 13 write!(f, "Slice of length {} cannot be set as IPv4 header options. The length must be a multiple of 4 and at maximum 40.", self.bad_len) 14 } 15 } 16 17 #[cfg(feature = "std")] 18 #[cfg_attr(docsrs, doc(cfg(feature = "std")))] 19 impl std::error::Error for BadOptionsLen { source(&self) -> Option<&(dyn std::error::Error + 'static)>20 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { 21 None 22 } 23 } 24 25 #[cfg(test)] 26 mod tests { 27 use super::*; 28 use alloc::format; 29 use std::{ 30 collections::hash_map::DefaultHasher, 31 error::Error, 32 hash::{Hash, Hasher}, 33 }; 34 35 #[test] debug()36 fn debug() { 37 assert_eq!( 38 "BadOptionsLen { bad_len: 123 }", 39 format!("{:?}", BadOptionsLen { bad_len: 123 }) 40 ); 41 } 42 43 #[test] clone_eq_hash()44 fn clone_eq_hash() { 45 let err = BadOptionsLen { bad_len: 123 }; 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 let err = BadOptionsLen { bad_len: 123 }; 63 assert_eq!( 64 format!("{}", err), 65 "Slice of length 123 cannot be set as IPv4 header options. The length must be a multiple of 4 and at maximum 40." 66 ); 67 } 68 69 #[cfg(feature = "std")] 70 #[test] source()71 fn source() { 72 assert!(BadOptionsLen { bad_len: 123 }.source().is_none()); 73 } 74 } 75