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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::{
    di::{Asyncify, Injectable, Insert},
    Handler, HandlerDescription,
};

impl<'a, Input, Output, Descr> Handler<'a, Input, Output, Descr>
where
    Input: Send + 'a,
    Output: 'a,
    Descr: HandlerDescription,
{
    /// Chain this handler with the filter predicate `pred`.
    #[must_use]
    #[track_caller]
    pub fn filter<Pred, FnArgs>(self, pred: Pred) -> Handler<'a, Input, Output, Descr>
    where
        Asyncify<Pred>: Injectable<Input, bool, FnArgs> + Send + Sync + 'a,
    {
        self.chain(crate::filter(pred))
    }

    /// Chain this handler with the async filter predicate `pred`.
    #[must_use]
    #[track_caller]
    pub fn filter_async<Pred, FnArgs>(self, pred: Pred) -> Handler<'a, Input, Output, Descr>
    where
        Pred: Injectable<Input, bool, FnArgs> + Send + Sync + 'a,
    {
        self.chain(crate::filter_async(pred))
    }

    /// Chain this handler with the filter projection `proj`.
    #[must_use]
    #[track_caller]
    pub fn filter_map<Proj, NewType, Args>(self, proj: Proj) -> Handler<'a, Input, Output, Descr>
    where
        Input: Insert<NewType> + Clone,
        Asyncify<Proj>: Injectable<Input, Option<NewType>, Args> + Send + Sync + 'a,
        NewType: Send,
    {
        self.chain(crate::filter_map(proj))
    }

    /// Chain this handler with the async filter projection `proj`.
    #[must_use]
    #[track_caller]
    pub fn filter_map_async<Proj, NewType, Args>(
        self,
        proj: Proj,
    ) -> Handler<'a, Input, Output, Descr>
    where
        Input: Insert<NewType> + Clone,
        Proj: Injectable<Input, Option<NewType>, Args> + Send + Sync + 'a,
        NewType: Send,
    {
        self.chain(crate::filter_map_async(proj))
    }

    /// Chain this handler with the map projection `proj`.
    #[must_use]
    #[track_caller]
    pub fn map<Proj, NewType, Args>(self, proj: Proj) -> Handler<'a, Input, Output, Descr>
    where
        Input: Insert<NewType> + Clone,
        Asyncify<Proj>: Injectable<Input, NewType, Args> + Send + Sync + 'a,
        NewType: Send,
    {
        self.chain(crate::map(proj))
    }

    /// Chain this handler with the async map projection `proj`.
    #[must_use]
    #[track_caller]
    pub fn map_async<Proj, NewType, Args>(self, proj: Proj) -> Handler<'a, Input, Output, Descr>
    where
        Input: Insert<NewType> + Clone,
        Proj: Injectable<Input, NewType, Args> + Send + Sync + 'a,
        NewType: Send,
    {
        self.chain(crate::map_async(proj))
    }

    /// Chain this handler with the inspection function `f`.
    #[must_use]
    #[track_caller]
    pub fn inspect<F, Args>(self, f: F) -> Handler<'a, Input, Output, Descr>
    where
        Asyncify<F>: Injectable<Input, (), Args> + Send + Sync + 'a,
    {
        self.chain(crate::inspect(f))
    }

    /// Chain this handler with the async inspection function `f`.
    #[must_use]
    #[track_caller]
    pub fn inspect_async<F, Args>(self, f: F) -> Handler<'a, Input, Output, Descr>
    where
        F: Injectable<Input, (), Args> + Send + Sync + 'a,
    {
        self.chain(crate::inspect_async(f))
    }

    /// Chain this handler with the endpoint handler `f`.
    #[must_use]
    #[track_caller]
    pub fn endpoint<F, FnArgs>(self, f: F) -> Handler<'a, Input, Output, Descr>
    where
        F: Injectable<Input, Output, FnArgs> + Send + Sync + 'a,
    {
        self.chain(crate::endpoint(f))
    }
}

#[cfg(test)]
mod tests {
    use std::ops::ControlFlow;

    use crate::{deps, help_inference};

    // Test that these methods just do compile.
    #[tokio::test]
    async fn test_methods() {
        let value = 42;

        let _: ControlFlow<(), _> =
            help_inference(crate::entry()).filter(|| true).dispatch(deps![value]).await;

        let _: ControlFlow<(), _> = help_inference(crate::entry())
            .filter_async(|| async { true })
            .dispatch(deps![value])
            .await;

        let _: ControlFlow<(), _> =
            help_inference(crate::entry()).filter_map(|| Some("abc")).dispatch(deps![value]).await;

        let _: ControlFlow<(), _> = help_inference(crate::entry())
            .filter_map_async(|| async { Some("abc") })
            .dispatch(deps![value])
            .await;

        let _: ControlFlow<(), _> =
            help_inference(crate::entry()).map(|| "abc").dispatch(deps![value]).await;

        let _: ControlFlow<(), _> = help_inference(crate::entry())
            .map_async(|| async { "abc" })
            .dispatch(deps![value])
            .await;

        let _: ControlFlow<(), _> =
            help_inference(crate::entry()).inspect(|| {}).dispatch(deps![value]).await;

        let _: ControlFlow<(), _> =
            help_inference(crate::entry()).inspect_async(|| async {}).dispatch(deps![value]).await;

        let _: ControlFlow<(), _> =
            help_inference(crate::entry()).endpoint(|| async {}).dispatch(deps![value]).await;
    }
}