1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::err;
use core::{cmp::Eq, cmp::PartialEq, fmt::Debug, fmt::Display, hash::Hash};

/// Error if a value exceeds the maximum allowed value.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct ValueTooBigError<T: Sized + Clone + Display + Debug + Eq + PartialEq + Hash> {
    /// Value that was disallowed.
    pub actual: T,

    /// Maximum allowed value (inclusive).
    pub max_allowed: T,

    /// Type of value.
    pub value_type: err::ValueType,
}

impl<T> core::fmt::Display for ValueTooBigError<T>
where
    T: Sized + Clone + Display + Debug + Eq + PartialEq + Hash,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(
            f,
            "Error '{}' is too big to be a '{}' (maximum allowed value is '{}')",
            self.actual, self.value_type, self.max_allowed
        )
    }
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<T> std::error::Error for ValueTooBigError<T>
where
    T: Sized + Clone + Display + Debug + Eq + PartialEq + Hash,
{
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::{collections::hash_map::DefaultHasher, error::Error, format, hash::Hasher};

    #[test]
    fn fmt() {
        assert_eq!(
            format!(
                "{}",
                ValueTooBigError {
                    actual: 3,
                    max_allowed: 2,
                    value_type: err::ValueType::IpFragmentOffset
                }
            ),
            "Error '3' is too big to be a 'IP Fragment Offset' (maximum allowed value is '2')"
        );
    }

    #[test]
    fn dbg() {
        assert_eq!(
            format!(
                "{:?}",
                ValueTooBigError {
                    actual: 3,
                    max_allowed: 2,
                    value_type: err::ValueType::IpFragmentOffset
                }
            ),
            format!(
                "ValueTooBigError {{ actual: {}, max_allowed: {}, value_type: {:?} }}",
                3,
                2,
                err::ValueType::IpFragmentOffset
            )
        );
    }

    #[test]
    fn clone_eq_hash() {
        let err = ValueTooBigError {
            actual: 3,
            max_allowed: 2,
            value_type: err::ValueType::IpFragmentOffset,
        };
        assert_eq!(err, err.clone());
        let hash_a = {
            let mut hasher = DefaultHasher::new();
            err.hash(&mut hasher);
            hasher.finish()
        };
        let hash_b = {
            let mut hasher = DefaultHasher::new();
            err.clone().hash(&mut hasher);
            hasher.finish()
        };
        assert_eq!(hash_a, hash_b);
    }

    #[cfg(feature = "std")]
    #[test]
    fn source() {
        assert!(ValueTooBigError {
            actual: 3,
            max_allowed: 2,
            value_type: err::ValueType::IpFragmentOffset
        }
        .source()
        .is_none());
    }
}