rorpc-parse 0.1.7

AST parsing utilities and code generation internals for rorpc proc macros
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
//! Code generation for the `#[rorpc(method, path)]` attribute macro.
//!
//! Parses the attribute arguments, analyses the handler signature, and emits:
//! - The original function unchanged
//! - An `inventory::submit!` for `HandlerMetadata`
//! - An `inventory::submit!` for `HandlerRegistration` (Axum router factory)
//! - `inventory::submit!` blocks for `SchemaRegistration` fallback schemas

use proc_macro2::TokenStream;
use quote::quote;
use syn::{
    Expr, ExprLit, ItemFn, Lit, MetaNameValue, Token,
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
};

use crate::{
    errors::{Error, Result, type_display},
    functions::extract_handler_signature,
    types::{JSON, QUERY, RESULT, innermost_custom_type, is_primitive, try_extract_wrapper},
};

// ---------------------------------------------------------------------------
// Attribute name constants — centralized for easy renaming
// ---------------------------------------------------------------------------

const ATTR_METHOD: &str = "method";
const ATTR_PATH: &str = "path";
const ATTR_DATA: &str = "data";

// ---------------------------------------------------------------------------
// OrpcArgs — parsed from #[orpc(method = "...", path = "...", data = TypePath)]
// ---------------------------------------------------------------------------

/// Parsed arguments for the `#[orpc(...)]` attribute.
pub struct OrpcArgs {
    pub method: String,
    pub path: String,
    pub stream_event: Option<String>,
}

// ---------------------------------------------------------------------------
// MethodShorthandArgs — parsed from #[orpc::get("/path")] or #[orpc::post("/path", data = "Type")]
// ---------------------------------------------------------------------------

/// Parsed arguments for method-specific shorthand macros like `#[orpc::get("/path")]`.
///
/// Syntax: `#[orpc::get("/path")]` or `#[orpc::post("/path", data = "StreamEvent")]`
pub struct MethodShorthandArgs {
    pub path: String,
    pub data: Option<String>,
}

impl Parse for MethodShorthandArgs {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        // First token must be a string literal (the path)
        let path_lit: syn::LitStr = input.parse()?;
        let path = path_lit.value();

        // Optional: comma + data = "Type"
        let mut data = None;

        if input.peek(Token![,]) {
            input.parse::<Token![,]>()?;

            let pairs = Punctuated::<MetaNameValue, Token![,]>::parse_terminated(input)?;

            for pair in &pairs {
                let key = pair
                    .path
                    .get_ident()
                    .map(|i| i.to_string())
                    .unwrap_or_default();

                let span = pair
                    .path
                    .get_ident()
                    .map(|i| i.span())
                    .unwrap_or_else(proc_macro2::Span::call_site);

                match key.as_str() {
                    ATTR_DATA => match &pair.value {
                        Expr::Lit(ExprLit {
                            lit: Lit::Str(s), ..
                        }) => {
                            data = Some(s.value());
                        }
                        Expr::Path(expr_path) => {
                            let type_path = syn::TypePath {
                                attrs: vec![],
                                qself: expr_path.qself.clone(),
                                path: expr_path.path.clone(),
                            };
                            data = Some(type_display(&syn::Type::Path(type_path)));
                        }
                        _ => {
                            return Err(syn::Error::new(
                                span,
                                format!(
                                    "{} must be a string literal (\"StreamEvent\") or type path (StreamEvent)",
                                    ATTR_DATA
                                ),
                            ));
                        }
                    },
                    _ => {
                        return Err(syn::Error::new(
                            span,
                            Error::unknown_key(span, &key, &[ATTR_DATA]).to_string(),
                        ));
                    }
                }
            }
        }

        Ok(MethodShorthandArgs { path, data })
    }
}

/// Convert method shorthand args to standard OrpcArgs.
///
/// This allows method-specific macros like `#[orpc::get("/path")]` to reuse
/// all the existing codegen logic without duplication.
impl MethodShorthandArgs {
    pub fn into_orpc_args(self, method: &str) -> OrpcArgs {
        OrpcArgs {
            method: method.to_uppercase(),
            path: self.path,
            stream_event: self.data,
        }
    }
}

const VALID_KEYS: &[&str] = &[ATTR_METHOD, ATTR_PATH, ATTR_DATA];

impl Parse for OrpcArgs {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let pairs = Punctuated::<MetaNameValue, Token![,]>::parse_terminated(input)?;

        let mut method = None;
        let mut path = None;
        let mut stream_event = None;

        for pair in &pairs {
            let key = pair
                .path
                .get_ident()
                .map(|i| i.to_string())
                .unwrap_or_default();

            let span = pair
                .path
                .get_ident()
                .map(|i| i.span())
                .unwrap_or_else(proc_macro2::Span::call_site);

            match key.as_str() {
                ATTR_METHOD => {
                    if let Expr::Lit(ExprLit {
                        lit: Lit::Str(s), ..
                    }) = &pair.value
                    {
                        method = Some(s.value().to_uppercase());
                    } else {
                        return Err(syn::Error::new(
                            span,
                            Error::invalid_attr_value(
                                span,
                                &key,
                                "a string literal",
                                "non-string expression",
                            )
                            .to_string(),
                        ));
                    }
                }
                ATTR_PATH => {
                    if let Expr::Lit(ExprLit {
                        lit: Lit::Str(s), ..
                    }) = &pair.value
                    {
                        path = Some(s.value());
                    } else {
                        return Err(syn::Error::new(
                            span,
                            Error::invalid_attr_value(
                                span,
                                &key,
                                "a string literal",
                                "non-string expression",
                            )
                            .to_string(),
                        ));
                    }
                }
                ATTR_DATA => {
                    match &pair.value {
                        // String literal: data = "StreamEvent" or data = "module::StreamEvent"
                        // Preferred syntax for IDE support
                        Expr::Lit(ExprLit {
                            lit: Lit::Str(s), ..
                        }) => {
                            stream_event = Some(s.value());
                        }
                        // Type path: data = StreamEvent (backward compatibility)
                        Expr::Path(expr_path) => {
                            let type_path = syn::TypePath {
                                attrs: vec![],
                                qself: expr_path.qself.clone(),
                                path: expr_path.path.clone(),
                            };
                            stream_event = Some(type_display(&syn::Type::Path(type_path)));
                        }
                        _ => {
                            return Err(syn::Error::new(
                                span,
                                format!(
                                    "{} must be a string literal (\"StreamEvent\") or type path (StreamEvent)",
                                    ATTR_DATA
                                ),
                            ));
                        }
                    }
                }
                _ => {
                    return Err(syn::Error::new(
                        span,
                        Error::unknown_key(span, &key, VALID_KEYS).to_string(),
                    ));
                }
            }
        }

        let method = method.ok_or_else(|| {
            syn::Error::new(
                proc_macro2::Span::call_site(),
                Error::missing_required_attr(
                    proc_macro2::Span::call_site(),
                    ATTR_METHOD,
                    "add `method = \"GET\"` to #[orpc]",
                )
                .to_string(),
            )
        })?;

        let path = path.ok_or_else(|| {
            syn::Error::new(
                proc_macro2::Span::call_site(),
                Error::missing_required_attr(
                    proc_macro2::Span::call_site(),
                    ATTR_PATH,
                    "add `path = \"/your/route\"` to #[orpc]",
                )
                .to_string(),
            )
        })?;

        Ok(OrpcArgs {
            method,
            path,
            stream_event,
        })
    }
}

// ---------------------------------------------------------------------------
// expand_orpc
// ---------------------------------------------------------------------------

/// Generate the full expansion for `#[rorpc::route(method, path)]` or shorthand macros.
///
/// Returns the original function unchanged plus all inventory registrations.
pub fn expand_orpc(args: OrpcArgs, func: ItemFn) -> TokenStream {
    match try_expand_orpc(args, func) {
        Ok(ts) => ts,
        Err(e) => e.to_compile_error(),
    }
}

fn try_expand_orpc(args: OrpcArgs, func: ItemFn) -> Result<TokenStream> {
    let sig = extract_handler_signature(&func)?;

    let fn_name = &func.sig.ident;
    let fn_name_str = sig.fn_name.as_str();
    let method = &args.method;
    let path = &args.path;

    let output_type_str = type_display(&sig.output_type);

    let error_type_token = match &sig.error_type {
        Some(ty) => {
            let s = type_display(ty);
            quote! { Some(#s) }
        }
        None => quote! { None },
    };

    let stream_event_token = match &args.stream_event {
        Some(type_name) => {
            let s = type_name.as_str();
            quote! { Some(#s) }
        }
        None => quote! { None },
    };

    let input_type_str = match &sig.input_type {
        Some(ty) => type_display(ty),
        None => "()".to_string(),
    };

    let query_type_token = match &sig.query_type {
        Some(ty) => {
            let s = type_display(ty);
            quote! { Some(#s) }
        }
        None => quote! { None },
    };

    // Encode path param types as comma-separated string: "i32,String"
    // Order matches the path template param order (declaration order in signature)
    let path_param_types_str = sig
        .path_params
        .iter()
        .map(|(_, ty)| type_display(ty))
        .collect::<Vec<_>>()
        .join(",");

    let registration = emit_handler_registration(fn_name, method, path, &sig.state_type);
    let schema_registrations = emit_schema_registrations(&func);

    Ok(quote! {
        #func

        ::rorpc::inventory::submit! {
            ::rorpc::HandlerMetadata {
                name: #fn_name_str,
                method: #method,
                path: #path,
                input_type_name: #input_type_str,
                query_type_name: #query_type_token,
                output_type_name: #output_type_str,
                module_path: ::std::module_path!(),
                namespace: None,
                error_type_name: #error_type_token,
                stream_event_type_name: #stream_event_token,
                path_param_types: #path_param_types_str,
            }
        }

        #registration
        #schema_registrations
    })
}

// ---------------------------------------------------------------------------
// Handler registration factory
// ---------------------------------------------------------------------------

fn emit_handler_registration(
    fn_name: &syn::Ident,
    method: &str,
    path: &str,
    state_type: &Option<syn::Type>,
) -> TokenStream {
    if let Some(state_ty) = state_type {
        quote! {
            ::rorpc::inventory::submit! {
                ::rorpc::HandlerRegistration {
                    path: #path,
                    method: #method,
                    factory: |state: ::std::sync::Arc<dyn ::std::any::Any + Send + Sync>, final_path: &str| {
                        use ::axum::routing::{delete, get, patch, post, put};
                        let method_router = match #method {
                            "GET"    => get(#fn_name),
                            "POST"   => post(#fn_name),
                            "PUT"    => put(#fn_name),
                            "PATCH"  => patch(#fn_name),
                            "DELETE" => delete(#fn_name),
                            _        => post(#fn_name),
                        };
                        if let Some(typed_state) = state.downcast_ref::<#state_ty>() {
                            ::axum::Router::new()
                                .route(final_path, method_router)
                                .with_state(typed_state.clone())
                        } else {
                            ::axum::Router::new()
                        }
                    },
                }
            }
        }
    } else {
        quote! {
            ::rorpc::inventory::submit! {
                ::rorpc::HandlerRegistration {
                    path: #path,
                    method: #method,
                    factory: |_state: ::std::sync::Arc<dyn ::std::any::Any + Send + Sync>, final_path: &str| {
                        use ::axum::routing::{delete, get, patch, post, put};
                        let method_router = match #method {
                            "GET"    => get(#fn_name),
                            "POST"   => post(#fn_name),
                            "PUT"    => put(#fn_name),
                            "PATCH"  => patch(#fn_name),
                            "DELETE" => delete(#fn_name),
                            _        => post(#fn_name),
                        };
                        ::axum::Router::new().route(final_path, method_router)
                    },
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Schema registrations — z.unknown() fallback for types without #[derive(ZodTs)]
// ---------------------------------------------------------------------------

fn emit_schema_registrations(func: &ItemFn) -> TokenStream {
    let mut seen = std::collections::HashSet::new();
    let mut registrations = Vec::new();

    // Collect candidate types from Json<T> and Query<T> params and return type
    let mut candidates: Vec<&syn::Type> = Vec::new();

    for arg in &func.sig.inputs {
        if let syn::FnArg::Typed(pat_type) = arg {
            // Check for Json<T>
            if let Some(m) = try_extract_wrapper(&pat_type.ty, JSON)
                && let Some(inner) = m.first_type()
            {
                candidates.push(inner);
            }
            // Check for Query<T>
            if let Some(m) = try_extract_wrapper(&pat_type.ty, QUERY)
                && let Some(inner) = m.first_type()
            {
                candidates.push(inner);
            }
        }
    }

    if let syn::ReturnType::Type(_, ty) = &func.sig.output {
        // Handle both Json<T> and Result<Json<T>, E>
        if let Some(m) = try_extract_wrapper(ty, JSON) {
            if let Some(inner) = m.first_type() {
                candidates.push(inner);
            }
        } else if let Some(result_m) = try_extract_wrapper(ty, RESULT)
            && let Some(first) = result_m.first_type()
            && let Some(json_m) = try_extract_wrapper(first, JSON)
            && let Some(inner) = json_m.first_type()
        {
            candidates.push(inner);
        }
    }

    for ty in candidates {
        if let Some(custom_ty) = innermost_custom_type(ty) {
            if is_primitive(custom_ty) {
                continue;
            }
            let name = type_display(custom_ty);
            if !seen.insert(name.clone()) {
                continue;
            }
            let fallback = format!(
                "z.unknown() /* add #[derive(ZodTs)] to {} for a real schema */",
                name
            );
            registrations.push(quote! {
                ::rorpc::inventory::submit! {
                    ::rorpc::SchemaRegistration {
                        type_name: #name,
                        zod_ts: || #fallback.to_string(),
                        dependent_types: || vec![],
                    }
                }
            });
        }
    }

    quote! { #(#registrations)* }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn parse_data_type_string() {
        // Test that data = "StreamEvent" (string literal) parses correctly
        let args: OrpcArgs = syn::parse_quote! {
            method = "GET", path = "/stream", data = "StreamEvent"
        };

        assert_eq!(args.method, "GET");
        assert_eq!(args.path, "/stream");
        assert_eq!(args.stream_event, Some("StreamEvent".to_string()));
    }

    #[test]
    fn parse_data_qualified_path_string() {
        // Test that data = "crate::models::StreamEvent" works
        let args: OrpcArgs = syn::parse_quote! {
            method = "GET", path = "/stream", data = "crate::models::StreamEvent"
        };

        assert_eq!(
            args.stream_event,
            Some("crate::models::StreamEvent".to_string())
        );
    }

    #[test]
    fn parse_data_type_path_backward_compat() {
        // Test backward compatibility: data = StreamEvent (bare path)
        let args: OrpcArgs = syn::parse_quote! {
            method = "GET", path = "/stream", data = StreamEvent
        };

        assert_eq!(args.method, "GET");
        assert_eq!(args.path, "/stream");
        assert_eq!(args.stream_event, Some("StreamEvent".to_string()));
    }

    #[test]
    fn parse_without_data() {
        // Test that data is optional
        let args: OrpcArgs = syn::parse_quote! {
            method = "POST", path = "/create"
        };

        assert_eq!(args.method, "POST");
        assert_eq!(args.path, "/create");
        assert_eq!(args.stream_event, None);
    }

    #[test]
    fn data_type_converts_to_string_literal() {
        // Verify that when we generate the metadata, data becomes a string literal
        let args: OrpcArgs = syn::parse_quote! {
            method = "GET", path = "/stream", data = "StreamEvent"
        };

        let func: syn::ItemFn = parse_quote! {
            async fn stream_test() -> Sse<impl Stream<Item = Event>> {
                todo!()
            }
        };

        let result = try_expand_orpc(args, func);
        assert!(result.is_ok(), "expand_orpc should succeed");

        // Check that the generated code contains Some("StreamEvent") as a string literal
        let tokens = result.unwrap().to_string();

        // quote! serialises with spaces between tokens, so `Some("StreamEvent")` becomes
        // `Some ("StreamEvent")`. Check the field name and the quoted value separately.
        assert!(
            tokens.contains("stream_event_type_name") && tokens.contains(r#""StreamEvent""#),
            "Generated code should contain stream_event_type_name: Some(\"StreamEvent\"), got: {}",
            tokens
        );
        // Also assert it is NOT a bare identifier (which would be a type error at compile time)
        assert!(
            !tokens.contains("Some (StreamEvent)") && !tokens.contains("Some(StreamEvent)"),
            "stream_event_type_name must be a string literal, not a bare identifier"
        );
    }
}

// ---------------------------------------------------------------------------
// MethodShorthandArgs tests
// ---------------------------------------------------------------------------

#[test]
fn parse_shorthand_path_only() {
    // Test: #[rorpc::get("/planet/list")]
    let args: MethodShorthandArgs = syn::parse_quote! { "/planet/list" };

    assert_eq!(args.path, "/planet/list");
    assert_eq!(args.data, None);
}

#[test]
fn parse_shorthand_with_data_string() {
    // Test: #[rorpc::get("/stream", data = "EventData")]
    let args: MethodShorthandArgs = syn::parse_quote! { "/stream", data = "EventData" };

    assert_eq!(args.path, "/stream");
    assert_eq!(args.data, Some("EventData".to_string()));
}

#[test]
fn parse_shorthand_with_qualified_data() {
    // Test: #[rorpc::get("/stream", data = "models::EventData")]
    let args: MethodShorthandArgs = syn::parse_quote! { "/stream", data = "models::EventData" };

    assert_eq!(args.path, "/stream");
    assert_eq!(args.data, Some("models::EventData".to_string()));
}

#[test]
fn parse_shorthand_with_data_type_path() {
    // Test backward compat: #[rorpc::get("/stream", data = EventData)]
    let args: MethodShorthandArgs = syn::parse_quote! { "/stream", data = EventData };

    assert_eq!(args.path, "/stream");
    assert_eq!(args.data, Some("EventData".to_string()));
}

#[test]
fn shorthand_converts_to_orpc_args() {
    let shorthand: MethodShorthandArgs = syn::parse_quote! { "/planet/list" };
    let args = shorthand.into_orpc_args("GET");

    assert_eq!(args.method, "GET");
    assert_eq!(args.path, "/planet/list");
    assert_eq!(args.stream_event, None);
}

#[test]
fn shorthand_with_data_converts_to_orpc_args() {
    let shorthand: MethodShorthandArgs = syn::parse_quote! { "/stream", data = "EventData" };
    let args = shorthand.into_orpc_args("GET");

    assert_eq!(args.method, "GET");
    assert_eq!(args.path, "/stream");
    assert_eq!(args.stream_event, Some("EventData".to_string()));
}

#[test]
fn shorthand_method_normalized_to_uppercase() {
    let shorthand: MethodShorthandArgs = syn::parse_quote! { "/test" };
    let args = shorthand.into_orpc_args("get"); // lowercase input

    assert_eq!(args.method, "GET"); // should be uppercase
}