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
342
343
344
345
346
347
348
349
//! Procedural macros for the rust-webx framework (package `rust-webx-macros`,
//! imported as `webx_macros`).
//!
//! - `#[get]` / `#[post]` / `#[put]` / `#[delete]` / `#[endpoint]` — route registration
//! - `#[handler]` — handler auto-registration via `inventory`
//! - `#[webx::main]` / `#[webx::main(embed)]` — application entry + asset embedding
//! - `#[webx::embed_assets]` — asset table include for integration tests
//! - `#[derive(WebxRequestMeta)]` — OpenAPI parameter metadata
//! - `#[claims]` / `#[authorize]` — authentication and authorization
//! - `#[display]` / `#[description]` — endpoint name and description resource
//! references
//! - `#[category]` — optional display grouping for endpoint listings
//!
//! Endpoint text priority is `#[display]` / `#[description]` first, then the
//! `impl` block's `///` doc comment. A doc comment's first line is the name
//! (OpenAPI `summary`); the full comment is the description. When there is no
//! text at all, the name is derived from the request type.
//!
//! `#[display(key = "endpoint.blog.list.name")]` stores a resource key; the
//! translation lives in a resource file and is resolved by the host's
//! `Culture.Default`. `#[display("博客列表")]` stores a literal instead.
use TokenStream;
use quote;
use parse_quote;
/// Application entry — ASP.NET Core `Main` analogue (`#[tokio::main]`).
///
/// | Attribute | Baked-in wwwroot |
/// |-----------|------------------|
/// | `#[webx::main]` | No — disk SPA only (`.use_spa` / auto-detect) |
/// | `#[webx::main(embed)]` | Yes — requires `build.rs` `web_root(...).build()` |
///
/// ```ignore
/// // Disk-only (no build.rs embed step)
/// #[webx::main]
/// async fn main() {
/// Host::builder().use_spa("wwwroot").build().run().await.unwrap();
/// }
///
/// // Baked into the binary
/// // build.rs: webx::builder().web_root("wwwroot").build()?;
/// #[webx::main(embed)]
/// async fn main() {
/// Host::builder().use_spa("wwwroot").build().run().await.unwrap();
/// }
/// ```
///
/// - **`web_root` (build.rs)** — which files are compiled into the executable
/// - **`.use_spa(...)` (runtime)** — disk directory consulted first (operator overrides)
/// - Without `build.rs` / without `(embed)` — nothing is baked in
/// Include the `OUT_DIR` asset table (for integration tests that never run `main`).
// ---------------------------------------------------------------------------
// Route macros: full form + shortcuts
// ---------------------------------------------------------------------------
/// Full form: marks an IRequest impl with HTTP method and route path.
///
/// ```ignore
/// #[endpoint(HttpGet, "/users/{id}")]
/// impl IRequest<UserModel> for GetUserRequest {}
/// ```
/// Shortcut: registers an IRequest impl at GET /path.
///
/// ```ignore
/// #[get("/users/{id}")]
/// impl IRequest<UserModel> for GetUserRequest {}
/// ```
/// Shortcut: registers an IRequest impl at POST /path.
///
/// ```ignore
/// #[post("/users")]
/// impl IRequest<UserModel> for CreateUserRequest {}
/// ```
/// Shortcut: registers an IRequest impl at PUT /path.
///
/// ```ignore
/// #[put("/users/{id}")]
/// impl IRequest<UserModel> for UpdateUserRequest {}
/// ```
/// Shortcut: registers an IRequest impl at DELETE /path.
///
/// ```ignore
/// #[delete("/users/{id}")]
/// impl IRequest<String> for DeleteUserRequest {}
/// ```
// ---------------------------------------------------------------------------
// Handler auto-registration
// ---------------------------------------------------------------------------
/// Auto-registers an IRequestHandler implementation at compile time via inventory.
///
/// Place on `impl IRequestHandler<T, R> for Handler` blocks.
/// The handler struct MUST implement `Default`.
///
/// ```ignore
/// #[handler]
/// #[async_trait]
/// impl IRequestHandler<HelloRequest, String> for HelloHandler {
/// async fn handle(&mut self, _req: HelloRequest) -> Result<String> { ... }
/// }
/// ```
// ---------------------------------------------------------------------------
// Parameter binding (OpenAPI metadata via WebxRequestMeta derive helpers)
// ---------------------------------------------------------------------------
//
// `frompath`, `fromquery`, `frombody`, `fromform`, `fromfile`, `fromheader`
// and `fromclaim` are helper attributes for `#[derive(WebxRequestMeta)]` — not
// standalone attribute macros (avoids rustc conflicts on struct fields).
/// Registers parameter metadata and the request binder from `#[from*]` fields.
///
/// Markers: `#[frompath]`, `#[fromquery]`, `#[frombody]`, `#[fromform]`,
/// `#[fromfile]`, `#[fromheader("Name")]`, `#[fromclaim("key")]`. Every marker
/// except `#[frombody]` accepts an optional source key.
///
/// Unmarked fields follow the request method: `GET`/`HEAD`/`DELETE`/`OPTIONS`
/// bind from the query string, then the route; `POST`/`PUT`/`PATCH` bind from
/// the JSON body, then the form, then the query string. Headers and claims are
/// opt-in only. `#[request(queryall)]` forces unmarked fields to the query.
// ---------------------------------------------------------------------------
// Declaration macros
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// Claims attribute
// ---------------------------------------------------------------------------
/// Marks a request struct as carrying authentication claims.
///
/// Appends a `#[serde(skip)] pub claims: Option<Box<dyn IClaims>>` field to
/// the struct and generates an inherent `set_claims` method that shadows the
/// blanket no-op `IClaimsCarrier::set_claims`. The dispatcher injects claims
/// into the request *before* calling `IRequestHandler::handle`.
///
/// Must be the outermost attribute so that `#[derive(...)]` sees the injected
/// field:
///
/// ```ignore
/// #[claims]
/// #[derive(Default, Deserialize)]
/// pub struct CreateCommentRequest {
/// pub blog_id: i32,
/// pub content: String,
/// }
/// ```
// ---------------------------------------------------------------------------
// Authorization attribute (declarative, handled by emit_endpoint)
// ---------------------------------------------------------------------------
/// Declares authorization requirements on a route.
///
/// Each attribute is one rule and **every** rule must be satisfied. Inside a
/// rule, `roles` / `permissions` are any-of lists and the two dimensions are
/// ANDed; a bare `#[authorize]` requires a valid authenticated principal.
///
/// ```ignore
/// #[get("/api/users")]
/// #[authorize(role = "admin")]
/// impl IRequest<Vec<UserModel>> for ListUsersRequest {}
///
/// #[get("/api/auth/me")]
/// #[authorize]
/// impl IRequest<UserView> for AuthMeRequest {}
///
/// // Either role is enough.
/// #[get("/api/reports")]
/// #[authorize(roles = ["admin", "auditor"])]
/// impl IRequest<Vec<Report>> for ListReportsRequest {}
///
/// // Both rules must hold: admin role and the export permission.
/// #[post("/api/reports/export")]
/// #[authorize(role = "admin")]
/// #[authorize(permission = "reports:export")]
/// impl IRequest<ResponseData> for ExportReportsRequest {}
/// ```
// ---------------------------------------------------------------------------
// Endpoint text attributes (declarative, handled by emit_endpoint)
// ---------------------------------------------------------------------------
/// Declares an endpoint's display name as a resource reference.
///
/// `#[display("literal")]` is a fixed name. `#[display(key = "...")]` resolves
/// the key from the host's resource files; `text` supplies a literal fallback
/// when the key is absent. Localization is a host-wide culture setting, not one
/// attribute per language.
///
/// ```ignore
/// #[get("/api/blog")]
/// #[display(key = "endpoint.blog.list.name", text = "博客模块-博客列表")]
/// impl IRequest<Vec<BlogModel>> for ListBlogRequest {}
/// ```
/// Declares an endpoint's long description as a resource reference.
///
/// Takes priority over the impl block's `///` doc comment. Accepts the same
/// forms as [`display`].
///
/// ```ignore
/// #[get("/api/blog")]
/// #[description(key = "endpoint.blog.list.description")]
/// impl IRequest<Vec<BlogModel>> for ListBlogRequest {}
/// ```
/// Declares an optional display grouping for the endpoint.
///
/// The category is metadata for endpoint listings (permission trees, admin
/// navigation, OpenAPI tags); it does not affect routing or authorization.
/// It accepts the same forms as [`display`].
///
/// ```ignore
/// #[get("/api/blog")]
/// #[category("博客模块")]
/// impl IRequest<Vec<BlogModel>> for ListBlogRequest {}
/// ```
/// Rewrite an endpoint-text attribute into a hidden attribute the route macro
/// can read regardless of declaration order.
///
/// Attribute macros expand outermost-first, so a `#[display]` above `#[post]`
/// runs first and would otherwise be invisible to the route macro. Carrying the
/// same arguments on `#[webx::__webx_display(...)]` lets the route macro read it
/// in either order; the hidden attribute is a no-op passthrough afterwards.
/// Internal carrier for [`display`] — do not use directly.
/// Internal carrier for [`description`] — do not use directly.
/// Internal carrier for [`category`] — do not use directly.