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
//! `#[public]` proc macro implementation.
//!
//! `#[public]` is a *marker* attribute: it declares that a handler is
//! deliberately unauthenticated. Unlike [`#[secured]`](crate::secured) it
//! injects no runtime guard and does not rewrite the handler signature — it
//! only emits a `__AUTUMN_PUBLIC` marker const that the route macros
//! (`#[get]`, `#[post]`, …) read back to populate `ApiDoc::public`.
//!
//! The marker mirrors `#[secured]`'s `__AUTUMN_SECURED_ROLES` mechanism so the
//! route macro can detect the declaration whether `#[public]` sits above or
//! below the route attribute in the stack.
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{ItemFn, parse_quote};
pub fn public_macro(attr: TokenStream, item: TokenStream) -> TokenStream {
if attr.into_iter().next().is_some() {
return syn::Error::new(Span::call_site(), "#[public] does not take any arguments")
.to_compile_error();
}
let mut input_fn: ItemFn = match syn::parse2(item) {
Ok(f) => f,
Err(err) => return err.to_compile_error(),
};
// Prepend the marker const so a route macro that expands *after* this one
// can read the declaration back out of the handler body. When the route
// macro is outermost instead, it detects the `#[public]` attribute directly
// (see `api_doc::is_public`).
let marker: syn::Stmt = parse_quote! {
// Route macros read this marker when #[public] expands before #[get]/#[post]/etc.
const __AUTUMN_PUBLIC: () = ();
};
input_fn.block.stmts.insert(0, marker);
quote! { #input_fn }
}
#[cfg(test)]
mod tests {
use quote::quote;
use super::public_macro;
#[test]
fn injects_public_marker() {
let generated =
public_macro(quote! {}, quote! { async fn h() -> &'static str { "ok" } }).to_string();
assert!(
generated.contains("__AUTUMN_PUBLIC"),
"public marker must be emitted: {generated}"
);
}
#[test]
fn preserves_handler_signature() {
let generated =
public_macro(quote! {}, quote! { async fn h() -> &'static str { "ok" } }).to_string();
// No return-type rewrite and no injected extractors — the marker only.
assert!(generated.contains("async fn h"));
assert!(!generated.contains("Response"));
assert!(!generated.contains("__autumn_session"));
}
#[test]
fn rejects_arguments() {
let generated = public_macro(quote! { "oops" }, quote! { async fn h() {} }).to_string();
assert!(generated.contains("compile_error"));
}
}