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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! Macros for [`blueprint_sdk`].
//!
//! [`blueprint_sdk`]: https://crates.io/crates/blueprint_sdk
use FunctionKind;
use TokenStream;
use ;
use ;
/// A procedural macro that outputs the [JSON ABI] for the given file path.
///
/// [JSON ABI]: https://docs.ethers.org/v5/api/utils/abi/formats/#abi-formats--solidity
/// Generates better error messages when applied to job functions.
///
/// While using [`blueprint_sdk`], you can get long error messages for simple mistakes. For example:
///
/// ```compile_fail
/// use blueprint_sdk::Router;
///
/// #[tokio::main]
/// async fn main() {
/// Router::<()>::new().route(0, job);
/// }
///
/// fn job() -> &'static str {
/// "Hello, world"
/// }
/// ```
///
/// You will get a long error message about function not implementing [`Job`] trait. But why
/// does this function not implement it? To figure it out, the [`debug_job`] macro can be used.
///
/// ```compile_fail
/// # use blueprint_sdk::Router;
/// # use blueprint_sdk::macros::debug_job;
/// #
/// # #[tokio::main]
/// # async fn main() {
/// # Router::new().route(0, job);
/// # }
/// #
/// #[debug_job]
/// fn job() -> &'static str {
/// "Hello, world"
/// }
/// ```
///
/// ```text
/// error: jobs must be async functions
/// --> main.rs:xx:1
/// |
/// xx | fn job() -> &'static str {
/// | ^^
/// ```
///
/// As the error message says, job function needs to be async.
///
/// ```no_run
/// use blueprint_sdk::{Router, macros::debug_job};
///
/// #[tokio::main]
/// async fn main() {
/// Router::<()>::new().route(0, job);
/// }
///
/// #[debug_job]
/// async fn job() -> &'static str {
/// "Hello, world"
/// }
/// ```
///
/// # Changing context type
///
/// By default `#[debug_job]` assumes your context type is `()` unless your job has a
/// [`blueprint_sdk::Context`] argument:
///
/// ```
/// use blueprint_sdk::extract::Context;
/// use blueprint_sdk::macros::debug_job;
///
/// #[debug_job]
/// async fn job(
/// // this makes `#[debug_job]` use `AppContext`
/// Context(context): Context<AppContext>,
/// ) {
/// }
///
/// #[derive(Clone)]
/// struct AppContext {}
/// ```
///
/// If your job takes multiple [`blueprint_sdk::Context`] arguments or you need to otherwise
/// customize the context type you can set it with `#[debug_job(context = ...)]`:
///
/// ```
/// use blueprint_sdk::extract::Context;
/// use blueprint_sdk::{extract::FromRef, macros::debug_job};
///
/// #[debug_job(context = AppContext)]
/// async fn job(Context(app_ctx): Context<AppContext>, Context(inner_ctx): Context<InnerContext>) {
/// }
///
/// #[derive(Clone)]
/// struct AppContext {
/// inner: InnerContext,
/// }
///
/// #[derive(Clone)]
/// struct InnerContext {}
///
/// impl FromRef<AppContext> for InnerContext {
/// fn from_ref(context: &AppContext) -> Self {
/// context.inner.clone()
/// }
/// }
/// ```
///
/// # Limitations
///
/// This macro does not work for functions in an `impl` block that don't have a `self` parameter:
///
/// ```compile_fail
/// use blueprint_sdk::macros::debug_job;
/// use blueprint_tangle_extra::extract::TangleArg;
///
/// struct App {}
///
/// impl App {
/// #[debug_job]
/// async fn my_job(TangleArg(_): TangleArg<u64>) {}
/// }
/// ```
///
/// This will yield an error similar to this:
///
/// ```text
/// error[E0425]: cannot find function `__blueprint_macros_check_job_0_from_job_call_check` in this scope
// --> src/main.rs:xx:xx
// |
// xx | pub async fn my_job(TangleArg(_): TangleArg<u64>) {}
// | ^^^^ not found in this scope
/// ```
/// # Performance
///
/// This macro has no effect when compiled with the release profile. (eg. `cargo build --release`)
///
/// [`blueprint_sdk`]: https://docs.rs/blueprint_sdk/0.1
/// [`Job`]: https://docs.rs/blueprint_sdk/0.1/blueprint_sdk/job/trait.Job.html
/// [`blueprint_sdk::Context`]: https://docs.rs/blueprint_sdk/0.1/blueprint_sdk/context/struct.Context.html
/// [`debug_job`]: macro@debug_job
/// Derive an implementation of [`FromRef`] for each field in a struct.
///
/// # Example
///
/// ```
/// use blueprint_sdk::{
/// Router,
/// extract::{Context, FromRef},
/// };
///
/// #
/// # type Keystore = String;
/// # type DatabasePool = ();
/// #
/// // This will implement `FromRef` for each field in the struct.
/// #[derive(FromRef, Clone)]
/// struct AppContext {
/// keystore: Keystore,
/// database_pool: DatabasePool,
/// // fields can also be skipped
/// #[from_ref(skip)]
/// openai_api_token: String,
/// }
///
/// // So those types can be extracted via `Context`
/// async fn my_job(Context(keystore): Context<Keystore>) {}
///
/// async fn other_job(Context(database_pool): Context<DatabasePool>) {}
///
/// # let keystore = Default::default();
/// # let database_pool = Default::default();
/// let ctx = AppContext {
/// keystore,
/// database_pool,
/// openai_api_token: "sk-secret".to_owned(),
/// };
///
/// let router = Router::new()
/// .route(0, my_job)
/// .route(1, other_job)
/// .with_context(ctx);
/// # let _: Router = router;
/// ```
///
/// [`FromRef`]: https://docs.rs/blueprint-sdk/0.1/blueprint_sdk/extract/trait.FromRef.html
+ 'a
where
I: + 'a,