1 use super::*; 2 3 /// Code values for ICMPv6 time exceeded message. 4 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 5 pub enum TimeExceededCode { 6 /// "hop limit exceeded in transit" 7 HopLimitExceeded = 0, 8 /// "fragment reassembly time exceeded" 9 FragmentReassemblyTimeExceeded = 1, 10 } 11 12 impl TimeExceededCode { 13 /// Tries to convert a code [`u8`] value to a [`TimeExceededCode`] value. 14 /// 15 /// Returns [`None`] in case the code value is not known as a time exceeded code. 16 #[inline] from_u8(code_u8: u8) -> Option<TimeExceededCode>17 pub fn from_u8(code_u8: u8) -> Option<TimeExceededCode> { 18 use TimeExceededCode::*; 19 match code_u8 { 20 CODE_TIME_EXCEEDED_HOP_LIMIT_EXCEEDED => Some(HopLimitExceeded), 21 CODE_TIME_EXCEEDED_FRAGMENT_REASSEMBLY_TIME_EXCEEDED => { 22 Some(FragmentReassemblyTimeExceeded) 23 } 24 _ => None, 25 } 26 } 27 28 /// Returns the [`u8`] value of the code. 29 #[inline] code_u8(&self) -> u830 pub fn code_u8(&self) -> u8 { 31 *self as u8 32 } 33 } 34 35 #[cfg(test)] 36 pub(crate) mod time_exceeded_code_test_consts { 37 use super::{TimeExceededCode::*, *}; 38 39 pub const VALID_VALUES: [(TimeExceededCode, u8); 2] = [ 40 (HopLimitExceeded, CODE_TIME_EXCEEDED_HOP_LIMIT_EXCEEDED), 41 ( 42 FragmentReassemblyTimeExceeded, 43 CODE_TIME_EXCEEDED_FRAGMENT_REASSEMBLY_TIME_EXCEEDED, 44 ), 45 ]; 46 } 47 48 #[cfg(test)] 49 mod test { 50 use super::{time_exceeded_code_test_consts::*, TimeExceededCode::*, *}; 51 use alloc::format; 52 53 #[test] from_u8()54 fn from_u8() { 55 for (code, code_u8) in VALID_VALUES { 56 assert_eq!(Some(code), TimeExceededCode::from_u8(code_u8)); 57 } 58 for code_u8 in 2..=u8::MAX { 59 assert_eq!(None, TimeExceededCode::from_u8(code_u8)); 60 } 61 } 62 63 #[test] from_enum()64 fn from_enum() { 65 for (code, code_u8) in VALID_VALUES { 66 assert_eq!(code.code_u8(), code_u8); 67 } 68 } 69 70 #[test] clone_eq()71 fn clone_eq() { 72 for (code, _) in VALID_VALUES { 73 assert_eq!(code.clone(), code); 74 } 75 } 76 77 #[test] debug()78 fn debug() { 79 let tests = [ 80 (HopLimitExceeded, "HopLimitExceeded"), 81 ( 82 FragmentReassemblyTimeExceeded, 83 "FragmentReassemblyTimeExceeded", 84 ), 85 ]; 86 for test in tests { 87 assert_eq!(format!("{:?}", test.0), test.1); 88 } 89 } 90 } 91