Skip to main content

wee_woo/
lib.rs

1#![no_std]
2#![doc = env!("CARGO_PKG_DESCRIPTION")]
3
4use core::{error::Error, fmt, iter};
5
6/// An extension trait for [`Error`]s
7pub trait ErrorExt {
8    /// Attach a source to the current error
9    ///
10    /// The value of `source` will be returned by the [`Error::source`] and
11    /// [`Error::cause`] implementations of the returned value. Note that the
12    /// latter is deprecated.
13    ///
14    /// See also [`ErrorExt::caused`]
15    fn with_source<Source>(
16        self,
17        source: Source,
18    ) -> ErrorWithSource<Self, Source>
19    where
20        Self: Sized;
21
22    /// Set `self` as the source of the given error
23    ///
24    /// The value of `self` will be returned by the [`Error::source`] and
25    /// [`Error::cause`] implementations of the returned value. Note that the
26    /// latter is deprecated.
27    ///
28    /// See also [`ErrorExt::with_source`]
29    fn caused<Error>(self, error: Error) -> ErrorWithSource<Error, Self>
30    where
31        Self: Sized;
32
33    /// [`Display`] an [`Error`] and its [`source`]s with a separator
34    ///
35    /// [`Display`]: fmt::Display
36    /// [`source`]: Error::source
37    fn display_with_sources<I>(&self, infix: I) -> DisplayWithSources<&Self, I>
38    where
39        Self: Sized;
40}
41
42impl<This> ErrorExt for This
43where
44    This: Error,
45{
46    fn with_source<Source>(
47        self,
48        source: Source,
49    ) -> ErrorWithSource<Self, Source>
50    where
51        Self: Sized,
52    {
53        ErrorWithSource {
54            this: self,
55            source,
56        }
57    }
58
59    fn caused<Error>(self, error: Error) -> ErrorWithSource<Error, Self>
60    where
61        Self: Sized,
62    {
63        ErrorWithSource {
64            this: error,
65            source: self,
66        }
67    }
68
69    fn display_with_sources<I>(&self, infix: I) -> DisplayWithSources<&Self, I>
70    where
71        Self: Sized,
72    {
73        DisplayWithSources {
74            error: self,
75            infix,
76        }
77    }
78}
79
80/// [`Display`] an [`Error`] and its [`source`]s with a separator
81///
82/// [`Display`]: fmt::Display
83/// [`source`]: Error::source
84pub struct DisplayWithSources<E, I> {
85    /// The error (and its sources) to write
86    pub error: E,
87
88    /// Separator to write between the error and each subsequent source
89    pub infix: I,
90}
91
92impl<E, I> fmt::Display for DisplayWithSources<E, I>
93where
94    E: Error,
95    I: fmt::Display,
96{
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        write!(f, "{}", self.error)?;
99
100        let mut source = self.error.source();
101
102        source
103            .into_iter()
104            .chain(iter::from_fn(|| {
105                source = source.and_then(Error::source);
106                source
107            }))
108            .try_for_each(|source| write!(f, "{}{source}", self.infix))
109    }
110}
111
112/// An [`Error`] with a [`source`] [`Error`]
113///
114/// This is useful for constructing ad-hoc chains of errors.
115///
116/// [`source`]: Error::source
117#[derive(Debug)]
118pub struct ErrorWithSource<This, Source> {
119    /// The current error
120    ///
121    /// Note that calling [`Error::source`] on an [`ErrorWithSource`] will not
122    /// call [`Error::source`] on [`this`]; [`source`] will always be returned
123    /// instead.
124    ///
125    /// [`this`]: ErrorWithSource#structfield.this
126    /// [`source`]: ErrorWithSource#structfield.source
127    pub this: This,
128
129    /// The error that caused the current error
130    ///
131    /// This value will be returned by [`ErrorWithSource`]'s [`Error::source`]
132    /// and [`Error::cause`] implementations. Note that the latter is
133    /// deprecated.
134    pub source: Source,
135}
136
137impl<This, Source> Error for ErrorWithSource<This, Source>
138where
139    This: fmt::Display + fmt::Debug,
140    Source: Error + 'static,
141{
142    fn source(&self) -> Option<&(dyn Error + 'static)> {
143        Some(&self.source)
144    }
145
146    fn cause(&self) -> Option<&dyn Error> {
147        Some(&self.source)
148    }
149}
150
151impl<This, Source> fmt::Display for ErrorWithSource<This, Source>
152where
153    This: fmt::Display,
154{
155    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156        self.this.fmt(f)
157    }
158}