rorpc-parse 0.1.4

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
//! Handler function signature analysis.
//!
//! Extracts a fully-typed [`HandlerSignature`] from a `syn::ItemFn` using
//! AST-based type inspection via [`crate::types`]. All type matching is done
//! on path segment idents — never on string representations.

use proc_macro2::Span;
use syn::{FnArg, ItemFn, ReturnType, Type, spanned::Spanned};

use crate::{
    errors::{Error, Result},
    types::{JSON, PATH, QUERY, RESULT, SSE, STATE, try_extract_wrapper},
};

// ---------------------------------------------------------------------------
// HandlerSignature
// ---------------------------------------------------------------------------

/// Fully analysed handler function signature.
///
/// Produced by [`extract_handler_signature`]. All fields are resolved against
/// the actual AST — no string-based type inference.
#[derive(Debug)]
pub struct HandlerSignature {
    /// The function's identifier, e.g. `"list_planets"`.
    pub fn_name: String,
    /// Span of the function identifier for error reporting.
    pub fn_span: Span,
    /// The `S` in a `State<S>` parameter, if present.
    pub state_type: Option<Type>,
    /// The `T` in a `Json<T>` parameter, if present.
    pub input_type: Option<Type>,
    /// The `T` in a `Query<T>` parameter, if present.
    pub query_type: Option<Type>,
    /// Ordered list of `(binding_name, rust_type)` pairs from `Path<T>` parameters.
    ///
    /// E.g., `Path(id): Path<i32>` → `[("id", Type::i32)]`
    /// Multiple params: `Path(id): Path<i32>, Path(slug): Path<String>` → `[("id", i32), ("slug", String)]`
    pub path_params: Vec<(String, Type)>,
    /// The resolved output type:
    /// - `Json<T>` return → `T`
    /// - `Result<Json<T>, E>` return → `T`
    /// - `Sse<...>` return → unit `()` (output type comes from `data` attribute)
    pub output_type: Type,
    /// The `E` in `Result<_, E>`, if present.
    pub error_type: Option<Type>,
    /// Whether the handler returns `Sse<...>` (an SSE streaming response).
    pub is_streaming: bool,
    /// Whether the function is declared `async`.
    pub is_async: bool,
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Extract a [`HandlerSignature`] from a handler function.
///
/// Validates:
/// - The return type is `Json<T>` or `Result<Json<T>, E>` (not a bare type)
/// - Collects `State<S>`, `Json<T>`, and `Query<T>` parameters when present
///
/// Handlers that return neither `Json<T>` nor `Result<Json<T>, E>` are
/// rejected with an error pointing at the return type token.
pub fn extract_handler_signature(func: &ItemFn) -> Result<HandlerSignature> {
    let fn_name = func.sig.ident.to_string();
    let fn_span = func.sig.ident.span();
    let is_async = func.sig.asyncness.is_some();

    let (output_type, error_type, is_streaming) = extract_return_types(&func.sig.output, &fn_name)?;
    let state_type = extract_state_param(&func.sig.inputs);
    let input_type = extract_json_param(&func.sig.inputs);
    let query_type = extract_query_param(&func.sig.inputs);
    let path_params = extract_path_params(&func.sig.inputs);

    Ok(HandlerSignature {
        fn_name,
        fn_span,
        state_type,
        input_type,
        query_type,
        path_params,
        output_type,
        error_type,
        is_streaming,
        is_async,
    })
}

// ---------------------------------------------------------------------------
// Internal extraction helpers
// ---------------------------------------------------------------------------

/// Extract the unwrapped output type, optional error type, and streaming flag
/// from a return type.
///
/// Accepts:
/// - `-> Json<T>` → (T, None, false)
/// - `-> Result<Json<T>, E>` → (T, Some(E), false)
/// - `-> Sse<...>` → ((), None, true)
fn extract_return_types(
    return_type: &ReturnType,
    fn_name: &str,
) -> Result<(Type, Option<Type>, bool)> {
    let ty = match return_type {
        ReturnType::Default => {
            return Err(Error::missing_return_type(
                proc_macro2::Span::call_site(),
                fn_name,
            ));
        }
        ReturnType::Type(_, ty) => ty.as_ref(),
    };

    // Case 1: Sse<...> — streaming handler; output type comes from data attribute
    if try_extract_wrapper(ty, SSE).is_some() {
        let unit: Type = syn::parse_quote! { () };
        return Ok((unit, None, true));
    }

    // Case 2: Json<T>
    if let Some(m) = try_extract_wrapper(ty, JSON) {
        let output = m
            .first_type()
            .ok_or_else(|| Error::empty_generic_args(ty.span(), JSON))?
            .clone();
        return Ok((output, None, false));
    }

    // Case 3: Result<Json<T>, E>
    if let Some(result_match) = try_extract_wrapper(ty, RESULT) {
        let first = result_match
            .first_type()
            .ok_or_else(|| Error::empty_generic_args(ty.span(), RESULT))?;

        let json_match = try_extract_wrapper(first, JSON).ok_or_else(|| {
            Error::invalid_handler_sig(
                first.span(),
                fn_name,
                "Result's first type argument must be Json<T>",
            )
        })?;

        let output = json_match
            .first_type()
            .ok_or_else(|| Error::empty_generic_args(first.span(), JSON))?
            .clone();

        let error_type = result_match.second_type().cloned();
        return Ok((output, error_type, false));
    }

    Err(Error::invalid_handler_sig(
        ty.span(),
        fn_name,
        "return type must be Json<T>, Result<Json<T>, E>, or Sse<impl Stream<...>>",
    ))
}

/// Find a `State<S>` parameter and return the inner `S`.
fn extract_state_param(
    inputs: &syn::punctuated::Punctuated<FnArg, syn::Token![,]>,
) -> Option<Type> {
    for arg in inputs {
        if let FnArg::Typed(pat_type) = arg
            && let Some(m) = try_extract_wrapper(&pat_type.ty, STATE)
        {
            return m.first_type().cloned();
        }
    }
    None
}

/// Find the first `Json<T>` parameter and return the inner `T`.
fn extract_json_param(inputs: &syn::punctuated::Punctuated<FnArg, syn::Token![,]>) -> Option<Type> {
    for arg in inputs {
        if let FnArg::Typed(pat_type) = arg
            && let Some(m) = try_extract_wrapper(&pat_type.ty, JSON)
        {
            return m.first_type().cloned();
        }
    }
    None
}

/// Find the first `Query<T>` parameter and return the inner `T`.
fn extract_query_param(
    inputs: &syn::punctuated::Punctuated<FnArg, syn::Token![,]>,
) -> Option<Type> {
    for arg in inputs {
        if let FnArg::Typed(pat_type) = arg
            && let Some(m) = try_extract_wrapper(&pat_type.ty, QUERY)
        {
            return m.first_type().cloned();
        }
    }
    None
}

/// Collect all `Path<T>` parameters, returning `(binding_name, inner_type)` pairs.
///
/// `Path(id): Path<i32>` → `("id", Type::i32)`
/// Preserves declaration order so types can be zipped with path template params.
fn extract_path_params(
    inputs: &syn::punctuated::Punctuated<FnArg, syn::Token![,]>,
) -> Vec<(String, Type)> {
    let mut params = Vec::new();
    for arg in inputs {
        if let FnArg::Typed(pat_type) = arg
            && let Some(m) = try_extract_wrapper(&pat_type.ty, PATH)
            && let Some(inner) = m.first_type()
        {
            let name = extract_path_binding_name(&pat_type.pat);
            params.push((name, inner.clone()));
        }
    }
    params
}

/// Extract the binding name from a `Path(name)` or `Path { name }` pattern.
///
/// `Path(id)` → `"id"`, falls back to `"param"` for unrecognised patterns.
fn extract_path_binding_name(pat: &syn::Pat) -> String {
    // Most common form: Path(id) — a TupleStruct pattern
    if let syn::Pat::TupleStruct(ts) = pat
        && let Some(first) = ts.elems.first()
        && let syn::Pat::Ident(id) = first
    {
        return id.ident.to_string();
    }
    // Plain ident (uncommon but valid)
    if let syn::Pat::Ident(id) = pat {
        return id.ident.to_string();
    }
    "param".to_string()
}

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

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

    fn sig(func: ItemFn) -> HandlerSignature {
        extract_handler_signature(&func).unwrap()
    }

    fn sig_err(func: ItemFn) -> Error {
        extract_handler_signature(&func).unwrap_err()
    }

    // --- valid signatures ---

    #[test]
    fn json_return_only() {
        let f: ItemFn = parse_quote! {
            async fn handler() -> Json<Planet> {}
        };
        let s = sig(f);
        assert_eq!(s.fn_name, "handler");
        assert_eq!(type_display(&s.output_type), "Planet");
        assert!(s.error_type.is_none());
        assert!(s.input_type.is_none());
        assert!(s.state_type.is_none());
        assert!(s.is_async);
        assert!(!s.is_streaming);
    }

    #[test]
    fn result_json_return() {
        let f: ItemFn = parse_quote! {
            async fn handler() -> Result<Json<Planet>, AppError> {}
        };
        let s = sig(f);
        assert_eq!(type_display(&s.output_type), "Planet");
        assert_eq!(type_display(s.error_type.as_ref().unwrap()), "AppError");
    }

    #[test]
    fn qualified_result_return() {
        let f: ItemFn = parse_quote! {
            async fn handler() -> std::result::Result<Json<Planet>, AppError> {}
        };
        let s = sig(f);
        assert_eq!(type_display(&s.output_type), "Planet");
    }

    #[test]
    fn qualified_json_return() {
        let f: ItemFn = parse_quote! {
            async fn handler() -> axum::extract::Json<Planet> {}
        };
        let s = sig(f);
        assert_eq!(type_display(&s.output_type), "Planet");
    }

    #[test]
    fn state_param_extracted() {
        let f: ItemFn = parse_quote! {
            async fn handler(State(db): State<Db>) -> Json<Planet> {}
        };
        let s = sig(f);
        assert_eq!(type_display(s.state_type.as_ref().unwrap()), "Db");
    }

    #[test]
    fn json_param_extracted() {
        let f: ItemFn = parse_quote! {
            async fn handler(Json(body): Json<CreatePlanet>) -> Json<Planet> {}
        };
        let s = sig(f);
        assert_eq!(type_display(s.input_type.as_ref().unwrap()), "CreatePlanet");
    }

    #[test]
    fn both_state_and_json_params() {
        let f: ItemFn = parse_quote! {
            async fn handler(State(db): State<Db>, Json(body): Json<CreatePlanet>) -> Result<Json<Planet>, AppError> {}
        };
        let s = sig(f);
        assert_eq!(type_display(s.state_type.as_ref().unwrap()), "Db");
        assert_eq!(type_display(s.input_type.as_ref().unwrap()), "CreatePlanet");
        assert_eq!(type_display(&s.output_type), "Planet");
        assert!(s.error_type.is_some());
    }

    #[test]
    fn path_param_extracted() {
        let f: ItemFn = parse_quote! {
            async fn handler(Path(id): Path<i32>) -> Json<Planet> {}
        };
        let s = sig(f);
        assert_eq!(s.path_params.len(), 1);
        assert_eq!(s.path_params[0].0, "id");
        assert_eq!(type_display(&s.path_params[0].1), "i32");
    }

    #[test]
    fn multiple_path_params_extracted() {
        let f: ItemFn = parse_quote! {
            async fn handler(Path(ws_id): Path<i32>, Path(proj_id): Path<String>) -> Json<Planet> {}
        };
        let s = sig(f);
        assert_eq!(s.path_params.len(), 2);
        assert_eq!(s.path_params[0].0, "ws_id");
        assert_eq!(type_display(&s.path_params[0].1), "i32");
        assert_eq!(s.path_params[1].0, "proj_id");
        assert_eq!(type_display(&s.path_params[1].1), "String");
    }

    #[test]
    fn path_and_query_params_both_extracted() {
        let f: ItemFn = parse_quote! {
            async fn handler(Path(id): Path<i32>, Query(q): Query<SearchQuery>) -> Json<Planet> {}
        };
        let s = sig(f);
        assert_eq!(s.path_params.len(), 1);
        assert_eq!(s.path_params[0].0, "id");
        assert_eq!(type_display(s.query_type.as_ref().unwrap()), "SearchQuery");
    }

    #[test]
    fn no_path_params_gives_empty_vec() {
        let f: ItemFn = parse_quote! {
            async fn handler(State(db): State<Db>) -> Json<Planet> {}
        };
        let s = sig(f);
        assert!(s.path_params.is_empty());
    }

    #[test]
    fn sync_function_allowed() {
        let f: ItemFn = parse_quote! {
            fn handler() -> Json<Planet> {}
        };
        let s = sig(f);
        assert!(!s.is_async);
    }

    #[test]
    fn sse_return_is_streaming() {
        let f: ItemFn = parse_quote! {
            async fn stream_events(State(_state): State<AppState>) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {}
        };
        let s = sig(f);
        assert!(s.is_streaming);
        assert!(s.error_type.is_none());
        // output_type is unit () for SSE handlers
        assert_eq!(type_display(&s.output_type), "()");
    }

    #[test]
    fn qualified_sse_return_is_streaming() {
        let f: ItemFn = parse_quote! {
            async fn handler() -> axum::response::Sse<SomeStream> {}
        };
        let s = sig(f);
        assert!(s.is_streaming);
    }

    // --- invalid signatures ---

    #[test]
    fn no_return_type_error() {
        let f: ItemFn = parse_quote! {
            async fn handler() {}
        };
        let err = sig_err(f);
        assert!(err.to_string().contains("has no return type"));
    }

    #[test]
    fn bare_type_return_error() {
        let f: ItemFn = parse_quote! {
            async fn handler() -> Vec<Planet> {}
        };
        let err = sig_err(f);
        assert!(err.to_string().contains("return type must be Json<T>"));
    }

    #[test]
    fn result_without_json_inner_error() {
        let f: ItemFn = parse_quote! {
            async fn handler() -> Result<Vec<Planet>, AppError> {}
        };
        let err = sig_err(f);
        assert!(
            err.to_string()
                .contains("Result's first type argument must be Json<T>")
        );
    }
}