Skip to main content

etherparse/err/ipv4/
bad_options_len.rs

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)]
6pub struct BadOptionsLen {
7    /// Invalid length.
8    pub bad_len: usize,
9}
10
11impl core::fmt::Display for BadOptionsLen {
12    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
17impl core::error::Error for BadOptionsLen {
18    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
19        None
20    }
21}
22
23#[cfg(test)]
24mod tests {
25    use super::*;
26    use alloc::format;
27    use std::{
28        collections::hash_map::DefaultHasher,
29        error::Error,
30        hash::{Hash, Hasher},
31    };
32
33    #[test]
34    fn debug() {
35        assert_eq!(
36            "BadOptionsLen { bad_len: 123 }",
37            format!("{:?}", BadOptionsLen { bad_len: 123 })
38        );
39    }
40
41    #[test]
42    fn clone_eq_hash() {
43        let err = BadOptionsLen { bad_len: 123 };
44        assert_eq!(err, err.clone());
45        let hash_a = {
46            let mut hasher = DefaultHasher::new();
47            err.hash(&mut hasher);
48            hasher.finish()
49        };
50        let hash_b = {
51            let mut hasher = DefaultHasher::new();
52            err.clone().hash(&mut hasher);
53            hasher.finish()
54        };
55        assert_eq!(hash_a, hash_b);
56    }
57
58    #[test]
59    fn fmt() {
60        let err = BadOptionsLen { bad_len: 123 };
61        assert_eq!(
62            format!("{}", err),
63            "Slice of length 123 cannot be set as IPv4 header options. The length must be a multiple of 4 and at maximum 40."
64        );
65    }
66
67    #[cfg(feature = "std")]
68    #[test]
69    fn source() {
70        assert!(BadOptionsLen { bad_len: 123 }.source().is_none());
71    }
72}