forked from async-graphql/async-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguard.rs
66 lines (55 loc) · 1.94 KB
/
guard.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
//! Field guards
use crate::{Context, FieldResult};
use serde::export::PhantomData;
/// Field guard
///
/// Guard is a pre-condition for a field that is resolved if `Ok(()` is returned, otherwise an error is returned.
#[async_trait::async_trait]
pub trait Guard {
#[allow(missing_docs)]
async fn check(&self, ctx: &Context<'_>) -> FieldResult<()>;
}
/// An extension trait for `Guard`
pub trait GuardExt: Guard + Sized {
/// Merge the two guards.
fn and<R: Guard>(self, other: R) -> And<Self, R> {
And(self, other)
}
}
impl<T: Guard> GuardExt for T {}
/// Guard for `GuardExt::and`
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<'_>) -> FieldResult<()> {
self.0.check(ctx).await?;
self.1.check(ctx).await
}
}
/// Field post guard
///
/// Guard is a post-condition for a field that is resolved if `Ok(()` is returned, otherwise an error is returned.
#[async_trait::async_trait]
pub trait PostGuard<T: Send + Sync> {
#[allow(missing_docs)]
async fn check(&self, ctx: &Context<'_>, result: &T) -> FieldResult<()>;
}
/// An extension trait for `PostGuard<T>`
pub trait PostGuardExt<T: Send + Sync>: PostGuard<T> + Sized {
/// Merge the two guards.
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 {}
/// PostGuard for `PostGuardExt<T>::and`
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) -> FieldResult<()> {
self.0.check(ctx, result).await?;
self.1.check(ctx, result).await
}
}