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
159
160
161
162
163
use tower::layer::util::Stack;

use crate::server_prepare::PrepareDecorator;
use crate::{
    prepare_behave::effect_traits::{
        Prepare, PrepareMiddlewareEffect, PrepareRouteEffect, PrepareStateEffect,
    },
    prepare_sets::ContainerResult,
    ConcurrentPrepareSet, ServerPrepare,
};

type ServerPrepareNestRoute<C, P, Ri, Li, Log, State, Graceful, Decorator> = ServerPrepare<
    C,
    ContainerResult<(<P as Prepare<C>>::Effect, Ri), Li>,
    Log,
    State,
    Graceful,
    Decorator,
>;

type ServerPrepareNestMiddleware<C, P, Ri, Li, S, Log, State, Graceful, Decorator> = ServerPrepare<
    C,
    ContainerResult<
        Ri,
        Stack<<<P as Prepare<C>>::Effect as PrepareMiddlewareEffect<S>>::Middleware, Li>,
    >,
    Log,
    State,
    Graceful,
    Decorator,
>;

impl<C: 'static, Log, State, Graceful, Ri: 'static, Li: 'static, Decorator>
    ServerPrepare<C, ContainerResult<Ri, Li>, Log, State, Graceful, Decorator>
where
    Decorator: PrepareDecorator,
{
    /// adding a set of [Prepare] executing concurrently
    ///
    /// # Note
    ///
    /// [Prepare] set added by different [Self::prepare_concurrent] will be executed serially
    pub fn prepare_concurrent<F>(
        self,
        concurrent: F,
    ) -> ServerPrepare<C, ContainerResult<Ri, Li>, Log, State, Graceful, Decorator>
    where
        F: FnOnce(ConcurrentPrepareSet<'_, C, Decorator>) -> ConcurrentPrepareSet<'_, C, Decorator>
            + 'static,
    {
        let prepares = self.span.in_scope(|| {
            debug!(mode = "Concurrent", action = "Add Prepare");
            let decorator = self.prepares.get_decorator();
            let concurrent_set =
                ConcurrentPrepareSet::new(self.prepares.get_configure(), &*decorator);
            self.prepares.combine(concurrent(concurrent_set))
        });
        ServerPrepare::new(prepares, self.graceful, self.state, self.span)
    }

    /// adding a [Prepare] apply effect on [**Router**](axum::Router)
    ///
    /// ## Note
    ///
    /// the [Prepare] task will be executed one by one.
    ///
    /// **DO NOT** block any task for a long time, neither **sync** nor **async**
    pub fn prepare_route<P, S>(
        self,
        prepare: P,
    ) -> ServerPrepareNestRoute<C, P, Ri, Li, Log, State, Graceful, Decorator>
    where
        P: Prepare<C> + 'static,
        P::Effect: PrepareRouteEffect<S>,
        Ri: PrepareRouteEffect<S>,
        S: Clone + Send + 'static + Sync,
    {
        let prepares = self.span.in_scope(|| {
            debug!(
                mode = "Serial",
                action = "Add Prepare Route",
                prepare = core::any::type_name::<P>()
            );
            self.prepares.then_route(prepare)
        });

        ServerPrepare::new(prepares, self.graceful, self.state, self.span)
    }
    /// adding a [Prepare] adding effect on **State**
    ///
    /// ## Note
    ///
    /// the [Prepare] task will be executed one by one.
    ///
    /// **DO NOT** block any task for a long time, neither **sync** nor **async**
    pub fn prepare_state<P>(
        self,
        prepare: P,
    ) -> ServerPrepare<C, ContainerResult<Ri, Li>, Log, State, Graceful, Decorator>
    where
        P: Prepare<C> + 'static,
        P::Effect: PrepareStateEffect,
    {
        let prepares = self.span.in_scope(|| {
            debug!(
                mode = "Serial",
                action = "Add Prepare State",
                prepare = core::any::type_name::<P>()
            );
            self.prepares.then_state(prepare)
        });

        ServerPrepare::new(prepares, self.graceful, self.state, self.span)
    }

    /// adding a [Prepare] apply  effect on **State** and **Middleware**
    ///
    /// ## Note
    ///
    /// the [Prepare] task will be executed one by one.
    ///
    /// **DO NOT** block any task for a long time, neither **sync** nor **async**
    pub fn prepare_middleware<S, P>(
        self,
        prepare: P,
    ) -> ServerPrepareNestMiddleware<C, P, Ri, Li, S, Log, State, Graceful, Decorator>
    where
        S: 'static,
        P: Prepare<C> + 'static,
        P::Effect: PrepareMiddlewareEffect<S>,
    {
        let prepares = self.span.in_scope(|| {
            debug!(
                mode = "Serial",
                action = "Add Prepare Middleware",
                prepare = core::any::type_name::<P>()
            );
            self.prepares.then_middleware(prepare)
        });

        ServerPrepare::new(prepares, self.graceful, self.state, self.span)
    }

    /// adding a [Prepare] without effect
    pub fn prepare<P>(
        self,
        prepare: P,
    ) -> ServerPrepare<C, ContainerResult<Ri, Li>, Log, State, Graceful, Decorator>
    where
        P: Prepare<C, Effect = ()> + 'static,
    {
        let prepares = self.span.in_scope(|| {
            debug!(
                mode = "Serial",
                action = "Add Prepare Middleware",
                prepare = core::any::type_name::<P>()
            );
            self.prepares.then(prepare)
        });

        ServerPrepare::new(prepares, self.graceful, self.state, self.span)
    }
}