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 ;
/// Passes a service's complete call result to a second service.
///
/// Created by [`crate::ServiceChain::then`]. The second service receives
/// `Result<A::Res, A::Error>` and can transform a successful response, recover
/// from an error, or return an error of its own. Its result becomes the final
/// result. Both services use the same state and error types.
///
/// Unlike [`crate::dev::AndThen`], this invokes the second service even when
/// the first call fails.
///
/// # Readiness and lifecycle
///
/// Binding is forwarded to both children. Readiness succeeds only when both
/// children are ready. Errors from the chain's readiness check are returned
/// directly, not passed to the second service as a request. Shutdown drives
/// both children's shutdown futures to completion.
///
/// Raw children share the enclosing readiness scope, and calls forward
/// directly without an extra readiness check. Wrap a child with
/// [`crate::ready()`] when it needs an independent cache and a call-time check,
/// particularly when the second child's readiness can change while the first
/// call is awaiting.
///
/// Cloning clones both children; shared state and readiness depend on their
/// clone implementations.
///
/// # Example
///
/// ```
/// use ntex_service::{Service, fn_service, service};
///
/// # async fn example() -> Result<(), &'static str> {
/// let pipeline = service(fn_service(async |req: u32| {
/// if req == 0 { Err("missing value") } else { Ok(req) }
/// }))
/// .then(fn_service(async |result: Result<u32, &'static str>| {
/// Ok::<_, &'static str>(result.unwrap_or(10) * 2)
/// }))
/// .pipeline(());
///
/// assert_eq!(pipeline.call(3).await?, 6);
/// assert_eq!(pipeline.call(0).await?, 20);
/// # Ok(())
/// # }
/// ```
/// Creates a [`Then`] service from two service factories.
///
/// Created by [`crate::ServiceChainFactory::then`]. Both factories use the
/// same state, service-error, and initialization-error types. The second
/// factory creates services whose request is the first service's complete
/// `Result`, rather than just its successful response.
///
/// Creation is sequential with the same state reference. The second factory
/// is invoked only after the first succeeds. An initialization error from
/// either factory is returned unchanged; it is not passed to the second
/// factory or handled as a service-call result.
///
/// Created services use the call and lifecycle behavior described in [`Then`].
/// Wrap a child factory with [`crate::ready_factory()`] to give each of its
/// created services an independent readiness cache. Cloning clones both
/// factories without creating services.
///
/// # Example
///
/// ```
/// use ntex_service::{factory, fn_factory, fn_service};
///
/// # async fn example() -> Result<(), &'static str> {
/// let factory = factory(fn_factory(async |_: &()| {
/// Ok::<_, &'static str>(fn_service(async |_: u32| Err::<u32, _>("unavailable")))
/// }))
/// .then(fn_factory(async |_: &()| {
/// Ok::<_, &'static str>(fn_service(async |result: Result<u32, &'static str>| {
/// Ok::<_, &'static str>(result.unwrap_or(42))
/// }))
/// }));
///
/// let pipeline = factory.pipeline(()).await?;
/// assert_eq!(pipeline.call(1).await?, 42);
/// # Ok(())
/// # }
/// ```