-
Notifications
You must be signed in to change notification settings - Fork 16
/
extractors.rs
71 lines (58 loc) · 1.81 KB
/
extractors.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use poem::Request;
use std::collections::HashSet;
use std::future::Future;
use std::hash::Hash;
pub trait AuthoritiesExtractor<'a, Req, Type> {
type Future: Future<Output = poem::Result<HashSet<Type>>> + Send + Sync;
fn extract(&self, request: &'a mut Request) -> Self::Future;
}
impl<'a, F, O, Type> AuthoritiesExtractor<'a, &Request, Type> for F
where
F: Fn(&'a Request) -> O,
O: Future<Output = poem::Result<HashSet<Type>>> + Send + Sync,
Type: Eq + Hash + 'static,
{
type Future = O;
fn extract(&self, req: &'a mut Request) -> Self::Future {
(self)(req)
}
}
impl<'a, F, O, Type> AuthoritiesExtractor<'a, &mut Request, Type> for F
where
F: Fn(&'a mut Request) -> O,
O: Future<Output = poem::Result<HashSet<Type>>> + Send + Sync,
Type: Eq + Hash + 'static,
{
type Future = O;
fn extract(&self, req: &'a mut Request) -> Self::Future {
(self)(req)
}
}
#[cfg(test)]
mod tests {
use super::*;
async fn extract(_req: &Request) -> poem::Result<HashSet<String>> {
Ok(HashSet::from(["TEST_PERMISSION".to_string()]))
}
#[tokio::test]
async fn test_fn_extractor_impl() {
let req = Request::default();
let authorities = extract(&req).await;
authorities
.unwrap()
.iter()
.for_each(|perm| assert_eq!("TEST_PERMISSION", perm.as_str()));
}
async fn mut_extract(_req: &mut Request) -> poem::Result<HashSet<String>> {
Ok(HashSet::from(["TEST_PERMISSION".to_string()]))
}
#[tokio::test]
async fn test_fn_mut_extractor_impl() {
let mut req = Request::default();
let authorities = mut_extract(&mut req).await;
authorities
.unwrap()
.iter()
.for_each(|perm| assert_eq!("TEST_PERMISSION", perm.as_str()));
}
}