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
//! Macros for `ulo`. Each expands to calls on the core's value-level API, which integration
//! crates call directly; nothing here does what that API cannot.
//!
//! Errors are `syn::Error`s spanned on the offending token, and per-field and per-parameter
//! assertions are emitted with `quote_spanned!`, so a compile error points at the field,
//! parameter or attribute rather than at the macro invocation.
use TokenStream;
/// A type the container builds and injects into others: a provider, listed under a module's
/// `providers`.
///
/// On a struct, every field is an injection point except one marked `#[injectable(default)]`,
/// which is set from `Default`. On an impl block, the constructor is the fn named `new`, or the
/// one marked `#[construct]`; its parameters are injection points, it may be `async`, and it may
/// return `Self` or a `Result<Self, E>`. Both marks are read and removed, and a hidden import of
/// each written beside the item lets an editor answer hover and go-to-definition on it. A mark is
/// written bare or under `ulo`, `#[ulo::construct]`; any other qualifier is a compile error at it.
///
/// Arguments: a scope, `singleton`, `execution` or `transient` (none means `Auto`), and
/// `timeout = <Duration expr>`, which writes `CONSTRUCT_TIMEOUT`. `Construct::hooks` is filled by
/// probing the five lifecycle traits.
///
/// A type whose handlers a transport calls is a controller and takes `#[controller]` instead: an
/// `#[injectable]` type is not a `Controller`, so `#[handlers]` on it and `#[module]`'s
/// `controllers` both refuse it at compile time.
/// A controller: a type the container builds and its transports reach, through the handlers of
/// its `#[handlers]` impl. Only another controller injects one; `wire()` refuses a read of a
/// controller by any other binding.
///
/// It writes what `#[injectable]` writes, from a struct (`#[controller(default)]` marks a field
/// set from `Default`) or from an impl block's `new` or `#[construct]` fn, and takes the same
/// arguments: a scope and `timeout = <Duration expr>`. With no scope it is `Auto`: a singleton if
/// nothing below it needs an execution, and built per call otherwise. It takes no route prefix;
/// the module mounts a controller under one with `m.controller::<T>().at(prefix)`.
///
/// It also writes `impl ulo::Controller`, the mark that makes a type a controller, holding where
/// the type implements `ulo::Handlers`, the impl `#[handlers]` writes, so a `#[controller]` type
/// with no handler impl fails to compile here. `#[module]` accepts under `controllers` only a
/// type implementing `Handlers` and refuses a `Controller` under `providers`, and `wire()` refuses
/// a controller registered as a provider. A WebSocket gateway is a controller and takes
/// `#[controller]` too. A `#[controller]` type whose handlers are mounted by hand, a gateway
/// implementing `Gateway` among them, implements `Handlers` itself.
///
/// Rust resolves the attribute's path, and any path that reaches it is accepted, a re-export or
/// a renamed dependency's name among them. The code it writes names `::ulo`: a crate depending on
/// `ulo` under another name aliases it at its root, `extern crate framework as ulo;`. The same
/// holds for `#[injectable]` and `#[handlers]`; the markers they read are written bare or under
/// `ulo`.
///
/// On an impl block it stacks with `#[handlers]` in either order, so the constructor and the
/// handlers can share one impl, `#[controller] #[handlers] impl Greeter { fn new(..) .. }`.
/// Marks the constructor inside an `#[injectable]` or `#[controller]` impl when it is not named
/// `new`; the attribute on the impl reads and removes it. Anywhere else it is an error.
/// A module: `imports`, `providers`, `controllers`, `exports` and `global`, lowered to a
/// `Module` impl whose `register` calls the value API. A struct with fields is a configured
/// module, identified by value, and every `Secret<_>` field is registered for redaction.
///
/// Provider entries: `Type` (`provide::<Type>()`), `Type as dyn Trait` (`also_as`), `expr?`
/// (`try_value`), `expr` (`value`), `with = |..| ..` (`with`, or `try_with` when the closure's
/// output is a `Result`), `with(<scope>) = |..| ..` (`singleton`, `execution` or `transient`, or
/// the `try_` form), a bare closure (shorthand for `with = closure`), `K: with = |..| ..` and
/// `K: with(<scope>) = |..| ..` (the same binding, also bound under the trait key `K` with
/// `also_as::<K>(|a| a)`), and `into dyn Trait: [..]` (contributions). A bare path reads as a
/// type, so a constant is bound by value with a block, `{ LIMITS }`. Export entries: `Type` and
/// `reexport Type`. A `controllers` entry implements `Handlers`, and through it `Controller`, by
/// `#[controller]` and `#[handlers]` or by hand, and an entry under `providers`, by type or by
/// value, implements no `Controller`; each mistake is a compile
/// error at the entry. A closure under `providers` is not checked. A controller may be exported,
/// for another controller to read.
///
/// A qualified binding is written with the `@` that reports print for a key. `Type @ Q`
/// (`provide::<Type>().qualified::<Q>()`), `Type as dyn Trait @ Q` (both keys qualified), `expr @ Q`
/// and `expr? @ Q` (`value` and `try_value`, then `.qualified::<Q>()`) take it in `providers`;
/// `Type @ Q` and `reexport Type @ Q` take it in `exports` (`export_qualified`,
/// `reexport_qualified`). A closure binding and an `into` item take none, and a second `@` on an
/// entry is refused:
///
/// ```
/// use ulo::{App, Dep, ExecOptions, injectable, module};
///
/// struct Primary;
/// struct Replica;
///
/// #[derive(Debug, PartialEq)]
/// struct Db(&'static str);
///
/// #[module(
/// providers = [Db("primary") @ Primary, Db("replica") @ Replica],
/// exports = [Db @ Primary, Db @ Replica],
/// )]
/// struct DbModule;
///
/// #[injectable]
/// struct Writer {
/// db: Dep<Db, Primary>,
/// }
///
/// #[module(imports = [DbModule], providers = [Writer])]
/// struct AppModule;
///
/// # #[tokio::main(flavor = "current_thread")]
/// # async fn main() {
/// let app = App::builder(AppModule).wire().unwrap().connect().await.unwrap();
/// assert_eq!(app.get::<Writer>().await.unwrap().db.0, "primary");
/// # }
/// ```
///
/// `#[module]` writes what a module declares: imports, bindings of a type or a value, collection
/// contributions, controllers and exports. A module that needs a readiness check (`.ready(..)`),
/// a closure hook (`.on_init(..)`, `.on_destroy(..)`), a qualified closure binding or collection,
/// the `enhancer` mark or module metadata such as `ulo_http::PreDispatch` implements `Module` by
/// hand and calls those on the `ModuleDef`; it imports and is imported by `#[module]` modules
/// like any other.
///
/// The key comes first in `K: with = ..` because a closure's body extends as far as it can: in
/// `with = |cfg| RedisCache::new(cfg) as dyn Cache`, the `as` would parse as a cast inside the
/// body. As with `X as dyn T`, the concrete type stays bound in the module too.
///
/// An `into K: [..]` list takes the forms `#[guards]` does, whatever `K` is: a type
/// (`provide::<A>`), `value = expr` (a shared value; the macro writes the `Arc::new`),
/// `with = |..| ..` (a factory written as in `#[guards]`: `with`, or `try_with` when its output
/// is a `Result`) and `with(<scope>) = |..| ..` (`singleton`, `execution` or `transient`, or the
/// `try_` form of each). It also takes `value = expr?` (`try_value`), whose `Err` `wire()`
/// reports as it reports a providers entry's `expr?`. Every item lowers to `contribute::<K>()`.
/// A global enhancer is a contribution under a role key, `into AnyGuard<Http>: [AuthGuard]`,
/// which the core recognises by the key's type.
///
/// A `K` that is module metadata, a type implementing `ulo::Meta` such as `ulo_http::PreDispatch`
/// or `ulo_http::Upgrades`, is not a contribution key: a transport reads it through
/// `m.meta::<K>()`, so `into PreDispatch: [..]` fails to compile with an error naming `K`. Such a
/// module is written by hand with `ModuleDef`.
///
/// `with` says how an item is built: by its closure. How long it lives is a separate argument,
/// written as on `#[injectable]`. `with = ..` declares `Auto`: built once unless what the closure
/// reads needs an execution, then built per execution. A provider contribution under `Auto` is a
/// singleton, and `wire()` refuses one whose closure reads execution data. `with(singleton) = ..`,
/// `with(execution) = ..` and `with(transient) = ..` write the scope instead.
///
/// Inference reads what a closure takes, not what it does. A closure that creates per-call state
/// but reads nothing per call, such as `|| RequestTimer::start()`, is built once under `Auto`;
/// it must be written `with(execution) = || RequestTimer::start()`.
/// A controller's handler impl, on every transport: writes `impl ulo::Handlers`, whose `mount`
/// registers each handler, with the impl's `#[guards]`, `#[interceptors]`, `#[error_handlers]` and
/// `#[meta]` as the controller tier and each method's as the method tier; every other item stays
/// as written. A handler is a method carrying its transport's attribute, `#[ulo_http::get(..)]`,
/// `#[ulo_rpc::message(..)]`, `#[ulo_ws::message(..)]` or `#[ulo_grpc::method]`. The controller
/// itself is declared with `#[controller]`, on the struct or stacked on this impl in either order,
/// where `new` or the `#[construct]` fn builds it, and a module that registers the controller
/// mounts these handlers through `Handlers::mount`. `Handlers` requires `Controller`, so on a type
/// that is not a controller, one declared `#[injectable]` among them, `#[handlers]` is a compile
/// error naming `Controller`.
///
/// An impl-level `value = expr` is built once by `Handlers::mount` and shared by every handler
/// it applies to, so a rate limiter declared on the impl limits the controller as a whole. A
/// controller-level transport key that no handler's transport carries, `#[guards(htpp = ..)]`, is
/// a compile error spanned on the key.
///
/// A handler behind `#[cfg]` is mounted, and its transport key counted, only in a build that
/// compiles it. A scoped entry for a transport whose handlers all sit behind
/// `#[cfg(feature = "rpc")]` goes in `#[cfg_attr(feature = "rpc", guards(rpc = ..))]` on the impl,
/// which rustc evaluates before `#[handlers]` reads the impl's attributes.
///
/// `#[handlers]` reads a method's attributes through `cfg_attr`, at any depth:
/// - `#[cfg_attr(feature = "x", ulo_http::get("/beta"))]` makes the method a handler only where
/// the predicate holds, gated like a handler behind `#[cfg]`: mounted, and its key counted, only
/// in that build. Elsewhere it is a plain method, dead code unless something calls it;
/// `#[cfg(feature = "x")]` removes it instead.
/// - `#[cfg_attr(feature = "x", guards(..))]`, and likewise `interceptors`, `error_handlers` and
/// `meta`, on a handler applies its entries only where the predicate holds.
/// - One `cfg_attr` may hold several of these beside unrelated attributes,
/// `#[cfg_attr(feature = "x", ulo_http::get("/beta"), guards(AuthGuard), inline)]`;
/// `#[handlers]` takes the enhancers and `meta` and leaves the rest in place.
///
/// `#[handlers]` reads and removes every enhancer and `#[meta]` attribute it takes, and writes
/// beside the impl a hidden import of each, through which an editor answers hover and
/// go-to-definition on the attribute's name, and on its `ulo` segment when it is written
/// `#[ulo::guards]`. A marker is written bare or under `ulo`; any other qualifier,
/// `#[framework::guards]` or a typo, is a compile error at the attribute.
///
/// A method carrying an attribute outside the language's own (`doc`, `allow`, `cfg`, `inline` and
/// the like), the enhancer and `#[meta]` markers and `#[construct]`, such as
/// `#[tracing::instrument]`, inside `cfg_attr` or not, is treated as a handler, so helpers go in a
/// separate `impl` block. One such attribute written outside any `cfg_attr` makes the method a
/// handler in every build; when every
/// one sits inside a `cfg_attr`, the first decides, so the transport attribute goes first. A
/// transport attribute inside `cfg_attr` beside another attribute macro outside one fails to
/// compile where the predicate fails. A handler takes `&self`, or `self: Arc<Self>` for a reply
/// that outlives the call, and declares no type or const parameter; the controller itself may be
/// generic.
/// Guards for every handler of a `#[handlers]` impl, or for one method: by type (`AuthGuard`),
/// transport-scoped by type (`http = AuthGuard`), by value (`value = expr`), or by closure
/// (`with = |u: Ext<CurrentUser>| ..`). Read and removed by `#[handlers]`, so on the impl it goes
/// below `#[handlers]`; on a method it may sit inside `cfg_attr`; anywhere else it is an error.
///
/// On the impl, an entry applies to every handler and must have the role for each handler's
/// transport; `http = AuthGuard` limits it to that transport's handlers. `value = expr` is built
/// once and shared: on a method by that handler's calls, on the impl by every handler it applies
/// to.
///
/// A closure is written synchronously, its parameters injection points. `with` says only that
/// the closure builds the guard; how long the guard lives is a separate argument, written as on
/// `#[injectable]`. `with = ..` declares `Auto`: built once and shared unless what the closure
/// reads needs an execution, then built per execution. `with(singleton) = ..`,
/// `with(execution) = ..` and `with(transient) = ..` write the scope instead, and
/// `http(with(execution) = ..)` limits one to a transport's handlers.
///
/// Inference reads what a closure takes, not what it does. A closure that creates per-call state
/// but reads nothing per call, such as `|| RequestTimer::start()`, is built once under `Auto`;
/// it must be written `with(execution) = || RequestTimer::start()`.
/// Interceptors, with the same forms as `#[guards]`.
/// Error handlers, with the same forms as `#[guards]`.
/// Declared metadata on a handler or on its `#[handlers]` impl: plain `Send + Sync + 'static`
/// values, built once when the handler mounts, which guards and interceptors read through
/// `cx.exec().handler()`. The method's declaration of a type wins over the impl's. Read and
/// removed by `#[handlers]`, so on the impl it goes below `#[handlers]`; on a method it may sit inside
/// `cfg_attr`; anywhere else it is an error.
/// Carries a handler's controller-tier and method-tier enhancers and metadata from `#[handlers]` to
/// the transport's handler attribute, which consumes it (`ulo_handler_codegen::protocol`).
/// Reaching expansion means no transport attribute consumed it.
/// Invoked by a transport's generated mount function with its transport type, its transport key,
/// the name of its `shared` parameter and the tokens `__handler` carried: expands to the two
/// `EnhancerSpec`s for one handler, with one role assertion per enhancer naming the handler.