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
use crate::{Context, Result};
use serde::export::PhantomData;
#[async_trait::async_trait]
pub trait Guard {
async fn check(&self, ctx: &Context<'_>) -> Result<()>;
}
pub trait GuardExt: Guard + Sized {
fn and<R: Guard>(self, other: R) -> And<Self, R> {
And(self, other)
}
}
impl<T: Guard> GuardExt for T {}
pub struct And<A: Guard, B: Guard>(A, B);
#[async_trait::async_trait]
impl<A: Guard + Send + Sync, B: Guard + Send + Sync> Guard for And<A, B> {
async fn check(&self, ctx: &Context<'_>) -> Result<()> {
self.0.check(ctx).await?;
self.1.check(ctx).await
}
}
#[async_trait::async_trait]
pub trait PostGuard<T: Send + Sync> {
async fn check(&self, ctx: &Context<'_>, result: &T) -> Result<()>;
}
pub trait PostGuardExt<T: Send + Sync>: PostGuard<T> + Sized {
fn and<R: PostGuard<T>>(self, other: R) -> PostAnd<T, Self, R> {
PostAnd(self, other, PhantomData)
}
}
impl<T: PostGuard<R>, R: Send + Sync> PostGuardExt<R> for T {}
pub struct PostAnd<T: Send + Sync, A: PostGuard<T>, B: PostGuard<T>>(A, B, PhantomData<T>);
#[async_trait::async_trait]
impl<T: Send + Sync, A: PostGuard<T> + Send + Sync, B: PostGuard<T> + Send + Sync> PostGuard<T>
for PostAnd<T, A, B>
{
async fn check(&self, ctx: &Context<'_>, result: &T) -> Result<()> {
self.0.check(ctx, result).await?;
self.1.check(ctx, result).await
}
}