1use std::borrow::Cow;
2
3use async_trait::async_trait;
4
5use crate::{ErrorChain, ResultChain};
6
7#[async_trait]
8pub trait FutResultChain<I, T, E, D> {
9 async fn about(self, desc: D) -> Result<T, ErrorChain<I>>
10 where
11 Self: Sized,
12 E: Into<I>,
13 D: Into<Cow<'static, str>>;
14 async fn about_else(self, f: impl FnOnce() -> D + Send) -> Result<T, ErrorChain<I>>
15 where
16 Self: Sized,
17 E: Into<I>,
18 D: Into<Cow<'static, str>>;
19}
20
21#[async_trait]
22impl<I, T, E, F> FutResultChain<I, T, E, &'static str> for F
23where
24 F: Future<Output = std::result::Result<T, E>> + Send,
25{
26 async fn about(self, desc: &'static str) -> Result<T, ErrorChain<I>>
27 where
28 Self: Sized,
29 E: Into<I>,
30 {
31 self.await.about(desc)
32 }
33 async fn about_else(self, f: impl FnOnce() -> &'static str + Send) -> Result<T, ErrorChain<I>>
34 where
35 Self: Sized,
36 E: Into<I>,
37 {
38 self.await.about_else(f)
39 }
40}
41
42#[async_trait]
43impl<I, T, E, F> FutResultChain<I, T, E, String> for F
44where
45 F: Future<Output = std::result::Result<T, E>> + Send,
46{
47 async fn about(self, desc: String) -> Result<T, ErrorChain<I>>
48 where
49 Self: Sized,
50 E: Into<I>,
51 {
52 self.await.about(desc)
53 }
54 async fn about_else(self, f: impl FnOnce() -> String + Send) -> Result<T, ErrorChain<I>>
55 where
56 Self: Sized,
57 E: Into<I>,
58 {
59 self.await.about_else(f)
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::FutResultChain;
66 use crate as resplus;
67 use crate::tests::{about, about_else};
68 use test_util::*;
69
70 #[tokio::test]
71 async fn about() {
72 async_assert_result!(about!(af0()), "source: Error\n source");
73 async_assert_result!(about!(af1(1)), "source: Error\n source");
74 async_assert_result!(about!(af2(1, 1)), "source: Error\n source");
75 }
76
77 #[tokio::test]
78 async fn about_else() {
79 async_assert_result!(about_else!(af0()), "source: Error\n source");
80 async_assert_result!(about_else!(af1(1)), "source: Error\n source");
81 async_assert_result!(about_else!(af2(1, 1)), "source: Error\n source");
82 }
83 }