vespera_macro 0.4.0

Procedural macros for Vespera - automatic route discovery and OpenAPI generation
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Vespera macro implementation crate.
//!
//! This crate contains all the proc-macros for Vespera:
//! - `#[vespera::route(...)]` - Mark a function as a route handler
//! - `#[derive(Schema)]` - Register a type for `OpenAPI` schema generation
//! - `schema!(...)` - Get `OpenAPI` schema at compile time
//! - `vespera!(...)` - Generate Axum router with `OpenAPI`
//! - `export_app!(...)` - Export router for merging
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │ Compile-time (vespera! macro)                                    │
//! ├─────────────────────────────────────────────────────────────────┤
//! │ 1. Scan src/routes/ for .rs files              [collector]       │
//! │ 2. Parse #[route] attributes                   [args, route]     │
//! │ 3. Extract handler signatures                  [parser]          │
//! │ 4. Convert Rust types → JSON Schema            [parser/schema]   │
//! │ 5. Build OpenAPI document                      [openapi_gen]     │
//! │ 6. Write openapi.json to disk                  [vespera_impl]    │
//! │ 7. Generate Axum Router TokenStream            [router_codegen]  │
//! │ 8. Inject Swagger/ReDoc HTML routes           [router_codegen]  │
//! └─────────────────────────────────────────────────────────────────┘
//!
//! # Module Organization
//!
//! - `args` - Parse `#[route(...)]` attribute arguments
//! - `collector` - Filesystem scanning and route discovery
//! - `error` - Unified error handling
//! - `http` - HTTP method constants and validation
//! - `macro_storage` - Shared per-crate storage for `#[route]` / `#[cron]` metadata
//! - `metadata` - Type definitions for collected metadata
//! - `method` - HTTP method token stream generation
//! - `openapi_generator` - OpenAPI spec assembly
//! - `parser` - Type extraction and schema generation
//! - `route` - Route information structures
//! - `route_impl` - Route attribute macro implementation
//! - `router_codegen` - Router and macro input parsing
//! - `schema_impl` - Schema derive macro implementation
//! - `schema_macro` - `schema_type!` macro implementation
//! - `vespera_impl` - Main macro orchestration

mod args;
mod collector;
mod cron_impl;
mod error;
mod file_utils;
mod garde_emit;
mod http;
mod macro_storage;
mod metadata;
mod method;
mod openapi_generator;

mod multipart_impl;
mod parser;
mod route;
mod route_impl;
mod router_codegen;
mod schema_assertions;
mod schema_impl;
mod schema_macro;
mod vespera_impl;

use proc_macro::TokenStream;

use crate::{
    router_codegen::{AutoRouterInput, ExportAppInput, process_vespera_input},
    vespera_impl::{process_export_app, process_vespera_macro},
};

/// route attribute macro
#[cfg(not(tarpaulin_include))]
#[proc_macro_attribute]
pub fn route(attr: TokenStream, item: TokenStream) -> TokenStream {
    match route_impl::process_route_attribute(attr.into(), item.into()) {
        Ok(tokens) => tokens.into(),
        Err(e) => e.to_compile_error().into(),
    }
}

/// cron attribute macro
///
/// Mark a function as a cron job with the given cron expression.
///
/// # Example
/// ```ignore
/// #[vespera::cron("0 */5 * * * *")]
/// pub async fn cleanup_sessions() {
///     println!("Running cleanup");
/// }
/// ```
#[cfg(not(tarpaulin_include))]
#[proc_macro_attribute]
pub fn cron(attr: TokenStream, item: TokenStream) -> TokenStream {
    match cron_impl::process_cron_attribute(attr.into(), item.into()) {
        Ok(tokens) => tokens.into(),
        Err(e) => e.to_compile_error().into(),
    }
}

/// Derive macro for Schema
///
/// Supports `#[schema(name = "CustomName")]` attribute to set custom `OpenAPI` schema name.
///
/// # Duplicate schema name detection
///
/// `SCHEMA_STORAGE` is keyed by the OpenAPI schema name (struct ident by
/// default, or `#[schema(name = "...")]` if specified).  When two
/// **different** struct definitions register under the same name, only
/// the last one would survive in `openapi.json` — a silent footgun
/// that has bitten real users.  This derive therefore checks the
/// storage before inserting and emits a `compile_error!` so the
/// conflict surfaces at build time instead of at spec-generation time.
///
/// Identical re-registrations (e.g. incremental rebuilds running the
/// same derive twice) are idempotent: the definition token-stream
/// matches and the second call is a no-op.
#[cfg(not(tarpaulin_include))]
#[proc_macro_derive(Schema, attributes(schema, serde))]
pub fn derive_schema(input: TokenStream) -> TokenStream {
    schema_macro::file_cache::bump_epoch();

    let input = syn::parse_macro_input!(input as syn::DeriveInput);
    let (metadata, expanded) = schema_impl::process_derive_schema(&input);
    let Some(metadata) = metadata else {
        return TokenStream::from(expanded);
    };
    let name = metadata.name.clone();

    // Register into the current crate's bucket (see `current_crate_key`).
    // `Err` means a DIFFERENT definition is already registered under this name
    // for this crate — surface it as a hard compile error rather than the
    // silent last-write-wins overwrite that would hide a schema from the
    // generated `openapi.json`.
    if schema_impl::register_schema(name.clone(), metadata).is_err() {
        let span = input.ident.span();
        let msg = format!(
            "duplicate vespera Schema name `{name}` -- two different struct \
             definitions both register under the same OpenAPI schema name. \
             The later definition would silently overwrite the earlier one \
             in the generated `openapi.json`. Rename one of the structs, or \
             annotate one with `#[schema(name = \"OtherName\")]` to give \
             them distinct OpenAPI names."
        );
        let err = syn::Error::new(span, msg).to_compile_error();
        return TokenStream::from(err);
    }

    TokenStream::from(expanded)
}

/// Derive macro for `Multipart` with serde attribute support.
///
/// This is vespera's re-implementation of `axum_typed_multipart`'s derive macro
/// that natively supports `#[serde(rename_all)]` and `#[serde(rename)]` for
/// field name resolution in multipart form data.
///
/// # Supported Attributes
///
/// **Struct-level:**
/// - `#[serde(rename_all = "camelCase")]` — rename all fields (highest priority)
/// - `#[try_from_multipart(rename_all = "camelCase")]` — fallback rename
/// - `#[try_from_multipart(strict)]` — reject unknown/duplicate fields
///
/// **Field-level:**
/// - `#[form_data(field_name = "...")]` — explicit field name override
/// - `#[serde(rename = "...")]` — serde field rename
/// - `#[form_data(limit = "10MiB")]` — field size limit
/// - `#[form_data(default)]` — use `Default::default()` when missing
#[cfg(not(tarpaulin_include))]
#[proc_macro_derive(Multipart, attributes(serde, form_data, try_from_multipart))]
pub fn derive_multipart(input: TokenStream) -> TokenStream {
    let input = syn::parse_macro_input!(input as syn::DeriveInput);
    TokenStream::from(multipart_impl::process_derive(&input))
}

/// Generate an `OpenAPI` Schema from a type with optional field filtering.
///
/// This macro creates a `vespera::schema::Schema` struct at compile time
/// from a type that has `#[derive(Schema)]`.
///
/// # Syntax
///
/// ```ignore
/// // Full schema (all fields)
/// let user_schema = schema!(User);
///
/// // Schema with fields omitted
/// let response_schema = schema!(User, omit = ["password", "internal_id"]);
///
/// // Schema with only specified fields (pick)
/// let summary_schema = schema!(User, pick = ["id", "name"]);
/// ```
///
/// # Parameters
///
/// - `Type`: The type to generate schema for (must have `#[derive(Schema)]`)
/// - `omit = [...]`: Optional list of field names to exclude from the schema
/// - `pick = [...]`: Optional list of field names to include (excludes all others)
///
/// Note: `omit` and `pick` cannot be used together.
///
/// # Example
///
/// ```ignore
/// use vespera::{Schema, schema};
///
/// #[derive(Schema)]
/// struct User {
///     pub id: i32,
///     pub name: String,
///     pub email: String,
///     pub password: String,  // sensitive!
/// }
///
/// // For API responses, omit password
/// let response_schema = schema!(User, omit = ["password"]);
///
/// // For list endpoints, only return summary fields
/// let list_schema = schema!(User, pick = ["id", "name"]);
/// ```
#[cfg(not(tarpaulin_include))]
#[proc_macro]
pub fn schema(input: TokenStream) -> TokenStream {
    schema_macro::file_cache::bump_epoch();

    let input = syn::parse_macro_input!(input as schema_macro::SchemaInput);

    let storage = schema_impl::current_crate_schemas();

    match schema_macro::generate_schema_code(&input, &storage) {
        Ok(tokens) => TokenStream::from(tokens),
        Err(e) => e.to_compile_error().into(),
    }
}

/// Generate a new struct type derived from an existing type with field filtering.
///
/// This macro creates a new struct at compile time by picking or omitting fields
/// from an existing type that has `#[derive(Schema)]`.
///
/// # Syntax
///
/// ```ignore
/// // Pick specific fields
/// schema_type!(CreateUserRequest from User, pick = ["name", "email"]);
///
/// // Omit specific fields
/// schema_type!(UserResponse from User, omit = ["password", "internal_id"]);
///
/// // Without Clone derive
/// schema_type!(UserUpdate from User, pick = ["name"], clone = false);
/// ```
///
/// # Parameters
///
/// - `NewTypeName`: The name of the new struct to generate
/// - `from SourceType`: The source type to derive from (must have `#[derive(Schema)]`)
/// - `pick = [...]`: List of field names to include (excludes all others)
/// - `omit = [...]`: List of field names to exclude
/// - `clone = bool`: Whether to derive Clone (default: true)
/// - `partial`: Make all fields `Option<T>` (fields already `Option<T>` are unchanged)
/// - `partial = [...]`: Make only listed fields `Option<T>`
///
/// Note: `omit` and `pick` cannot be used together.
///
/// # Example
///
/// ```ignore
/// use vespera::{Schema, schema_type};
///
/// #[derive(Schema)]
/// pub struct User {
///     pub id: i32,
///     pub name: String,
///     pub email: String,
///     pub password: String,
/// }
///
/// // Generate CreateUserRequest with only name and email
/// schema_type!(CreateUserRequest from User, pick = ["name", "email"]);
///
/// // Generate UserPublic without password
/// schema_type!(UserPublic from User, omit = ["password"]);
///
/// // Now use in handlers:
/// pub async fn create_user(Json(req): Json<CreateUserRequest>) -> Json<UserPublic> {
///     // ...
/// }
/// ```
#[cfg(not(tarpaulin_include))]
#[proc_macro]
pub fn schema_type(input: TokenStream) -> TokenStream {
    schema_macro::file_cache::bump_epoch();

    let input = syn::parse_macro_input!(input as schema_macro::SchemaTypeInput);
    let ignore_schema = input.ignore_schema;

    let (tokens, generated_metadata) = {
        let storage = schema_impl::current_crate_schemas();
        match schema_macro::generate_schema_type_code(&input, &storage) {
            Ok(result) => result,
            Err(e) => return e.to_compile_error().into(),
        }
    };

    // The emitted token stream contains a struct with
    // `#[derive(Schema)]`; that derive macro registers the schema into
    // `SCHEMA_STORAGE` on its own.  We only need to pre-register here
    // when `ignore_schema` is set, because in that case the emitted
    // struct does NOT carry `#[derive(Schema)]` and would otherwise
    // be invisible to the OpenAPI generator.
    //
    // Pre-registering in the non-ignore path would cause the
    // duplicate-name check in `derive_schema` to fire on every
    // `schema_type!` call — the macro's own pre-insert collides with
    // the derive's later insert because the two `StructMetadata`
    // definitions are textually different (the pre-registered one is
    // synthesised by `schema_macro`; the derive-emitted one is the
    // expanded struct token stream).
    if ignore_schema && let Some(metadata) = generated_metadata {
        let name = metadata.name.clone();
        schema_impl::insert_schema(name, metadata);
    }
    TokenStream::from(tokens)
}

#[cfg(not(tarpaulin_include))]
#[proc_macro]
pub fn vespera(input: TokenStream) -> TokenStream {
    schema_macro::file_cache::bump_epoch();

    let input = syn::parse_macro_input!(input as AutoRouterInput);
    // Capture the `dir = "..."` literal span (or the macro call site when
    // `dir` is omitted) before `process_vespera_input` consumes `input`, so a
    // "route folder not found" diagnostic points at the offending argument
    // rather than the whole `vespera!` invocation.
    let folder_span = input
        .dir
        .as_ref()
        .map_or_else(proc_macro2::Span::call_site, syn::LitStr::span);
    let processed = process_vespera_input(input);
    // Per-crate snapshots (see `schema_impl::current_crate_key`): a shared
    // rust-analyzer proc-macro server never leaks another crate's schemas /
    // routes into this `vespera!` expansion.
    let schema_storage = schema_impl::current_crate_schemas();
    let route_storage = route_impl::current_crate_routes();

    match process_vespera_macro(&processed, &schema_storage, &route_storage, folder_span) {
        Ok(tokens) => tokens.into(),
        Err(e) => e.to_compile_error().into(),
    }
}

/// Export a vespera app as a reusable component.
///
/// Generates a struct with:
/// - `OPENAPI_SPEC: &'static str` - The `OpenAPI` JSON spec
/// - `router() -> Router` - Function returning the Axum router
///
/// # Example
/// ```ignore
/// // Simple - uses "routes" folder by default
/// vespera::export_app!(MyApp);
///
/// // Custom directory and explicit public route prefix
/// vespera::export_app!(MyApp, dir = "api");
/// vespera::export_app!(MyPlugin, prefix = "/api/media-library");
/// // The latter emits routes under /api/media-library and namespaces generated
/// // component schemas (for example Item -> MediaLibraryItem). A conventional
/// // leading /api is omitted from the PascalCase schema namespace. An explicit
/// // #[schema(name = "SharedThing")] remains global and is not namespaced.
///
/// // Generates:
/// // pub struct MyApp;
/// // impl MyApp {
/// //     pub const OPENAPI_SPEC: &'static str = "...";
/// //     pub fn router() -> axum::Router { ... }
/// // }
/// ```
///
#[cfg(not(tarpaulin_include))]
#[proc_macro]
pub fn export_app(input: TokenStream) -> TokenStream {
    schema_macro::file_cache::bump_epoch();

    let ExportAppInput { name, dir, prefix } = syn::parse_macro_input!(input as ExportAppInput);
    // Capture the `dir = "..."` literal span (or the macro call site when
    // `dir` is omitted) before `dir` is consumed below, so a "route folder
    // not found" diagnostic points at the offending argument.
    let folder_span = dir
        .as_ref()
        .map_or_else(proc_macro2::Span::call_site, syn::LitStr::span);
    let folder_name = dir
        .map(|d| d.value())
        .or_else(|| std::env::var("VESPERA_DIR").ok())
        .unwrap_or_else(|| "routes".to_string());
    let prefix = prefix.map_or_else(String::new, |value| value.value());
    let schema_storage = schema_impl::current_crate_schemas();
    let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") else {
        return syn::Error::new(proc_macro2::Span::call_site(), "export_app! macro: CARGO_MANIFEST_DIR is not set. This macro must be used within a cargo build.").to_compile_error().into();
    };

    let route_storage = route_impl::current_crate_routes();

    match process_export_app(
        &name,
        &folder_name,
        &schema_storage,
        &manifest_dir,
        &route_storage,
        &prefix,
        folder_span,
    ) {
        Ok(tokens) => tokens.into(),
        Err(e) => e.to_compile_error().into(),
    }
}