arcature-macros 0.1.0

Proc-macro crate for Arcature: #[model], #[request], #[controller], #[derive(Job)], #[derive(Event)].
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
//! `#[controller]` — an Axum controller with route metadata.
//!
//! An attribute macro applied to an `impl` block. Emits the impl unchanged
//! (the methods remain genuine Axum handlers) and additionally emits
//! `impl ControllerMetadata`, whose `METHODS` associated const carries one
//! [`ControllerMethod`] per handler: its name, its parameter names, and the
//! page identity derived from the **return type**.
//!
//! The page edge is read from the signature, never the body. A handler
//! returning `Page<T>` or `Result<Page<T>, E>` yields
//! `page: Some(<T>::PAGE_CONTRACT.name())` — a const expression that only
//! exists when `T` is a `#[page]` type, so a non-page type fails to compile.
//! That is the Client Exposure Firewall applied to the return type. Any
//! other return shape (`Response`, `Json<T>`, `Redirect`, `impl
//! IntoResponse`) yields `page: None`; such a handler declares its page on
//! the route, or carries an explicit `#[page("Name")]` helper attribute.
//!
//! One file, one macro: this is the entirety of the `#[controller]`
//! expansion.

use proc_macro2::TokenStream;
use quote::quote;
use syn::{FnArg, ImplItem, ImplItemFn, ItemImpl, Pat, PathArguments, ReturnType, Type};

use crate::diagnostic::{MacroError, MacroErrorCode, MacroResult};

/// The `#[controller]` attribute macro.
pub fn controller(_attr: TokenStream, item: TokenStream) -> MacroResult {
    let mut item_impl: ItemImpl =
        syn::parse2(item).map_err(|err| MacroError::from_syn(MacroErrorCode::ArcM001, err))?;

    let mut methods = Vec::new();
    for item in &mut item_impl.items {
        if let ImplItem::Fn(method) = item {
            validate(method)?;
            methods.push(describe(method)?);
        }
    }

    let self_ty = &item_impl.self_ty;
    let (impl_generics, _, where_clause) = item_impl.generics.split_for_impl();

    Ok(quote! {
        #item_impl

        #[automatically_derived]
        impl #impl_generics ::arcature::ControllerMetadata for #self_ty #where_clause {
            const METHODS: &'static [::arcature::ControllerMethod] = &[
                #(#methods),*
            ];
        }
    })
}

/// Enforces the controller-method contract: `pub async fn` with a return
/// type and no `self` receiver (an Axum handler is a free function).
fn validate(method: &ImplItemFn) -> Result<(), MacroError> {
    let sig = &method.sig;
    let span = sig.ident.span();

    if !matches!(method.vis, syn::Visibility::Public(_)) {
        return Err(MacroError::new(
            MacroErrorCode::ArcM004,
            span,
            "controller methods must be `pub`",
        ));
    }
    if sig.asyncness.is_none() {
        return Err(MacroError::new(
            MacroErrorCode::ArcM004,
            span,
            "controller methods must be `async fn`",
        ));
    }
    if matches!(sig.output, ReturnType::Default) {
        return Err(MacroError::new(
            MacroErrorCode::ArcM004,
            span,
            "controller methods must have a return type",
        ));
    }
    if sig
        .inputs
        .iter()
        .any(|arg| matches!(arg, FnArg::Receiver(_)))
    {
        return Err(MacroError::new(
            MacroErrorCode::ArcM004,
            span,
            "controller methods must not take `self` — a controller method is an \
             Axum handler, so its parameters are extractors",
        ));
    }

    Ok(())
}

/// Builds the `ControllerMethod` const expression for one handler, stripping
/// the `#[page("Name")]` helper attribute from the emitted method.
fn describe(method: &mut ImplItemFn) -> Result<TokenStream, MacroError> {
    let name = method.sig.ident.to_string();
    let params = method.sig.inputs.iter().map(param_name).collect::<Vec<_>>();

    let page = match take_page_attribute(method)? {
        Some(explicit) => quote! { ::core::option::Option::Some(#explicit) },
        None => page_from_return_type(&method.sig.output),
    };

    Ok(quote! {
        ::arcature::ControllerMethod {
            name: #name,
            params: &[#(#params),*],
            page: #page,
        }
    })
}

/// The parameter's binding name. A destructuring pattern
/// (`Path((a, b)): Path<(u32, u32)>`) has no single name; it reports `"_"`
/// rather than inventing one, keeping the slice's arity honest.
fn param_name(arg: &FnArg) -> String {
    match arg {
        FnArg::Typed(typed) => match &*typed.pat {
            Pat::Ident(ident) => ident.ident.to_string(),
            _ => "_".to_string(),
        },
        // Rejected by `validate`; unreachable in a well-formed expansion.
        FnArg::Receiver(_) => "self".to_string(),
    }
}

/// Removes a `#[page("Name")]` helper attribute from the method and returns
/// the declared page identity. The attribute is the escape hatch for a
/// handler that renders a page but does not return `Page<T>`; it must be
/// stripped, or the real `#[page]` attribute macro would try to expand it.
fn take_page_attribute(method: &mut ImplItemFn) -> Result<Option<String>, MacroError> {
    let Some(index) = method.attrs.iter().position(|a| a.path().is_ident("page")) else {
        return Ok(None);
    };
    let attr = method.attrs.remove(index);
    let literal: syn::LitStr = attr.parse_args().map_err(|_| {
        MacroError::new(
            MacroErrorCode::ArcM002,
            attr.path()
                .get_ident()
                .map_or_else(proc_macro2::Span::call_site, syn::Ident::span),
            "#[page] on a controller method requires a string literal page name, \
             e.g. #[page(\"users/show\")]",
        )
    })?;

    let name = literal.value();
    if name.is_empty() {
        return Err(MacroError::new(
            MacroErrorCode::ArcM002,
            literal.span(),
            "#[page] page name must not be empty",
        ));
    }
    Ok(Some(name))
}

/// Derives the page identity from the return type: `Page<T>` or
/// `Result<Page<T>, E>` yields `Some(<T>::PAGE_CONTRACT.name())`, everything
/// else yields `None`.
fn page_from_return_type(output: &ReturnType) -> TokenStream {
    let ReturnType::Type(_, ty) = output else {
        return quote! { ::core::option::Option::None };
    };

    let page_type = page_argument(ty).or_else(|| {
        // `Result<Page<T>>` / `Result<Page<T>, E>` — look through the ok type.
        first_generic_argument(ty, "Result").and_then(page_argument)
    });

    match page_type {
        Some(inner) => quote! {
            ::core::option::Option::Some(<#inner>::PAGE_CONTRACT.name())
        },
        None => quote! { ::core::option::Option::None },
    }
}

/// The `T` of a `Page<T>` type, if `ty` is one.
fn page_argument(ty: &Type) -> Option<&Type> {
    first_generic_argument(ty, "Page")
}

/// The first generic type argument of `ty`, when `ty` is a path type whose
/// final segment is named `expected` (so both `Page<T>` and
/// `arcature::Page<T>` match).
fn first_generic_argument<'a>(ty: &'a Type, expected: &str) -> Option<&'a Type> {
    let Type::Path(path) = ty else { return None };
    let segment = path.path.segments.last()?;
    if segment.ident != expected {
        return None;
    }
    let PathArguments::AngleBracketed(arguments) = &segment.arguments else {
        return None;
    };
    arguments.args.iter().find_map(|argument| match argument {
        syn::GenericArgument::Type(inner) => Some(inner),
        _ => None,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use quote::quote;

    fn expand(tokens: proc_macro2::TokenStream) -> String {
        controller(proc_macro2::TokenStream::new(), tokens)
            .expect("controller should expand")
            .to_string()
    }

    fn expand_err(tokens: proc_macro2::TokenStream) -> MacroError {
        controller(proc_macro2::TokenStream::new(), tokens).expect_err("should be rejected")
    }

    #[test]
    fn emits_the_impl_unchanged_alongside_the_metadata_impl() {
        let s = expand(quote! {
            impl HomeController {
                pub async fn index() -> Response { todo!() }
            }
        });
        assert!(s.contains("impl HomeController"), "got: {s}");
        assert!(s.contains("pub async fn index"), "got: {s}");
        assert!(
            s.contains("impl :: arcature :: ControllerMetadata for HomeController"),
            "got: {s}"
        );
        assert!(s.contains("const METHODS :"), "got: {s}");
    }

    #[test]
    fn records_the_method_name_and_parameter_names() {
        let s = expand(quote! {
            impl LinkController {
                pub async fn store(auth: Current, input: StoreLink) -> Response { todo!() }
            }
        });
        assert!(s.contains("name : \"store\""), "got: {s}");
        assert!(s.contains("params : & [\"auth\" , \"input\"]"), "got: {s}");
    }

    #[test]
    fn a_destructuring_parameter_reports_underscore_rather_than_a_made_up_name() {
        let s = expand(quote! {
            impl A {
                pub async fn show(Path((a, b)): Path<(u32, u32)>) -> Response { todo!() }
            }
        });
        assert!(s.contains("params : & [\"_\"]"), "got: {s}");
    }

    #[test]
    fn derives_the_page_identity_from_a_bare_page_return_type() {
        let s = expand(quote! {
            impl A {
                pub async fn index() -> Page<HomePage> { todo!() }
            }
        });
        assert!(
            s.contains("Some (< HomePage > :: PAGE_CONTRACT . name ())"),
            "got: {s}"
        );
    }

    #[test]
    fn derives_the_page_identity_through_result_with_one_or_two_arguments() {
        let one = expand(quote! {
            impl A { pub async fn index() -> Result<Page<HomePage>> { todo!() } }
        });
        assert!(one.contains("< HomePage > :: PAGE_CONTRACT"), "got: {one}");

        let two = expand(quote! {
            impl A { pub async fn index() -> Result<Page<HomePage>, Error> { todo!() } }
        });
        assert!(two.contains("< HomePage > :: PAGE_CONTRACT"), "got: {two}");
    }

    #[test]
    fn derives_the_page_identity_through_a_qualified_path() {
        let s = expand(quote! {
            impl A {
                pub async fn index() -> arcature::Result<arcature::Page<pages::HomePage>> { todo!() }
            }
        });
        assert!(
            s.contains("< pages :: HomePage > :: PAGE_CONTRACT"),
            "got: {s}"
        );
    }

    #[test]
    fn a_non_page_return_type_has_no_page_edge() {
        for output in [
            quote! { Response },
            quote! { Result<Response> },
            quote! { Json<UserResource> },
            quote! { impl IntoResponse },
        ] {
            let s = expand(quote! {
                impl A { pub async fn index() -> #output { todo!() } }
            });
            assert!(
                s.contains("page : :: core :: option :: Option :: None"),
                "got: {s}"
            );
        }
    }

    #[test]
    fn an_explicit_page_attribute_is_stripped_and_wins_over_the_return_type() {
        let s = expand(quote! {
            impl A {
                #[page("users/show")]
                pub async fn show() -> Response { todo!() }
            }
        });
        assert!(s.contains("Some (\"users/show\")"), "got: {s}");
        assert!(
            !s.contains("# [page"),
            "attribute must be stripped, got: {s}"
        );
    }

    #[test]
    fn an_explicit_page_attribute_overrides_a_page_return_type() {
        let s = expand(quote! {
            impl A {
                #[page("Overridden")]
                pub async fn index() -> Page<HomePage> { todo!() }
            }
        });
        assert!(s.contains("Some (\"Overridden\")"), "got: {s}");
        assert!(!s.contains("PAGE_CONTRACT"), "got: {s}");
    }

    #[test]
    fn an_empty_page_attribute_name_is_rejected() {
        let err = expand_err(quote! {
            impl A {
                #[page("")]
                pub async fn index() -> Response { todo!() }
            }
        });
        assert_eq!(err.code(), MacroErrorCode::ArcM002);
    }

    #[test]
    fn a_non_literal_page_attribute_is_rejected() {
        let err = expand_err(quote! {
            impl A {
                #[page(users::show)]
                pub async fn index() -> Response { todo!() }
            }
        });
        assert_eq!(err.code(), MacroErrorCode::ArcM002);
    }

    #[test]
    fn an_empty_impl_emits_an_empty_methods_slice() {
        let s = expand(quote! { impl A {} });
        assert!(s.contains("const METHODS :"), "got: {s}");
        assert!(s.contains("& []"), "got: {s}");
    }

    #[test]
    fn generic_controllers_carry_their_generics_onto_the_metadata_impl() {
        let s = expand(quote! {
            impl<S> Controller<S> where S: Clone {
                pub async fn index() -> Response { todo!() }
            }
        });
        assert!(
            s.contains("impl < S > :: arcature :: ControllerMetadata for Controller < S >"),
            "got: {s}"
        );
        assert!(s.contains("where S : Clone"), "got: {s}");
    }

    #[test]
    fn rejects_a_private_method() {
        let err = expand_err(quote! {
            impl A { async fn index() -> Response { todo!() } }
        });
        assert_eq!(err.code(), MacroErrorCode::ArcM004);
        assert!(err.to_compile_error().to_string().contains("pub"));
    }

    #[test]
    fn rejects_a_sync_method() {
        let err = expand_err(quote! {
            impl A { pub fn index() -> Response { todo!() } }
        });
        assert_eq!(err.code(), MacroErrorCode::ArcM004);
        assert!(err.to_compile_error().to_string().contains("async"));
    }

    #[test]
    fn rejects_a_method_without_a_return_type() {
        let err = expand_err(quote! {
            impl A { pub async fn index() { } }
        });
        assert_eq!(err.code(), MacroErrorCode::ArcM004);
        assert!(err.to_compile_error().to_string().contains("return type"));
    }

    #[test]
    fn rejects_a_self_receiver() {
        let err = expand_err(quote! {
            impl A { pub async fn index(&self) -> Response { todo!() } }
        });
        assert_eq!(err.code(), MacroErrorCode::ArcM004);
        assert!(err.to_compile_error().to_string().contains("self"));
    }

    #[test]
    fn rejects_input_that_is_not_an_impl_block() {
        let err = expand_err(quote! { pub struct NotAnImpl; });
        assert_eq!(err.code(), MacroErrorCode::ArcM001);
    }
}