|
1 | | -use std::{error::Error, fmt, io}; |
2 | 1 | use crate::utils::VerboseErrorExt; |
3 | 2 |
|
4 | 3 | /// Wrap `std::io::Error` with additional message |
5 | 4 | /// |
6 | 5 | /// *Note* Only active when `verbose-errors` feature is enabled for this crate! |
7 | 6 | /// |
8 | 7 | /// Keeps the original error kind and stores the original I/O error as `source`. |
9 | | -impl<T> VerboseErrorExt for Result<T, io::Error> { |
| 8 | +impl<T> VerboseErrorExt for Result<T, std::io::Error> { |
| 9 | + #[cfg(feature = "verbose-errors")] |
10 | 10 | fn verbose_context(self, message: impl Fn() -> String) -> Self { |
11 | | - if cfg!(feature = "verbose-errors") { |
12 | | - self.map_err(|e| VerboseError::wrap(e, message())) |
13 | | - } else { |
14 | | - self |
15 | | - } |
| 11 | + self.map_err(|e| verbose::Error::wrap(e, message())) |
16 | 12 | } |
17 | 13 | } |
18 | 14 |
|
19 | | -#[derive(Debug)] |
20 | | -struct VerboseError { |
21 | | - source: io::Error, |
22 | | - message: String, |
23 | | -} |
| 15 | +#[cfg(feature = "verbose-errors")] |
| 16 | +mod verbose { |
| 17 | + use std::{error::Error as StdError, fmt, io}; |
24 | 18 |
|
25 | | -impl VerboseError { |
26 | | - fn wrap(source: io::Error, message: impl Into<String>) -> io::Error { |
27 | | - io::Error::new( |
28 | | - source.kind(), |
29 | | - VerboseError { |
30 | | - source, |
31 | | - message: message.into(), |
32 | | - }, |
33 | | - ) |
| 19 | + #[derive(Debug)] |
| 20 | + pub(crate) struct Error { |
| 21 | + source: io::Error, |
| 22 | + message: String, |
34 | 23 | } |
35 | | -} |
36 | 24 |
|
37 | | -impl fmt::Display for VerboseError { |
38 | | - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
39 | | - write!(f, "{}", self.message) |
| 25 | + impl Error { |
| 26 | + pub(crate) fn wrap(source: io::Error, message: impl Into<String>) -> io::Error { |
| 27 | + io::Error::new( |
| 28 | + source.kind(), |
| 29 | + Error { |
| 30 | + source, |
| 31 | + message: message.into(), |
| 32 | + }, |
| 33 | + ) |
| 34 | + } |
40 | 35 | } |
41 | | -} |
42 | 36 |
|
43 | | -impl Error for VerboseError { |
44 | | - fn description(&self) -> &str { |
45 | | - self.source.description() |
| 37 | + impl fmt::Display for Error { |
| 38 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 39 | + write!(f, "{}", self.message) |
| 40 | + } |
46 | 41 | } |
47 | 42 |
|
48 | | - fn source(&self) -> Option<&(dyn Error + 'static)> { |
49 | | - Some(&self.source) |
| 43 | + impl StdError for Error { |
| 44 | + fn description(&self) -> &str { |
| 45 | + self.source.description() |
| 46 | + } |
| 47 | + |
| 48 | + fn source(&self) -> Option<&(dyn StdError + 'static)> { |
| 49 | + Some(&self.source) |
| 50 | + } |
50 | 51 | } |
51 | 52 | } |
0 commit comments