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