• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /// Error when creating an [`crate::Ipv6RawExtHeader`] and the
2 /// payload len is non representable.
3 #[derive(Clone, Debug, Eq, PartialEq, Hash)]
4 pub enum ExtPayloadLenError {
5     /// Error when the payload length is smaller then
6     /// [`crate::Ipv6RawExtHeader::MIN_PAYLOAD_LEN`] (6).
7     TooSmall(usize),
8 
9     /// Error when the payload length is bigger then
10     /// [`crate::Ipv6RawExtHeader::MAX_PAYLOAD_LEN`] (2046).
11     TooBig(usize),
12 
13     /// Error when the payload length can not be represented
14     /// as a multiple of 8-bytes in the extension header
15     /// (`0 == (payload.len() + 2) % 8` is not fulfilled).
16     Unaligned(usize),
17 }
18 
19 impl core::fmt::Display for ExtPayloadLenError {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result20     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21         use ExtPayloadLenError::*;
22         match self {
23             TooSmall(size) =>
24                 write!(f, "IPv6 extensions header payload length is too small. The payload size ({} bytes) is less then 6 octets which is the minimum IPv6 extension header payload size.", size),
25             TooBig(size) =>
26                 write!(f, "IPv6 extensions header payload length is too large. The payload size ({} bytes) is larger then what can be be represented by the 'extended header size' field in an IPv6 extension header.", size),
27             Unaligned(size) =>
28                 write!(f, "IPv6 extensions header 'payload length ({} bytes) + 2' is not multiple of 8 (+ 2 for the `next_header` and `header_length` fields). This is required as the header length field can only express lengths in multiple of 8 bytes.", size),
29         }
30     }
31 }
32 
33 #[cfg(feature = "std")]
34 #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
35 impl std::error::Error for ExtPayloadLenError {
source(&self) -> Option<&(dyn std::error::Error + 'static)>36     fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
37         None
38     }
39 }
40 
41 #[cfg(test)]
42 mod tests {
43     use super::ExtPayloadLenError::*;
44     use crate::*;
45     use alloc::format;
46     use std::{
47         collections::hash_map::DefaultHasher,
48         error::Error,
49         hash::{Hash, Hasher},
50     };
51 
52     #[test]
debug()53     fn debug() {
54         assert_eq!("TooBig(3000)", format!("{:?}", TooBig(3000)));
55     }
56 
57     #[test]
clone_eq_hash()58     fn clone_eq_hash() {
59         let err = TooBig(5000);
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             "IPv6 extensions header payload length is too small. The payload size (2 bytes) is less then 6 octets which is the minimum IPv6 extension header payload size.",
78             format!("{}", TooSmall(2))
79         );
80         assert_eq!(
81             "IPv6 extensions header payload length is too large. The payload size (4000 bytes) is larger then what can be be represented by the 'extended header size' field in an IPv6 extension header.",
82             format!("{}", TooBig(4000))
83         );
84         assert_eq!(
85             "IPv6 extensions header 'payload length (12 bytes) + 2' is not multiple of 8 (+ 2 for the `next_header` and `header_length` fields). This is required as the header length field can only express lengths in multiple of 8 bytes.",
86             format!("{}", Unaligned(12))
87         );
88     }
89 
90     #[cfg(feature = "std")]
91     #[test]
source()92     fn source() {
93         assert!(TooSmall(1).source().is_none());
94         assert!(TooBig(4000).source().is_none());
95         assert!(Unaligned(12).source().is_none());
96     }
97 }
98