resplus_impl/
lib.rs

1#[cfg(feature = "async")]
2mod fut;
3#[cfg(feature = "async")]
4pub use fut::FutResultChain;
5mod sync;
6use std::{borrow::Cow, fmt::Display};
7pub use sync::ResultChain;
8
9#[derive(Debug)]
10pub struct ErrorChain<I> {
11    pub source: I,
12    pub context: Vec<Cow<'static, str>>,
13}
14
15impl<I> ErrorChain<I> {
16    pub fn new(source: impl Into<I>) -> Self {
17        Self {
18            source: source.into(),
19            context: Vec::new(),
20        }
21    }
22}
23
24impl<I: Display> Display for ErrorChain<I> {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        write!(f, "source: {}", self.source)?;
27        for (i, c) in self.context.iter().enumerate() {
28            write!(f, "\n {} {}", i, c)?;
29        }
30        Ok(())
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    macro_rules! about {
37        ($e:expr) => {
38            ($e).about("source")
39        };
40    }
41    pub(crate) use about;
42    macro_rules! about_else {
43        ($e:expr) => {
44            ($e).about_else(|| "source")
45        };
46    }
47    pub(crate) use about_else;
48}