Commit
+38 -25 +/-3 browse
1 | diff --git a/maitred/src/expand.rs b/maitred/src/expand.rs |
2 | index 7a75201..232eb21 100644 |
3 | --- a/maitred/src/expand.rs |
4 | +++ b/maitred/src/expand.rs |
5 | @@ -1,4 +1,4 @@ |
6 | - use std::result::Result as StdResult; |
7 | + use std::{future::Future, result::Result as StdResult}; |
8 | |
9 | use async_trait::async_trait; |
10 | use email_address::EmailAddress; |
11 | @@ -35,23 +35,27 @@ pub trait Expansion { |
12 | /// use maitred::ExpansionFunc; |
13 | /// |
14 | /// let my_expn_fn = ExpansionFunc(|name: &str| { |
15 | - /// Ok(vec![ |
16 | - /// EmailAddress::new_unchecked("fuu@bar.com"), |
17 | - /// EmailAddress::new_unchecked("baz@qux.com") |
18 | - /// ]) |
19 | + /// async move { |
20 | + /// Ok(vec![ |
21 | + /// EmailAddress::new_unchecked("fuu@bar.com"), |
22 | + /// EmailAddress::new_unchecked("baz@qux.com") |
23 | + /// ]) |
24 | + /// } |
25 | /// }); |
26 | /// ``` |
27 | - pub struct Func<F>(pub F) |
28 | + pub struct Func<F, T>(pub F) |
29 | where |
30 | - F: Fn(&str) -> Result; |
31 | + F: Fn(&str) -> T + Sync, |
32 | + T: Future<Output = Result> + Send; |
33 | |
34 | #[async_trait] |
35 | - impl<F> Expansion for Func<F> |
36 | + impl<F, T> Expansion for Func<F, T> |
37 | where |
38 | - F: Fn(&str) -> Result + Sync, |
39 | + F: Fn(&str) -> T + Sync, |
40 | + T: Future<Output = Result> + Send, |
41 | { |
42 | async fn expand(&self, name: &str) -> Result { |
43 | - let f = &self.0; |
44 | - f(name) |
45 | + let f = (self.0)(name); |
46 | + f.await |
47 | } |
48 | } |
49 | diff --git a/maitred/src/session.rs b/maitred/src/session.rs |
50 | index 17ebb65..18fc81f 100644 |
51 | --- a/maitred/src/session.rs |
52 | +++ b/maitred/src/session.rs |
53 | @@ -562,11 +562,14 @@ mod test { |
54 | Session::default().with_options( |
55 | Options::default() |
56 | .list_expansion(crate::expand::Func(|name: &str| { |
57 | - assert!(name == "mailing-list"); |
58 | - Ok(vec![ |
59 | - EmailAddress::new_unchecked("Fuu <fuu@bar.com>"), |
60 | - EmailAddress::new_unchecked("Baz <baz@qux.com>"), |
61 | - ]) |
62 | + let name = name.to_string(); |
63 | + async move { |
64 | + assert!(name == "mailing-list"); |
65 | + Ok(vec![ |
66 | + EmailAddress::new_unchecked("Fuu <fuu@bar.com>"), |
67 | + EmailAddress::new_unchecked("Baz <baz@qux.com>"), |
68 | + ]) |
69 | + } |
70 | })) |
71 | .into(), |
72 | ), |
73 | @@ -607,8 +610,12 @@ mod test { |
74 | Session::default().with_options( |
75 | Options::default() |
76 | .verification(crate::verify::Func(|addr: &EmailAddress| { |
77 | - assert!(addr.email() == "bar@baz.com"); |
78 | - Ok(()) |
79 | + let addr = addr.clone(); |
80 | + |
81 | + async move { |
82 | + assert!(addr.email() == "bar@baz.com"); |
83 | + Ok(()) |
84 | + } |
85 | })) |
86 | .into(), |
87 | ), |
88 | diff --git a/maitred/src/verify.rs b/maitred/src/verify.rs |
89 | index e4efb1c..090c523 100644 |
90 | --- a/maitred/src/verify.rs |
91 | +++ b/maitred/src/verify.rs |
92 | @@ -1,4 +1,4 @@ |
93 | - use std::result::Result as StdResult; |
94 | + use std::{future::Future, result::Result as StdResult}; |
95 | |
96 | use async_trait::async_trait; |
97 | use email_address::EmailAddress; |
98 | @@ -34,17 +34,19 @@ pub trait Verify { |
99 | } |
100 | |
101 | /// Helper wrapper implementing the Verify trait. |
102 | - pub struct Func<F>(pub F) |
103 | + pub struct Func<F, T>(pub F) |
104 | where |
105 | - F: Fn(&EmailAddress) -> Result; |
106 | + F: Fn(&EmailAddress) -> T + Sync, |
107 | + T: Future<Output = Result> + Send; |
108 | |
109 | #[async_trait] |
110 | - impl<F> Verify for Func<F> |
111 | + impl<F, T> Verify for Func<F, T> |
112 | where |
113 | - F: Fn(&EmailAddress) -> Result + Sync, |
114 | + F: Fn(&EmailAddress) -> T + Sync, |
115 | + T: Future<Output = Result> + Send, |
116 | { |
117 | async fn verify(&self, address: &EmailAddress) -> Result { |
118 | - let f = &self.0; |
119 | - f(address) |
120 | + let f = (self.0)(address); |
121 | + f.await |
122 | } |
123 | } |