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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use std::{
    collections::HashMap,
    path::{Path, PathBuf},
};

use proc_macro2::Span;
use quote::quote;

use crate::{
    metadata::{CollectedMetadata, StructMetadata},
    route_impl::StoredRouteInfo,
    router_codegen::{
        ProcessedVesperaInput, apply_export_prefix, generate_router_code, namespace_export_schemas,
        schema_namespace_from_prefix,
    },
};

use super::{
    cache::{
        CACHE_FORMAT, CacheKey, MergeSpecCache, VesperaCache, compute_config_hash_with_merge_cache,
        compute_export_config_hash, compute_macro_dev_fingerprint, compute_schema_hash,
        get_cache_path, get_export_cache_path, hash_str, read_cache, sidecar_matches, write_cache,
    },
    openapi_io::{
        ensure_openapi_files_from_cache, generate_and_write_openapi, load_validated_sidecar_specs,
        write_pretty_sidecar, write_spec_for_embedding,
    },
    path_utils::{find_folder_path, find_target_dir},
    route_merge::merge_route_storage_data,
};

/// Fold the macro-invocation-local schema/route storage into a freshly
/// obtained (cached or scanned) `metadata`, then reject duplicate schema
/// names.
///
/// The same three steps run on both the cache-hit and cache-miss branch of
/// both `vespera!` and `export_app!`; `macro_label` is the only difference
/// (`"vespera!"` vs `"export_app!"`), and it only reaches the error string.
fn finalize_metadata(
    metadata: &mut CollectedMetadata,
    schema_storage: &HashMap<String, StructMetadata>,
    route_storage: &[StoredRouteInfo],
    macro_label: &str,
) -> syn::Result<()> {
    metadata.structs.extend(schema_storage.values().cloned());
    merge_route_storage_data(metadata, route_storage);
    metadata
        .check_duplicate_schema_names()
        .map_err(|msg| syn::Error::new(Span::call_site(), format!("{macro_label} macro: {msg}")))
}

/// Process vespera macro - extracted for testability
#[allow(clippy::too_many_lines)]
pub fn process_vespera_macro(
    processed: &ProcessedVesperaInput,
    schema_storage: &HashMap<String, StructMetadata>,
    route_storage: &[StoredRouteInfo],
    folder_span: Span,
) -> syn::Result<proc_macro2::TokenStream> {
    let profile_start = if std::env::var("VESPERA_PROFILE").is_ok() {
        eprintln!(
            "[vespera-profile] storage at expansion: {} routes, {} schemas",
            route_storage.len(),
            schema_storage.len()
        );
        Some(std::time::Instant::now())
    } else {
        None
    };

    // Stage timer for `VESPERA_PROFILE=1` — prints per-stage elapsed
    // times so regressions can be attributed (scan vs openapi vs
    // serialization vs codegen).
    let mut stage_start = std::time::Instant::now();
    let mut stage = |name: &str| {
        if profile_start.is_some() {
            eprintln!("[vespera-profile]   {name}: {:?}", stage_start.elapsed());
            stage_start = std::time::Instant::now();
        }
    };

    let folder_path = find_folder_path(&processed.folder_name)?;
    if !folder_path.exists() {
        return Err(syn::Error::new(
            folder_span,
            format!(
                "vespera! macro: route folder '{}' not found. Create src/{} or specify a different folder with `dir = \"your_folder\"`.",
                processed.folder_name, processed.folder_name
            ),
        ));
    }

    // --- Incremental cache check ---
    // One directory walk serves both the fingerprint map and (on a
    // cache miss) route collection below.
    let cache_path = get_cache_path();
    let scanned = crate::collector::scan_route_folder(&folder_path)
        .map_err(|e| syn::Error::new(Span::call_site(), format!("vespera! macro: {e}")))?;
    let fingerprints = crate::collector::fingerprints_from_scan(&scanned);
    let schema_hash = compute_schema_hash(schema_storage);
    let mut merge_specs = MergeSpecCache::new();
    let config_hash = compute_config_hash_with_merge_cache(processed, &mut merge_specs);
    stage("fingerprints + hashes");

    let macro_version = env!("CARGO_PKG_VERSION").to_string();
    let macro_dev_fingerprint = compute_macro_dev_fingerprint();
    stage("macro_dev_fingerprint");
    // Fold the freshness predicate into the `Option` itself: a stale cache
    // becomes `None`, so the reusable-cache branch below is reachable only
    // through a `Some` pattern. Keeping the validity as a separate `bool`
    // would leave the `Some`-ness unproven to the type system and force an
    // `unwrap()`.  The clause chain itself lives in `VesperaCache::is_fresh`
    // so `export_app!` below cannot drift out of sync with it.
    let cache_key = CacheKey {
        macro_version: &macro_version,
        macro_dev_fingerprint,
        file_fingerprints: &fingerprints,
        schema_hash,
        config_hash,
    };
    let cached = read_cache(&cache_path).filter(|c| c.is_fresh(&cache_key));
    stage("read_cache");
    // Hash-validate the sidecar spec files (the cache only stores
    // hashes — content lives in `target/vespera/`).  Validation
    // failure downgrades to a full regeneration, which rewrites the
    // sidecars: corruption self-heals on the next build.
    let sidecars = cached.as_ref().and_then(|c| {
        load_validated_sidecar_specs(
            c.spec_json_hash,
            c.spec_pretty_hash,
            c.spec_json_fingerprint,
            c.spec_pretty_fingerprint,
        )
    });
    stage("validate_sidecar_specs");

    let (metadata, spec_tokens) = if let (Some(sidecars), Some(cache)) = (sidecars, cached) {
        let mut metadata = cache.metadata;
        finalize_metadata(&mut metadata, schema_storage, route_storage, "vespera!")?;
        stage("cache_branch_metadata_merge");

        // Ensure openapi.json files exist and are up-to-date from cache
        ensure_openapi_files_from_cache(&processed.openapi_file_names, sidecars.pretty.as_deref())?;
        stage("ensure_openapi_files_from_cache");

        (metadata, sidecars.spec_tokens)
    } else {
        // Borrow the pre-scanned `(path, mtime)` pairs as `&Path` — no
        // PathBuf clone of the whole file list per cache-miss expansion.
        let (mut metadata, file_asts) = crate::collector::collect_metadata_from_files(scanned.iter().map(|(path, _)| path.as_path()), &folder_path, &processed.folder_name, route_storage).map_err(|e| syn::Error::new(Span::call_site(), format!("vespera! macro: failed to scan route folder '{}'. Error: {}. Check that all .rs files have valid Rust syntax.", processed.folder_name, e)))?;
        stage("collect_metadata");

        // Clone metadata before extending (cache stores file-only structs)
        let cache_metadata = metadata.clone();
        finalize_metadata(&mut metadata, schema_storage, route_storage, "vespera!")?;
        stage("metadata merge");

        // B2: reject same-file extractor structs that lack `#[derive(Schema)]`
        // before they silently vanish from the generated spec. Runs only here
        // (cache miss) — a cache hit is byte-identical source that already
        // passed, so the check would be redundant.
        crate::parser::validate_schema_backed_extractors_with_cache(&metadata, &file_asts)?;
        stage("validate_schema_backed_extractors");

        let openapi = generate_and_write_openapi(
            processed,
            &metadata,
            file_asts,
            route_storage,
            &mut merge_specs,
        )?;
        stage("generate_and_write_openapi");

        let spec_json_hash = openapi.spec_json.as_deref().map(hash_str);
        let spec_pretty_hash = openapi.spec_pretty.as_deref().map(hash_str);
        let spec_pretty_fingerprint = write_pretty_sidecar(openapi.spec_pretty.as_deref());
        let embed_spec = write_spec_for_embedding(openapi.spec_json)?;
        let (spec_tokens, spec_json_fingerprint) =
            embed_spec.map_or((None, None), |spec| (Some(spec.tokens), spec.fingerprint));
        stage("write_spec_for_embedding");

        // Persist cache (best-effort, failures are silent) — spec
        // contents live in the sidecar files; only hashes are cached.
        write_cache(
            &cache_path,
            &VesperaCache {
                cache_format: CACHE_FORMAT,
                macro_version: macro_version.clone(),
                macro_dev_fingerprint,
                file_fingerprints: fingerprints,
                schema_hash,
                config_hash,
                metadata: cache_metadata,
                spec_json_hash,
                spec_pretty_hash,
                spec_json_fingerprint: spec_json_hash.and(spec_json_fingerprint),
                spec_pretty_fingerprint: spec_pretty_hash.and(spec_pretty_fingerprint),
            },
        );
        stage("write_cache");

        (metadata, spec_tokens)
    };

    // --- Cron job discovery from CRON_STORAGE ---
    // #[cron("...")] attribute already registers metadata at expansion time.
    // No folder scanning needed — just read the storage.
    let cron_jobs: Vec<crate::metadata::CronMetadata> = {
        // Per-crate snapshot (see `cron_impl::current_crate_crons`): in a
        // shared rust-analyzer proc-macro server this never picks up another
        // crate's `#[cron]` jobs.
        let storage = crate::cron_impl::current_crate_crons();
        // Fast path: most projects declare no `#[vespera::cron]` jobs, so the
        // final `collect()` would just produce `Vec::new()` anyway. Skip the
        // `fs::canonicalize` syscall on `<manifest>/src` and the
        // `HashMap`/`PathBuf` allocations entirely when there is nothing to
        // discover. Mirrors the early-return pattern in
        // `process_default_functions`.
        if storage.is_empty() {
            Vec::new()
        } else {
            let src_dir = std::env::var("CARGO_MANIFEST_DIR")
                .map(|d| {
                    let p = std::path::PathBuf::from(d).join("src");
                    // Canonicalize for reliable prefix stripping
                    let canonical = p.canonicalize().unwrap_or(p);
                    crate::file_utils::normalize_display_path(canonical)
                })
                .unwrap_or_default();
            let mut canonical_paths = HashMap::new();
            storage
                .iter()
                .map(|s| {
                    // Derive module path from file_path relative to src/
                    let module_path = s
                        .file_path
                        .as_ref()
                        .map(|fp| {
                            let normalized = canonicalized_cron_path(fp, &mut canonical_paths);
                            let relative = normalized
                                .strip_prefix(&src_dir)
                                .map_or(&*normalized, |rest| rest.trim_start_matches('/'));
                            // Convert path to module path: strip .rs, replace / with ::, strip mod
                            // Replace hyphens with underscores (Rust module convention)
                            cron_module_path(relative)
                        })
                        .unwrap_or_default();
                    crate::metadata::CronMetadata {
                        expression: s.expression.clone(),
                        function_name: s.fn_name.clone(),
                        module_path,
                        file_path: s.file_path.clone().unwrap_or_default(),
                    }
                })
                .collect()
        }
    };

    let result = Ok(generate_router_code(
        &metadata,
        processed.docs_url.as_deref(),
        processed.redoc_url.as_deref(),
        spec_tokens,
        &processed.merge,
        &cron_jobs,
    ));
    stage("generate_router_code");

    if let Some(start) = profile_start {
        eprintln!(
            "[vespera-profile] vespera! macro total: {:?}",
            start.elapsed()
        );
        crate::schema_macro::print_profile_summary();
    }

    result
}

fn canonicalized_cron_path(fp: &str, cache: &mut HashMap<PathBuf, String>) -> String {
    let path = PathBuf::from(fp);
    cache
        .entry(path)
        .or_insert_with_key(|path| {
            path.canonicalize()
                .map_or_else(|_| fp.to_string(), |path| path.display().to_string())
                .replace('\\', "/")
        })
        .clone()
}

fn cron_module_path(relative: &str) -> String {
    let stem = relative.strip_suffix(".rs").unwrap_or(relative);
    let mut module_path = String::with_capacity(stem.len());
    for ch in stem.chars() {
        match ch {
            '/' => module_path.push_str("::"),
            '-' => module_path.push('_'),
            _ => module_path.push(ch),
        }
    }
    if module_path.ends_with("::mod") {
        module_path.truncate(module_path.len() - "::mod".len());
    }
    module_path
}

fn namespace_and_serialize_export_openapi(
    openapi: &mut vespera_core::OpenApi,
    metadata: &CollectedMetadata,
    prefix: &str,
) -> syn::Result<String> {
    let schema_namespace = schema_namespace_from_prefix(prefix);
    namespace_export_schemas(openapi, metadata, &schema_namespace)?;
    serde_json::to_string(openapi).map_err(|error| syn::Error::new(Span::call_site(), format!("export_app! macro: failed to serialize OpenAPI spec to JSON. Error: {error}. Check that all schema types are serializable.")))
}

fn finish_export_metadata(
    mut metadata: CollectedMetadata,
    schema_storage: &HashMap<String, StructMetadata>,
    route_storage: &[StoredRouteInfo],
    prefix: &str,
) -> syn::Result<CollectedMetadata> {
    finalize_metadata(&mut metadata, schema_storage, route_storage, "export_app!")?;
    apply_export_prefix(&mut metadata, prefix);
    Ok(metadata)
}

/// Process `export_app` macro - extracted for testability
pub fn process_export_app(
    name: &syn::Ident,
    folder_name: &str,
    schema_storage: &HashMap<String, StructMetadata>,
    manifest_dir: &str,
    route_storage: &[StoredRouteInfo],
    prefix: &str,
    folder_span: Span,
) -> syn::Result<proc_macro2::TokenStream> {
    let profile_start = if std::env::var("VESPERA_PROFILE").is_ok() {
        Some(std::time::Instant::now())
    } else {
        None
    };

    let folder_path = find_folder_path(folder_name)?;
    if !folder_path.exists() {
        return Err(syn::Error::new(
            folder_span,
            format!(
                "export_app! macro: route folder '{folder_name}' not found. Create src/{folder_name} or specify a different folder with `dir = \"your_folder\"`.",
            ),
        ));
    }

    let app_name = name.to_string();
    let manifest_path = Path::new(manifest_dir);
    let target_dir = find_target_dir(manifest_path);
    let vespera_dir = target_dir.join("vespera");
    let spec_file = vespera_dir.join(format!("{app_name}.openapi.json"));
    let cache_path = get_export_cache_path(&app_name, folder_name, prefix);
    let scanned = crate::collector::scan_route_folder(&folder_path)
        .map_err(|e| syn::Error::new(Span::call_site(), format!("export_app! macro: {e}")))?;
    let fingerprints = crate::collector::fingerprints_from_scan(&scanned);
    let schema_hash = compute_schema_hash(schema_storage);
    let config_hash = compute_export_config_hash(&app_name, folder_name, prefix);
    let macro_version = env!("CARGO_PKG_VERSION").to_string();
    let macro_dev_fingerprint = compute_macro_dev_fingerprint();
    let cache_key = CacheKey {
        macro_version: &macro_version,
        macro_dev_fingerprint,
        file_fingerprints: &fingerprints,
        schema_hash,
        config_hash,
    };
    let cached = read_cache(&cache_path);
    let cache_hit = cached.as_ref().is_some_and(|c| {
        c.is_fresh(&cache_key)
            && sidecar_matches(&spec_file, c.spec_json_hash, c.spec_json_fingerprint)
    });

    // Build a single fully-extended `metadata` per branch — previously the
    // cache-miss path did extend+merge+check twice (once inside this `else`
    // for the OpenAPI generation, once again in the outer block below) and
    // returned the un-extended snapshot, forcing the redundant re-work. Now
    // each branch returns the already-extended `CollectedMetadata` and the
    // duplicated outer pass is removed.
    let metadata = if let (true, Some(cache)) = (cache_hit, cached) {
        finish_export_metadata(cache.metadata, schema_storage, route_storage, prefix)?
    } else {
        let (mut metadata, file_asts) = crate::collector::collect_metadata_from_files(scanned.iter().map(|(path, _)| path.as_path()), &folder_path, folder_name, route_storage).map_err(|e| syn::Error::new(Span::call_site(), format!("export_app! macro: failed to scan route folder '{folder_name}'. Error: {e}. Check that all .rs files have valid Rust syntax.")))?;
        let cache_metadata = metadata.clone();
        metadata = finish_export_metadata(metadata, schema_storage, route_storage, prefix)?;

        // B2: same-file extractor structs without `#[derive(Schema)]` would be
        // silently dropped from the spec — reject them at compile time.
        crate::parser::validate_schema_backed_extractors_with_cache(&metadata, &file_asts)?;

        let mut openapi_doc = crate::openapi_generator::try_generate_openapi_doc_with_metadata(
            None,
            None,
            None,
            None,
            &metadata,
            Some(file_asts),
            route_storage,
        )?;
        let spec_json =
            namespace_and_serialize_export_openapi(&mut openapi_doc, &metadata, prefix)?;

        // Write spec to temp file for compile-time merging by parent apps
        std::fs::create_dir_all(&vespera_dir).map_err(|e| syn::Error::new(Span::call_site(), format!("export_app! macro: failed to create build cache directory '{}'. Error: {}. Ensure the target directory is writable.", vespera_dir.display(), e)))?;
        let spec_json_fingerprint = super::openapi_io::write_if_changed(&spec_file, &spec_json).map_err(|e| syn::Error::new(Span::call_site(), format!("export_app! macro: failed to write OpenAPI spec file '{}'. Error: {}. Ensure the file path is writable.", spec_file.display(), e)))?;
        let spec_json_hash = Some(hash_str(&spec_json));
        write_cache(
            &cache_path,
            &VesperaCache {
                cache_format: CACHE_FORMAT,
                macro_version: macro_version.clone(),
                macro_dev_fingerprint,
                file_fingerprints: fingerprints,
                schema_hash,
                config_hash,
                metadata: cache_metadata,
                spec_json_hash,
                spec_pretty_hash: None,
                spec_json_fingerprint: spec_json_hash.and(spec_json_fingerprint),
                spec_pretty_fingerprint: None,
            },
        );
        metadata
    };
    let spec_path_str = crate::file_utils::path_to_include_str_literal(&spec_file);

    let router_code = generate_router_code(&metadata, None, None, None, &[], &[]);

    let result = Ok(quote! {
        /// Auto-generated vespera app struct
        pub struct #name;

        impl #name {
            /// OpenAPI specification as JSON string
            pub const OPENAPI_SPEC: &'static str = include_str!(#spec_path_str);

            /// Create the router for this app.
            /// Returns `Router<()>` which can be merged into any other router.
            pub fn router() -> vespera::axum::Router<()> {
                #router_code
            }
        }
    });

    if let Some(start) = profile_start {
        eprintln!(
            "[vespera-profile] export_app! macro total: {:?}",
            start.elapsed()
        );
        crate::schema_macro::print_profile_summary();
    }

    result
}

#[cfg(test)]
mod tests;