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
use super::*;

/// Error when decoding an SOMEIP header & payload from a slice.
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum SomeipSliceError {
    /// Error when an length error is encountered (e.g. unexpected
    /// end of slice).
    Len(LenError),

    /// Error caused by the contents of the header.
    Content(SomeipHeaderError),
}

impl core::fmt::Display for SomeipSliceError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        use SomeipSliceError::*;
        match self {
            Len(err) => err.fmt(f),
            Content(value) => value.fmt(f),
        }
    }
}

impl std::error::Error for SomeipSliceError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        use SomeipSliceError::*;
        match self {
            Len(err) => Some(err),
            Content(err) => Some(err),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{SomeipSliceError::*, *};

    #[test]
    fn debug() {
        let err = SomeipHeaderError::UnsupportedProtocolVersion(5);
        assert_eq!(
            format!("Content({:?})", err.clone()),
            format!("{:?}", Content(err))
        );
    }

    #[test]
    fn clone_eq_hash_ord() {
        use core::cmp::Ordering;
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let err = Content(SomeipHeaderError::UnsupportedProtocolVersion(5));
        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);
        assert_eq!(Ordering::Equal, err.cmp(&err));
        assert_eq!(Some(Ordering::Equal), err.partial_cmp(&err));
    }

    #[test]
    fn fmt() {
        {
            let err = LenError {
                required_len: 1,
                layer: Layer::SomeipHeader,
                len: 2,
                len_source: LenSource::Slice,
            };
            assert_eq!(format!("{}", &err), format!("{}", Len(err)));
        }
        {
            let err = SomeipHeaderError::UnsupportedProtocolVersion(6);
            assert_eq!(format!("{}", &err), format!("{}", Content(err.clone())));
        }
    }

    #[test]
    fn source() {
        use std::error::Error;

        assert!(Len(LenError {
            required_len: 1,
            layer: Layer::SomeipHeader,
            len: 2,
            len_source: LenSource::Slice,
        })
        .source()
        .is_some());
        assert!(Content(SomeipHeaderError::UnsupportedProtocolVersion(6))
            .source()
            .is_some());
    }
}