ironic-macros 1.0.8

Procedural macros for the Ironic application framework
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
use proc_macro2::TokenStream;
use quote::quote;
use syn::{
    Attribute, Expr, FnArg, ImplItem, ImplItemFn, ItemImpl, Lit, LitInt, LitStr, Meta, Pat,
    ReturnType, Token, Type, parse::Parse, parse::ParseStream, parse2, punctuated::Punctuated,
    spanned::Spanned,
};

use crate::controller::take_components;

const HTTP_METHODS: &[(&str, &str)] = &[
    ("get", "GET"),
    ("post", "POST"),
    ("put", "PUT"),
    ("patch", "PATCH"),
    ("delete", "DELETE"),
    ("head", "HEAD"),
    ("options", "OPTIONS"),
];

pub(crate) fn expand(attribute: TokenStream, item: TokenStream) -> syn::Result<TokenStream> {
    if !attribute.is_empty() {
        return Err(syn::Error::new_spanned(
            attribute,
            "`#[routes]` does not accept arguments",
        ));
    }
    let mut item = parse2::<ItemImpl>(item)?;
    if item.trait_.is_some() {
        return Err(syn::Error::new_spanned(
            &item.self_ty,
            "`#[routes]` requires an inherent impl",
        ));
    }
    if !item.generics.params.is_empty() {
        return Err(syn::Error::new_spanned(
            &item.generics,
            "`#[routes]` does not support generic impl blocks",
        ));
    }
    let self_ty = item.self_ty.clone();
    let mut definitions = Vec::new();

    for impl_item in &mut item.items {
        let ImplItem::Fn(method) = impl_item else {
            continue;
        };
        let Some((http_method, path)) = take_http_method(&mut method.attrs)? else {
            continue;
        };
        definitions.push(expand_method(&self_ty, method, &http_method, &path)?);
    }

    Ok(quote! {
        #item

        impl #self_ty {
            #[doc(hidden)]
            pub fn route_definitions() -> ::std::vec::Vec<::ironic::RouteDefinition> {
                use ::ironic::OpenApiRouteExt;
                ::std::vec![#(#definitions),*]
            }
        }
    })
}

fn take_http_method(attrs: &mut Vec<Attribute>) -> syn::Result<Option<(syn::Ident, LitStr)>> {
    let mut route = None;
    let mut retained = Vec::new();
    for attr in attrs.drain(..) {
        let Some(name) = attr.path().get_ident().map(ToString::to_string) else {
            retained.push(attr);
            continue;
        };
        let Some((_, constant)) = HTTP_METHODS.iter().find(|(method, _)| *method == name) else {
            retained.push(attr);
            continue;
        };
        if route.is_some() {
            return Err(syn::Error::new_spanned(
                attr,
                "a handler may declare only one HTTP method attribute",
            ));
        }
        let path = match &attr.meta {
            Meta::Path(_) => LitStr::new("/", attr.span()),
            _ => attr.parse_args::<LitStr>()?,
        };
        route = Some((syn::Ident::new(constant, attr.span()), path));
    }
    *attrs = retained;
    Ok(route)
}

// ── OpenAPI attribute types ──────────────────────────────────────────

struct OpenApiAttrs {
    summary: Option<String>,
    tags: Vec<String>,
    operation_id: Option<String>,
    security: Vec<String>,
    responses: Vec<ResponseAttr>,
    request_body: Option<RequestBodyAttr>,
}

struct ResponseAttr {
    status: String,
    description: String,
    json_type: Option<Type>,
}

struct RequestBodyAttr {
    json_type: Type,
}

/// Parses `#[api(key = "value", ...)]` attributes (`summary`, `tag`, `operation_id`).
fn take_openapi_fields(attrs: &mut Vec<Attribute>) -> syn::Result<OpenApiAttrs> {
    let mut summary = None;
    let mut tags = Vec::new();
    let mut operation_id = None;
    let mut security = Vec::new();
    let mut retained = Vec::new();

    for attr in attrs.drain(..) {
        if !attr.path().is_ident("api") {
            retained.push(attr);
            continue;
        }
        let nested = attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?;
        for meta in nested {
            match meta {
                Meta::NameValue(nv) if nv.path.is_ident("summary") => {
                    let s = lit_str_from_expr(&nv.value)?;
                    summary = Some(s.value());
                }
                Meta::NameValue(nv) if nv.path.is_ident("tag") => {
                    let s = lit_str_from_expr(&nv.value)?;
                    tags.push(s.value());
                }
                Meta::NameValue(nv) if nv.path.is_ident("operation_id") => {
                    let s = lit_str_from_expr(&nv.value)?;
                    operation_id = Some(s.value());
                }
                Meta::NameValue(nv) if nv.path.is_ident("security") => {
                    let s = lit_str_from_expr(&nv.value)?;
                    security.push(s.value());
                }
                other => {
                    return Err(syn::Error::new_spanned(
                        other,
                        "expected `summary`, `tag`, `operation_id`, or `security`",
                    ));
                }
            }
        }
    }
    *attrs = retained;

    let responses = take_response_attrs(attrs)?;
    let request_body = take_request_body_attr(attrs)?;

    Ok(OpenApiAttrs {
        summary,
        tags,
        operation_id,
        security,
        responses,
        request_body,
    })
}

/// Parses `#[resp(status, "description")]` or `#[resp(status, "description", json = Type)]`.
fn take_response_attrs(attrs: &mut Vec<Attribute>) -> syn::Result<Vec<ResponseAttr>> {
    let mut responses = Vec::new();
    let mut retained = Vec::new();

    for attr in attrs.drain(..) {
        if !attr.path().is_ident("resp") {
            retained.push(attr);
            continue;
        }
        let args: ResponseArgs = attr.parse_args()?;
        responses.push(ResponseAttr {
            status: args.status.to_string(),
            description: args.description.value(),
            json_type: args.json_type,
        });
    }
    *attrs = retained;
    Ok(responses)
}

struct ResponseArgs {
    status: LitInt,
    description: LitStr,
    json_type: Option<Type>,
}

impl Parse for ResponseArgs {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let status: LitInt = input.parse()?;
        input.parse::<Token![,]>()?;
        let description: LitStr = input.parse()?;
        let mut json_type = None;
        if input.parse::<Token![,]>().is_ok() {
            let key: syn::Ident = input.parse()?;
            if key != "json" {
                return Err(syn::Error::new(key.span(), "expected `json`"));
            }
            input.parse::<Token![=]>()?;
            json_type = Some(input.parse()?);
        }
        Ok(ResponseArgs {
            status,
            description,
            json_type,
        })
    }
}

/// Parses `#[body(json = Type)]`.
fn take_request_body_attr(attrs: &mut Vec<Attribute>) -> syn::Result<Option<RequestBodyAttr>> {
    let mut result = None;
    let mut retained = Vec::new();

    for attr in attrs.drain(..) {
        if !attr.path().is_ident("body") {
            retained.push(attr);
            continue;
        }
        let args: RequestBodyArgs = attr.parse_args()?;
        result = Some(RequestBodyAttr {
            json_type: args.json_type,
        });
    }
    *attrs = retained;
    Ok(result)
}

struct RequestBodyArgs {
    json_type: Type,
}

impl Parse for RequestBodyArgs {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let key: syn::Ident = input.parse()?;
        if key != "json" {
            return Err(syn::Error::new(key.span(), "expected `json`"));
        }
        input.parse::<Token![=]>()?;
        let json_type: Type = input.parse()?;
        Ok(RequestBodyArgs { json_type })
    }
}

fn generate_openapi_call(openapi: &OpenApiAttrs) -> Option<TokenStream> {
    if openapi.summary.is_none()
        && openapi.tags.is_empty()
        && openapi.operation_id.is_none()
        && openapi.security.is_empty()
        && openapi.responses.is_empty()
        && openapi.request_body.is_none()
    {
        return None;
    }

    let mut calls: Vec<TokenStream> = Vec::new();

    if let Some(ref s) = openapi.summary {
        calls.push(quote! { .summary(#s) });
    }
    if let Some(ref id) = openapi.operation_id {
        calls.push(quote! { .operation_id(#id) });
    }
    for tag in &openapi.tags {
        calls.push(quote! { .tag(#tag) });
    }
    for scheme in &openapi.security {
        calls.push(quote! {
            .security(#scheme, Vec::<String>::new())
        });
    }

    if let Some(ref rb) = openapi.request_body {
        let ty = &rb.json_type;
        calls.push(quote! {
            .request_body(::ironic::OpenApiRequestBody::json::<#ty>())
        });
    }

    for resp in &openapi.responses {
        let status_str = &resp.status;
        let desc = &resp.description;
        if let Some(ref json_ty) = resp.json_type {
            calls.push(quote! {
                .response(#status_str, ::ironic::OpenApiResponse::new(#desc).json::<#json_ty>())
            });
        } else {
            calls.push(quote! {
                .response(#status_str, ::ironic::OpenApiResponse::new(#desc))
            });
        }
    }

    Some(quote! {
        .openapi(::ironic::OpenApiOperation::new() #(#calls)*)
    })
}

// ── Core expand_method ───────────────────────────────────────────────

fn expand_method(
    self_ty: &Type,
    method: &mut ImplItemFn,
    http_method: &syn::Ident,
    path: &LitStr,
) -> syn::Result<TokenStream> {
    if method.sig.asyncness.is_none() {
        return Err(syn::Error::new_spanned(
            method.sig.fn_token,
            "route handlers must be async",
        ));
    }
    if !method.sig.generics.params.is_empty() {
        return Err(syn::Error::new_spanned(
            &method.sig.generics,
            "route handlers cannot be generic",
        ));
    }
    if matches!(method.sig.output, ReturnType::Default) {
        return Err(syn::Error::new_spanned(
            &method.sig,
            "route handlers must return `Result<_, HttpError>`",
        ));
    }

    let Some(FnArg::Receiver(receiver)) = method.sig.inputs.first() else {
        return Err(syn::Error::new_spanned(
            &method.sig,
            "route handlers require an `&self` receiver",
        ));
    };
    if receiver.reference.is_none() || receiver.mutability.is_some() {
        return Err(syn::Error::new_spanned(
            receiver,
            "route handlers require an immutable `&self` receiver",
        ));
    }

    let guards = take_components(&mut method.attrs, "guard")?;
    let interceptors = take_components(&mut method.attrs, "interceptor")?;
    let middlewares = take_components(&mut method.attrs, "middleware")?;
    let cache_ttl = take_cache_ttl(&mut method.attrs)?;
    let openapi = take_openapi_fields(&mut method.attrs)?;

    let mut extractors = Vec::new();
    let mut bindings = Vec::new();
    let mut arguments = Vec::new();
    let mut parameter_pipes: Vec<Vec<TokenStream>> = Vec::new();

    for (index, argument) in method.sig.inputs.iter_mut().skip(1).enumerate() {
        let FnArg::Typed(argument) = argument else {
            unreachable!()
        };
        let Pat::Ident(pattern) = argument.pat.as_ref() else {
            return Err(syn::Error::new_spanned(
                &argument.pat,
                "route parameter patterns must be identifiers",
            ));
        };
        let argument_name = &pattern.ident;
        let argument_type = &argument.ty;
        let (extractor, pipes) = take_extractor(&mut argument.attrs, argument_name, argument_type)?;
        extractors.push(extractor);
        parameter_pipes.push(pipes);
        bindings.push(quote!(let #argument_name = arguments.take::<#argument_type>(#index)?;));
        arguments.push(argument_name);
    }

    let method_name = &method.sig.ident;
    let cache_call = cache_ttl.map(|ttl| {
        quote! { .cache(::ironic::CacheMetadata::new(#ttl)) }
    });
    let openapi_call = generate_openapi_call(&openapi);
    let parameter_calls: Vec<TokenStream> = extractors
        .into_iter()
        .zip(parameter_pipes)
        .map(|(extractor, pipes)| {
            if pipes.is_empty() {
                quote! { .parameter(#extractor) }
            } else {
                quote! { .parameter_with_pipes(#extractor, [#(#pipes),*]) }
            }
        })
        .collect();

    Ok(quote! {
        ::ironic::RouteDefinition::new(
            ::ironic::HttpMethod::#http_method,
            #path,
            ::std::stringify!(#method_name),
            ::ironic::handler_fn(
                |controller: ::std::sync::Arc<#self_ty>, mut arguments| async move {
                    #(#bindings)*
                    controller.#method_name(#(#arguments),*).await
                },
            ),
        )
        .expect("the macro-validated route path is valid")
        #(#parameter_calls)*
        #(.guard(#guards))*
        #(.interceptor(#interceptors))*
        #(.middleware(#middlewares))*
        #cache_call
        #openapi_call
    })
}

fn take_extractor(
    attrs: &mut Vec<Attribute>,
    argument_name: &syn::Ident,
    argument_type: &Type,
) -> syn::Result<(TokenStream, Vec<TokenStream>)> {
    let mut extractor = None;
    let mut pipes = Vec::new();
    let mut retained = Vec::new();
    for attr in attrs.drain(..) {
        let Some(name) = attr.path().get_ident().map(ToString::to_string) else {
            retained.push(attr);
            continue;
        };
        match name.as_str() {
            "body" => {
                let value = quote!(::ironic::JsonBody::<#argument_type>::new());
                if extractor.replace(value).is_some() {
                    return Err(syn::Error::new_spanned(
                        argument_name,
                        "a route parameter must have exactly one extractor attribute",
                    ));
                }
            }
            "form" => {
                let value = quote!(::ironic::FormBody::<#argument_type>::new());
                if extractor.replace(value).is_some() {
                    return Err(syn::Error::new_spanned(
                        argument_name,
                        "a route parameter must have exactly one extractor attribute",
                    ));
                }
            }
            "query" => {
                let value = quote!(::ironic::QueryParameters::<#argument_type>::new());
                if extractor.replace(value).is_some() {
                    return Err(syn::Error::new_spanned(
                        argument_name,
                        "a route parameter must have exactly one extractor attribute",
                    ));
                }
            }
            "param" => {
                let name = optional_name(&attr, argument_name)?;
                let value = quote!(::ironic::PathParameter::<#argument_type>::new(#name));
                if extractor.replace(value).is_some() {
                    return Err(syn::Error::new_spanned(
                        argument_name,
                        "a route parameter must have exactly one extractor attribute",
                    ));
                }
            }
            "header" => {
                let name = optional_name(&attr, argument_name)?;
                let value = quote!(::ironic::HeaderParameter::<#argument_type>::new(#name));
                if extractor.replace(value).is_some() {
                    return Err(syn::Error::new_spanned(
                        argument_name,
                        "a route parameter must have exactly one extractor attribute",
                    ));
                }
            }
            "decorator" => {
                let extractor_type: Type = attr.parse_args()?;
                let value = quote!(#extractor_type::new());
                if extractor.replace(value).is_some() {
                    return Err(syn::Error::new_spanned(
                        argument_name,
                        "a route parameter must have exactly one extractor attribute",
                    ));
                }
            }
            "pipe" => {
                let pipe_fn: Expr = attr.parse_args()?;
                pipes.push(quote!(#pipe_fn()));
            }
            _ => {
                retained.push(attr);
            }
        }
    }
    *attrs = retained;
    let extractor = extractor.ok_or_else(|| {
        syn::Error::new_spanned(
            argument_name,
            "route parameters require one of `#[body]`, `#[form]`, `#[query]`, `#[param]`, `#[header]`, or `#[decorator(ExtractorType)]`",
        )
    })?;
    Ok((extractor, pipes))
}

fn optional_name(attr: &Attribute, argument_name: &syn::Ident) -> syn::Result<LitStr> {
    match &attr.meta {
        Meta::Path(_) => Ok(LitStr::new(
            &argument_name.to_string(),
            argument_name.span(),
        )),
        _ => attr.parse_args::<LitStr>(),
    }
}

struct CacheArgs {
    ttl_secs: u64,
}

impl Parse for CacheArgs {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let key: syn::Ident = input.parse()?;
        if key != "ttl_secs" {
            return Err(syn::Error::new(key.span(), "expected `ttl_secs`"));
        }
        input.parse::<syn::Token![=]>()?;
        let value: LitInt = input.parse()?;
        Ok(CacheArgs {
            ttl_secs: value.base10_parse()?,
        })
    }
}

fn lit_str_from_expr(expr: &Expr) -> syn::Result<LitStr> {
    match expr {
        Expr::Lit(expr_lit) => match &expr_lit.lit {
            Lit::Str(s) => Ok(s.clone()),
            _ => Err(syn::Error::new_spanned(expr, "expected a string literal")),
        },
        _ => Err(syn::Error::new_spanned(expr, "expected a string literal")),
    }
}

fn take_cache_ttl(attrs: &mut Vec<Attribute>) -> syn::Result<Option<u64>> {
    let mut ttl = None;
    let mut retained = Vec::new();
    for attr in attrs.drain(..) {
        if attr.path().is_ident("cache") {
            let args: CacheArgs = attr.parse_args()?;
            ttl = Some(args.ttl_secs);
        } else {
            retained.push(attr);
        }
    }
    *attrs = retained;
    Ok(ttl)
}