rorpc-parse 0.1.1

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
//! 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},
};

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

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

const VALID_KEYS: &[&str] = &["method", "path", "stream_event"];

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() {
                "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(),
                        ));
                    }
                }
                "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(),
                        ));
                    }
                }
                "stream_event" => {
                    // Accept a type path: stream_event = StreamEvent
                    if let Expr::Path(expr_path) = &pair.value {
                        let type_path = syn::TypePath {
                            attrs: vec![],
                            qself: expr_path.qself.clone(),
                            path: expr_path.path.clone(),
                        };
                        stream_event = Some(syn::Type::Path(type_path));
                    } else {
                        return Err(syn::Error::new(
                            span,
                            "stream_event must be a type path (e.g., StreamEvent or module::StreamEvent)",
                        ));
                    }
                }
                _ => {
                    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(),
                    "method",
                    "add `method = \"GET\"` to #[rorpc]",
                )
                .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(),
                    "path",
                    "add `path = \"/your/route\"` to #[rorpc]",
                )
                .to_string(),
            )
        })?;

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

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

/// Generate the full expansion for `#[orpc(method, path)] async fn handler(...)`.
///
/// 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(ty) => {
            let s = type_display(ty);
            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 },
    };

    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!(),
                error_type_name: #error_type_token,
                stream_event_type_name: #stream_event_token,
            }
        }

        #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>| {
                        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(#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>| {
                        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(#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_stream_event_type_path() {
        // Test that stream_event = StreamEvent (without quotes) parses correctly
        let args: OrpcArgs = syn::parse_quote! {
            method = "GET", path = "/stream", stream_event = StreamEvent
        };

        assert_eq!(args.method, "GET");
        assert_eq!(args.path, "/stream");
        assert!(args.stream_event.is_some());

        // Verify type_display produces the correct string
        let ty = args.stream_event.unwrap();
        let type_str = crate::errors::type_display(&ty);
        assert_eq!(type_str, "StreamEvent");
    }

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

        assert!(args.stream_event.is_some());
        let ty = args.stream_event.unwrap();
        let type_str = crate::errors::type_display(&ty);
        assert_eq!(type_str, "crate::models::StreamEvent");
    }

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

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

    #[test]
    fn stream_event_type_converts_to_string_literal() {
        // This is the critical test for the bug fix
        // Verify that when we generate the metadata, stream_event becomes a string literal
        let args: OrpcArgs = syn::parse_quote! {
            method = "GET", path = "/stream", stream_event = 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"
        );
    }
}