Skip to main content

etherparse/err/ipv6_exts/
header_write_error.rs

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