resplus_impl/
sync.rs

1use std::borrow::Cow;
2
3use crate::ErrorChain;
4
5pub trait ResultChain<I, T, E, D> {
6    fn about(self, desc: D) -> Result<T, ErrorChain<I>>
7    where
8        Self: Sized,
9        E: Into<I>,
10        D: Into<Cow<'static, str>>;
11    fn about_else(self, f: impl FnOnce() -> D) -> Result<T, ErrorChain<I>>
12    where
13        Self: Sized,
14        E: Into<I>,
15        D: Into<Cow<'static, str>>;
16}
17
18impl<I, T, E> ResultChain<I, T, E, &'static str> for std::result::Result<T, E> {
19    fn about(self, desc: &'static str) -> Result<T, ErrorChain<I>>
20    where
21        Self: Sized,
22        E: Into<I>,
23    {
24        self.map_err(|e| ErrorChain {
25            source: e.into(),
26            context: vec![desc.into()],
27        })
28    }
29    fn about_else(self, f: impl FnOnce() -> &'static str) -> Result<T, ErrorChain<I>>
30    where
31        Self: Sized,
32        E: Into<I>,
33    {
34        self.map_err(|e| ErrorChain {
35            source: e.into(),
36            context: vec![f().into()],
37        })
38    }
39}
40
41impl<I, T, E> ResultChain<I, T, E, String> for std::result::Result<T, E> {
42    fn about(self, desc: String) -> Result<T, ErrorChain<I>>
43    where
44        Self: Sized,
45        E: Into<I>,
46    {
47        self.map_err(|e| ErrorChain {
48            source: e.into(),
49            context: vec![desc.into()],
50        })
51    }
52    fn about_else(self, f: impl FnOnce() -> String) -> Result<T, ErrorChain<I>>
53    where
54        Self: Sized,
55        E: Into<I>,
56    {
57        self.map_err(|e| ErrorChain {
58            source: e.into(),
59            context: vec![f().into()],
60        })
61    }
62}
63//
64// pub trait ResultChainAttach<T, D> {
65//     fn attach(self, desc: D) -> Result<T, ErrorChain<I>>
66//     where
67//         Self: Sized,
68//         D: Into<Cow<'static, str>>;
69//     fn attach_else(self, f: impl FnOnce() -> D) -> Result<T, ErrorChain<I>>
70//     where
71//         Self: Sized,
72//         D: Into<Cow<'static, str>>;
73// }
74
75#[cfg(test)]
76mod tests {
77    use super::ResultChain;
78    use crate as resplus;
79    use crate::tests::{about, about_else};
80    use test_util::*;
81
82    #[test]
83    fn about() {
84        assert_result!(about!(f0()), "source: Error\n  source");
85        assert_result!(about!(f1(1)), "source: Error\n  source");
86        assert_result!(about!(f2(1, 1)), "source: Error\n  source");
87    }
88
89    #[test]
90    fn about_else() {
91        assert_result!(about_else!(f0()), "source: Error\n  source");
92        assert_result!(about_else!(f1(1)), "source: Error\n  source");
93        assert_result!(about_else!(f2(1, 1)), "source: Error\n  source");
94    }
95    //
96    // #[test]
97    // fn attach() {
98    //     assert_result!(attach!(about!(f0())), "source: Error\n  source\n  attach");
99    //     assert_result!(attach!(about!(f1(1))), "source: Error\n  source\n  attach");
100    //     assert_result!(
101    //         attach!(about!(f2(1, 1))),
102    //         "source: Error\n  source\n  attach"
103    //     );
104    // }
105    //
106    // #[test]
107    // fn attach_else() {
108    //     assert_result!(
109    //         attach_else!(about_else!(f0())),
110    //         "source: Error\n  source\n  attach"
111    //     );
112    //     assert_result!(
113    //         attach_else!(about_else!(f1(1))),
114    //         "source: Error\n  source\n  attach"
115    //     );
116    //     assert_result!(
117    //         attach_else!(about_else!(f2(1, 1))),
118    //         "source: Error\n  source\n  attach"
119    //     );
120    // }
121}