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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use crate;
use crateParts;
use ;
/// Extractor for context.
///
/// Context is global and used in every request a router with context receives.
/// For accessing data derived from calls, see [`Extension`].
///
/// [`Extension`]: crate::extract::Extension
///
/// # With `Router`
///
/// ```
/// use blueprint_sdk::extract::Context;
/// use blueprint_sdk::{Job, Router};
///
/// // The application context
/// //
/// // Here you can put configuration, database connection pools, or whatever
/// // context you need
/// #[derive(Clone)]
/// struct AppContext {}
///
/// let context = AppContext {};
///
/// const MY_JOB_ID: u32 = 0;
///
/// // create a `Router` that holds our context
/// let app = Router::new()
/// .route(MY_JOB_ID, handler)
/// // provide the context so the router can access it
/// .with_context(context);
///
/// async fn handler(
/// // access the context via the `Context` extractor
/// // extracting a context of the wrong type results in a compile error
/// Context(context): Context<AppContext>,
/// ) {
/// // use `context`...
/// }
/// # let _: Router = app;
/// ```
///
/// Note that [`Context`] is an extractor, so be sure to put it before any body
/// extractors, see [`the order of extractors`][order-of-extractors].
///
/// [order-of-extractors]: crate::extract#the-order-of-extractors
///
/// # With `Job`
///
/// ```
/// use blueprint_sdk::Job;
/// use blueprint_sdk::extract::Context;
///
/// #[derive(Clone)]
/// struct AppContext {}
///
/// let context = AppContext {};
///
/// async fn job(Context(context): Context<AppContext>) {
/// // use `context`...
/// }
///
/// // provide the context so the job can access it
/// let job_with_context = job.with_context(context);
/// ```
///
/// # Sub-Contexts
///
/// [`Context`] only allows a single context type, but you can use [`FromRef`] to extract "sub-contexts":
///
/// ```
/// use blueprint_sdk::Router;
/// use blueprint_sdk::extract::{Context, FromRef};
///
/// // the application context
/// #[derive(Clone)]
/// struct AppContext {
/// // that holds some api specific context
/// api_state: ApiContext,
/// }
///
/// // the api specific context
/// #[derive(Clone)]
/// struct ApiContext {}
///
/// // support converting an `AppContext` in an `ApiContext`
/// impl FromRef<AppContext> for ApiContext {
/// fn from_ref(app_state: &AppContext) -> ApiContext {
/// app_state.api_state.clone()
/// }
/// }
///
/// let context = AppContext {
/// api_state: ApiContext {},
/// };
///
/// const HANDLER_JOB_ID: u32 = 0;
/// const FETCH_API_JOB_ID: u32 = 1;
///
/// let app = Router::new()
/// .route(HANDLER_JOB_ID, handler)
/// .route(FETCH_API_JOB_ID, fetch_api)
/// .with_context(context);
///
/// async fn fetch_api(
/// // access the api specific context
/// Context(api_state): Context<ApiContext>,
/// ) {
/// }
///
/// async fn handler(
/// // we can still access to top level context
/// Context(context): Context<AppContext>,
/// ) {
/// }
/// # let _: Router = app;
/// ```
///
/// For convenience `FromRef` can also be derived using `#[derive(FromRef)]`.
///
/// # For library authors
///
/// If you're writing a library that has an extractor that needs context, this is the recommended way
/// to do it:
///
/// ```rust
/// use blueprint_sdk::extract::{FromJobCallParts, FromRef};
/// use blueprint_sdk::job::call::Parts;
/// use std::convert::Infallible;
///
/// // the extractor your library provides
/// struct MyLibraryExtractor;
///
/// impl<S> FromJobCallParts<S> for MyLibraryExtractor
/// where
/// // keep `S` generic but require that it can produce a `MyLibraryContext`
/// // this means users will have to implement `FromRef<UserContext> for MyLibraryContext`
/// MyLibraryContext: FromRef<S>,
/// S: Send + Sync,
/// {
/// type Rejection = Infallible;
///
/// async fn from_job_call_parts(
/// parts: &mut Parts,
/// context: &S,
/// ) -> Result<Self, Self::Rejection> {
/// // get a `MyLibraryContext` from a reference to the context
/// let context = MyLibraryContext::from_ref(context);
///
/// // ...
/// # unimplemented!()
/// }
/// }
///
/// // the context your library needs
/// struct MyLibraryContext {
/// // ...
/// }
/// ```
///
/// # Shared mutable context
///
/// [As context is global within a `Router`][global] you can't directly get a mutable reference to
/// the context.
///
/// The most basic solution is to use an `Arc<Mutex<_>>`. Which kind of mutex you need depends on
/// your use case. See [the tokio docs] for more details.
///
/// Note that holding a locked `std::sync::Mutex` across `.await` points will result in `!Send`
/// futures which are incompatible with `blueprint_sdk`. If you need to hold a mutex across `.await` points,
/// consider using a `tokio::sync::Mutex` instead.
///
/// ## Example
///
/// ```
/// use blueprint_sdk::Router;
/// use blueprint_sdk::extract::Context;
/// use std::sync::{Arc, Mutex};
///
/// #[derive(Clone)]
/// struct AppContext {
/// data: Arc<Mutex<String>>,
/// }
///
/// const MY_JOB_ID: u8 = 0;
///
/// async fn job(Context(context): Context<AppContext>) {
/// {
/// let mut data = context.data.lock().expect("mutex was poisoned");
/// *data = "updated foo".to_owned();
/// }
///
/// // ...
/// }
///
/// let context = AppContext {
/// data: Arc::new(Mutex::new("foo".to_owned())),
/// };
///
/// let app = Router::new().route(MY_JOB_ID, job).with_context(context);
/// # let _: Router = app;
/// ```
///
/// [global]: https://docs.rs/blueprint-sdk/latest/blueprint_sdk/struct.Router.html#method.with_context
/// [the tokio docs]: https://docs.rs/tokio/1.25.0/tokio/sync/struct.Mutex.html#which-kind-of-mutex-should-you-use
;