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::with_context(e, desc))
25    }
26    fn about_else(self, f: impl FnOnce() -> &'static str) -> Result<T, ErrorChain<I>>
27    where
28        Self: Sized,
29        E: Into<I>,
30    {
31        self.map_err(|e| ErrorChain::with_context(e, f()))
32    }
33}
34
35impl<I, T, E> ResultChain<I, T, E, String> for std::result::Result<T, E> {
36    fn about(self, desc: String) -> Result<T, ErrorChain<I>>
37    where
38        Self: Sized,
39        E: Into<I>,
40    {
41        self.map_err(|e| ErrorChain::with_context(e, desc))
42    }
43    fn about_else(self, f: impl FnOnce() -> String) -> Result<T, ErrorChain<I>>
44    where
45        Self: Sized,
46        E: Into<I>,
47    {
48        self.map_err(|e| ErrorChain::with_context(e, f()))
49    }
50}
51//
52// pub trait ResultChainAttach<T, D> {
53//     fn attach(self, desc: D) -> Result<T, ErrorChain<I>>
54//     where
55//         Self: Sized,
56//         D: Into<Cow<'static, str>>;
57//     fn attach_else(self, f: impl FnOnce() -> D) -> Result<T, ErrorChain<I>>
58//     where
59//         Self: Sized,
60//         D: Into<Cow<'static, str>>;
61// }
62
63#[cfg(test)]
64mod tests {
65    use super::ResultChain;
66    use crate as resplus;
67    use crate::tests::{about, about_else};
68    use test_util::*;
69
70    #[test]
71    fn about() {
72        assert_result!(about!(f0()), "source: Error\n  source");
73        assert_result!(about!(f1(1)), "source: Error\n  source");
74        assert_result!(about!(f2(1, 1)), "source: Error\n  source");
75    }
76
77    #[test]
78    fn about_else() {
79        assert_result!(about_else!(f0()), "source: Error\n  source");
80        assert_result!(about_else!(f1(1)), "source: Error\n  source");
81        assert_result!(about_else!(f2(1, 1)), "source: Error\n  source");
82    }
83    //
84    // #[test]
85    // fn attach() {
86    //     assert_result!(attach!(about!(f0())), "source: Error\n  source\n  attach");
87    //     assert_result!(attach!(about!(f1(1))), "source: Error\n  source\n  attach");
88    //     assert_result!(
89    //         attach!(about!(f2(1, 1))),
90    //         "source: Error\n  source\n  attach"
91    //     );
92    // }
93    //
94    // #[test]
95    // fn attach_else() {
96    //     assert_result!(
97    //         attach_else!(about_else!(f0())),
98    //         "source: Error\n  source\n  attach"
99    //     );
100    //     assert_result!(
101    //         attach_else!(about_else!(f1(1))),
102    //         "source: Error\n  source\n  attach"
103    //     );
104    //     assert_result!(
105    //         attach_else!(about_else!(f2(1, 1))),
106    //         "source: Error\n  source\n  attach"
107    //     );
108    // }
109}