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