etherparse/err/ipv6_exts/
header_read_error.rs1use super::HeaderError;
2
3#[cfg(feature = "std")]
5#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
6#[derive(Debug)]
7pub enum HeaderReadError {
8 Io(std::io::Error),
10
11 Content(HeaderError),
13}
14
15#[cfg(feature = "std")]
16#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
17impl HeaderReadError {
18 #[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 #[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 Extension Header IO Error: {}", err),
48 Content(value) => value.fmt(f),
49 }
50 }
51}
52
53#[cfg(feature = "std")]
54#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
55impl std::error::Error for HeaderReadError {
56 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57 use HeaderReadError::*;
58 match self {
59 Io(err) => Some(err),
60 Content(err) => Some(err),
61 }
62 }
63}
64
65#[cfg(all(test, feature = "std"))]
66mod test {
67 use super::{HeaderReadError::*, *};
68 use alloc::format;
69
70 #[test]
71 fn debug() {
72 let err = HeaderError::HopByHopNotAtStart;
73 assert_eq!(
74 format!("Content({:?})", err.clone()),
75 format!("{:?}", Content(err))
76 );
77 }
78
79 #[test]
80 fn fmt() {
81 {
82 let err = std::io::Error::new(
83 std::io::ErrorKind::UnexpectedEof,
84 "failed to fill whole buffer",
85 );
86 assert_eq!(
87 format!("IPv6 Extension Header IO Error: {}", err),
88 format!("{}", Io(err))
89 );
90 }
91 {
92 let err = HeaderError::HopByHopNotAtStart;
93 assert_eq!(format!("{}", &err), format!("{}", Content(err.clone())));
94 }
95 }
96
97 #[test]
98 fn source() {
99 use std::error::Error;
100 assert!(Io(std::io::Error::new(
101 std::io::ErrorKind::UnexpectedEof,
102 "failed to fill whole buffer",
103 ))
104 .source()
105 .is_some());
106 assert!(Content(HeaderError::HopByHopNotAtStart).source().is_some());
107 }
108
109 #[test]
110 fn io_error() {
111 assert!(Io(std::io::Error::new(
112 std::io::ErrorKind::UnexpectedEof,
113 "failed to fill whole buffer",
114 ))
115 .io_error()
116 .is_some());
117 assert!(Content(HeaderError::HopByHopNotAtStart)
118 .io_error()
119 .is_none());
120 }
121
122 #[test]
123 fn content_error() {
124 assert_eq!(
125 None,
126 Io(std::io::Error::new(
127 std::io::ErrorKind::UnexpectedEof,
128 "failed to fill whole buffer",
129 ))
130 .content_error()
131 );
132 {
133 let err = HeaderError::HopByHopNotAtStart;
134 assert_eq!(Some(err.clone()), Content(err.clone()).content_error());
135 }
136 }
137}