etherparse/err/ipv6/
header_read_error.rs

1use super::HeaderError;
2
3/// Error when decoding an IPv6 header via a `std::io::Read` source.
4#[cfg(feature = "std")]
5#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
6#[derive(Debug)]
7pub enum HeaderReadError {
8    /// IO error was encountered while reading header.
9    Io(std::io::Error),
10
11    /// Error caused by the contents of the header.
12    Content(HeaderError),
13}
14
15#[cfg(feature = "std")]
16#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
17impl HeaderReadError {
18    /// Returns the `std::io::Error` value if the `HeaderReadError` is `Io`.
19    /// Otherwise `None` is returned.
20    #[inline]
21    pub fn io_error(self) -> Option<std::io::Error> {
22        use HeaderReadError::*;
23        match self {
24            Io(value) => Some(value),
25            _ => None,
26        }
27    }
28
29    /// Returns the `err::ipv6::HeaderError` value if the `HeaderReadError` is `Content`.
30    /// Otherwise `None` is returned.
31    #[inline]
32    pub fn content_error(self) -> Option<HeaderError> {
33        use HeaderReadError::*;
34        match self {
35            Content(value) => Some(value),
36            _ => None,
37        }
38    }
39}
40
41#[cfg(feature = "std")]
42#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
43impl core::fmt::Display for HeaderReadError {
44    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
45        use HeaderReadError::*;
46        match self {
47            Io(err) => write!(f, "IPv6 Header IO Error: {err}"),
48            Content(value) => value.fmt(f),
49        }
50    }
51}
52
53impl core::error::Error for HeaderReadError {
54    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
55        use HeaderReadError::*;
56        match self {
57            Io(err) => Some(err),
58            Content(err) => Some(err),
59        }
60    }
61}
62
63#[cfg(all(test, feature = "std"))]
64mod test {
65    use super::{HeaderReadError::*, *};
66    use alloc::format;
67
68    #[test]
69    fn debug() {
70        let err = HeaderError::UnexpectedVersion { version_number: 6 };
71        assert_eq!(
72            format!("Content({:?})", err.clone()),
73            format!("{:?}", Content(err))
74        );
75    }
76
77    #[test]
78    fn fmt() {
79        {
80            let err = std::io::Error::new(
81                std::io::ErrorKind::UnexpectedEof,
82                "failed to fill whole buffer",
83            );
84            assert_eq!(
85                format!("IPv6 Header IO Error: {}", err),
86                format!("{}", Io(err))
87            );
88        }
89        {
90            let err = HeaderError::UnexpectedVersion { version_number: 6 };
91            assert_eq!(format!("{}", &err), format!("{}", Content(err.clone())));
92        }
93    }
94
95    #[test]
96    fn source() {
97        use core::error::Error;
98        assert!(Io(std::io::Error::new(
99            std::io::ErrorKind::UnexpectedEof,
100            "failed to fill whole buffer",
101        ))
102        .source()
103        .is_some());
104        assert!(
105            Content(HeaderError::UnexpectedVersion { version_number: 6 })
106                .source()
107                .is_some()
108        );
109    }
110
111    #[test]
112    fn io_error() {
113        assert!(Io(std::io::Error::new(
114            std::io::ErrorKind::UnexpectedEof,
115            "failed to fill whole buffer",
116        ))
117        .io_error()
118        .is_some());
119        assert!(
120            Content(HeaderError::UnexpectedVersion { version_number: 6 })
121                .io_error()
122                .is_none()
123        );
124    }
125
126    #[test]
127    fn content_error() {
128        assert_eq!(
129            None,
130            Io(std::io::Error::new(
131                std::io::ErrorKind::UnexpectedEof,
132                "failed to fill whole buffer",
133            ))
134            .content_error()
135        );
136        {
137            let err = HeaderError::UnexpectedVersion { version_number: 6 };
138            assert_eq!(Some(err.clone()), Content(err.clone()).content_error());
139        }
140    }
141}