vespera_macro 0.4.0

Procedural macros for Vespera - automatic route discovery and OpenAPI generation
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
use std::collections::HashMap;

use rstest::rstest;
use vespera_core::schema::{SchemaRef, SchemaType};

use super::*;

#[derive(Debug)]
struct ExpectedSchema {
    schema_type: SchemaType,
    nullable: bool,
    items_schema_type: Option<SchemaType>,
}

#[derive(Debug)]
struct ExpectedResponse {
    status: &'static str,
    schema: ExpectedSchema,
}

fn parse_return_type_str(return_type_str: &str) -> syn::ReturnType {
    if return_type_str.is_empty() {
        syn::ReturnType::Default
    } else {
        let full_signature = format!("fn test() {return_type_str}");
        syn::parse_str::<syn::Signature>(&full_signature)
            .expect("Failed to parse return type")
            .output
    }
}

fn assert_schema_matches(schema_ref: &SchemaRef, expected: &ExpectedSchema) {
    match schema_ref {
        SchemaRef::Inline(schema) => {
            assert_eq!(schema.schema_type, Some(expected.schema_type));
            assert_eq!(schema.nullable.unwrap_or(false), expected.nullable);
            if let Some(item_ty) = &expected.items_schema_type {
                let items = schema
                    .items
                    .as_ref()
                    .expect("items should be present for array");
                match items {
                    SchemaRef::Inline(item_schema) => {
                        assert_eq!(item_schema.schema_type, Some(*item_ty));
                    }
                    SchemaRef::Ref(_) => panic!("expected inline schema for array items"),
                }
            }
        }
        SchemaRef::Ref(_) => panic!("expected inline schema"),
    }
}

#[rstest]
#[case("", None, None, None)]
#[case(
        "-> String",
        Some(ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None }),
        None,
        None
    )]
#[case(
        "-> &str",
        Some(ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None }),
        None,
        None
    )]
#[case(
        "-> i32",
        Some(ExpectedSchema { schema_type: SchemaType::Integer, nullable: false, items_schema_type: None }),
        None,
        None
    )]
#[case(
        "-> bool",
        Some(ExpectedSchema { schema_type: SchemaType::Boolean, nullable: false, items_schema_type: None }),
        None,
        None
    )]
#[case(
        "-> Vec<String>",
        Some(ExpectedSchema { schema_type: SchemaType::Array, nullable: false, items_schema_type: Some(SchemaType::String) }),
        None,
        None
    )]
#[case(
        "-> Option<String>",
        Some(ExpectedSchema { schema_type: SchemaType::String, nullable: true, items_schema_type: None }),
        None,
        None
    )]
#[case(
        "-> Result<String, String>",
        Some(ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None }),
        Some(ExpectedResponse { status: "400", schema: ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None } }),
        None
    )]
#[case(
        "-> Result<i32, String>",
        Some(ExpectedSchema { schema_type: SchemaType::Integer, nullable: false, items_schema_type: None }),
        Some(ExpectedResponse { status: "400", schema: ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None } }),
        None
    )]
#[case(
        "-> Result<Json<User>, String>",
        Some(ExpectedSchema { schema_type: SchemaType::Object, nullable: false, items_schema_type: None }),
        Some(ExpectedResponse { status: "400", schema: ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None } }),
        None
    )]
#[case(
        "-> Result<&str, String>",
        Some(ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None }),
        Some(ExpectedResponse { status: "400", schema: ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None } }),
        None
    )]
#[case(
        "-> Result<String, (StatusCode, String)>",
        Some(ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None }),
        Some(ExpectedResponse { status: "400", schema: ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None } }),
        None
    )]
#[case(
        "-> Result<String, (StatusCode, Json<String>)>",
        Some(ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None }),
        Some(ExpectedResponse { status: "400", schema: ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None } }),
        None
    )]
#[case(
        "-> Result<(HeaderMap<String, String>, Json<i32>), String>",
        Some(ExpectedSchema { schema_type: SchemaType::Integer, nullable: false, items_schema_type: None }),
        Some(ExpectedResponse { status: "400", schema: ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None } }),
        Some(true)
    )]
#[case(
        "-> Result<String, (axum::http::StatusCode, Json<i32>)>",
        Some(ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None }),
        Some(ExpectedResponse { status: "400", schema: ExpectedSchema { schema_type: SchemaType::Integer, nullable: false, items_schema_type: None } }),
        None
    )]
// StatusCode as the sole Ok response type → no content (empty body)
#[case(
        "-> Result<StatusCode, (StatusCode, String)>",
        None,
        Some(ExpectedResponse { status: "400", schema: ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None } }),
        None
    )]
// CookieJar in Ok tuple → body is Json<String>, CookieJar filtered out
#[case(
        "-> Result<(CookieJar, Json<String>), (StatusCode, String)>",
        Some(ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None }),
        Some(ExpectedResponse { status: "400", schema: ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None } }),
        None
    )]
// CookieJar + StatusCode in Ok tuple → body is last non-metadata element
#[case(
        "-> Result<(StatusCode, CookieJar, Json<i32>), String>",
        Some(ExpectedSchema { schema_type: SchemaType::Integer, nullable: false, items_schema_type: None }),
        Some(ExpectedResponse { status: "400", schema: ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None } }),
        None
    )]
// Non-Result: StatusCode alone → no content (covers line 155)
#[case("-> StatusCode", None, None, None)]
// Non-Result: Json<T> wrapper → unwraps to T
#[case(
        "-> Json<String>",
        Some(ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None }),
        None,
        None
    )]
// Non-Result: Json<i32> wrapper → unwraps to integer
#[case(
        "-> Json<i32>",
        Some(ExpectedSchema { schema_type: SchemaType::Integer, nullable: false, items_schema_type: None }),
        None,
        None
    )]
// Non-Result: f64 → number type
#[case(
        "-> f64",
        Some(ExpectedSchema { schema_type: SchemaType::Number, nullable: false, items_schema_type: None }),
        None,
        None
    )]
// Non-Result: qualified axum::Json<String> → unwraps to String
#[case(
        "-> axum::Json<String>",
        Some(ExpectedSchema { schema_type: SchemaType::String, nullable: false, items_schema_type: None }),
        None,
        None
    )]
fn test_parse_return_type(
    #[case] return_type_str: &str,
    #[case] ok_expectation: Option<ExpectedSchema>,
    #[case] err_expectation: Option<ExpectedResponse>,
    #[case] ok_headers_expected: Option<bool>,
) {
    let known_schemas = HashSet::new();
    let struct_definitions = HashMap::new();
    let return_type = parse_return_type_str(return_type_str);

    let responses = parse_return_type(&return_type, &known_schemas, &struct_definitions);

    // Validate success response
    let ok_response = responses.get("200").expect("200 response should exist");
    assert_eq!(ok_response.description, "Successful response");
    match &ok_expectation {
        None => {
            assert!(ok_response.content.is_none());
        }
        Some(expected_schema) => {
            let content = ok_response
                .content
                .as_ref()
                .expect("ok content should exist");
            let media_type = content.values().next().expect("ok media type should exist");
            let schema_ref = media_type.schema.as_ref().expect("ok schema should exist");
            assert_schema_matches(schema_ref, expected_schema);
        }
    }
    if let Some(expect_headers) = ok_headers_expected {
        assert_eq!(ok_response.headers.is_some(), expect_headers);
    }

    // Validate error response (if any)
    match &err_expectation {
        None => assert_eq!(responses.len(), 1),
        Some(err) => {
            assert_eq!(responses.len(), 2);
            let err_response = responses
                .get(err.status)
                .expect("error response should exist");
            assert_eq!(err_response.description, "Error response");
            let content = err_response
                .content
                .as_ref()
                .expect("error content should exist");
            let media_type = content
                .values()
                .next()
                .expect("error media type should exist");
            let schema_ref = media_type
                .schema
                .as_ref()
                .expect("error schema should exist");
            assert_schema_matches(schema_ref, &err.schema);
        }
    }
}

#[rstest]
#[case("-> String", "200", "text/plain")]
#[case("-> &str", "200", "text/plain")]
#[case("-> Json<String>", "200", "application/json")]
#[case("-> i32", "200", "application/json")]
#[case("-> Result<String, String>", "200", "text/plain")]
#[case("-> Result<String, String>", "400", "text/plain")]
#[case(
    "-> Result<Json<User>, (StatusCode, String)>",
    "200",
    "application/json"
)]
#[case("-> Result<Json<User>, (StatusCode, String)>", "400", "text/plain")]
#[case(
    "-> Result<String, (StatusCode, Json<String>)>",
    "400",
    "application/json"
)]
fn response_content_type_matches_body_kind(
    #[case] return_type_str: &str,
    #[case] status: &str,
    #[case] expected_content_type: &str,
) {
    let return_type = parse_return_type_str(return_type_str);
    let responses = parse_return_type(&return_type, &HashSet::new(), &HashMap::new());
    let content = responses
        .get(status)
        .and_then(|response| response.content.as_ref())
        .unwrap_or_else(|| panic!("{status} content missing for `{return_type_str}`"));
    assert!(
        content.contains_key(expected_content_type),
        "`{return_type_str}` {status}: expected {expected_content_type}, got {:?}",
        content.keys().collect::<Vec<_>>()
    );
}

// ======== Tests for uncovered lines ========

#[test]
fn test_extract_result_types_non_path_non_ref() {
    // Test line 43: type that's neither Path nor Reference returns None
    // Tuple type is neither Path nor Reference
    let ty: syn::Type = syn::parse_str("(i32, String)").unwrap();
    let result = extract_result_types(&ty);
    assert!(result.is_none());

    // Array type
    let ty: syn::Type = syn::parse_str("[i32; 3]").unwrap();
    let result = extract_result_types(&ty);
    assert!(result.is_none());

    // Slice type
    let ty: syn::Type = syn::parse_str("[i32]").unwrap();
    let result = extract_result_types(&ty);
    assert!(result.is_none());
}

#[test]
fn test_extract_result_types_ref_to_non_path() {
    // Test line 43: &(Tuple) - Reference to non-Path type
    // Tests: else branch
    let ty: syn::Type = syn::parse_str("&(i32, String)").unwrap();
    let result = extract_result_types(&ty);
    // The Reference's elem is a Tuple, not a Path, so line 39 condition fails
    // Falls through to line 43
    assert!(result.is_none());
}

#[test]
fn test_extract_result_types_empty_path_segments() {
    // Test line 48: path.segments.is_empty() returns None
    // Create a Type::Path programmatically with empty segments
    use syn::punctuated::Punctuated;

    let type_path = syn::TypePath {
        attrs: Vec::new(),
        qself: None,
        path: syn::Path {
            leading_colon: None,
            segments: Punctuated::new(), // Empty segments!
        },
    };
    let ty = syn::Type::Path(type_path);

    // Tests: path.segments.is_empty() is true
    let result = extract_result_types(&ty);
    assert!(
        result.is_none(),
        "Empty path segments should return None (line 48)"
    );
}

#[test]
fn test_extract_result_types_empty_path_via_reference() {
    // Test line 48 via reference path: &Type::Path with empty segments
    use syn::punctuated::Punctuated;

    // Create inner Type::Path with empty segments
    let inner_type_path = syn::TypePath {
        attrs: Vec::new(),
        qself: None,
        path: syn::Path {
            leading_colon: None,
            segments: Punctuated::new(),
        },
    };
    let inner_ty = syn::Type::Path(inner_type_path);

    // Wrap in a reference
    let ty = syn::Type::Reference(syn::TypeReference {
        attrs: Vec::new(),
        and_token: syn::token::And::default(),
        lifetime: None,
        mutability: None,
        elem: Box::new(inner_ty),
    });

    // Tests: reference to path then empty segments
    let result = extract_result_types(&ty);
    assert!(
        result.is_none(),
        "Empty path segments via reference should return None (line 48)"
    );
}

#[test]
fn test_extract_result_types_with_reference() {
    // Test the Reference path (line 38-41) that succeeds
    // &Result<T, E> should still extract types
    let ty: syn::Type = syn::parse_str("&Result<String, i32>").unwrap();
    let _result = extract_result_types(&ty);
    // Note: This doesn't actually work because is_keyword_type_by_type_path
    // checks for Result type, but ref to Result is different
    // The important thing is the code doesn't panic
    // Tests: exercises reference path even if result is None
}

#[test]
fn test_unwrap_json_non_json() {
    // Test unwrap_json with non-Json type returns original
    let ty: syn::Type = syn::parse_str("String").unwrap();
    let unwrapped = unwrap_json(&ty);
    // Should return the same type
    assert!(matches!(unwrapped, syn::Type::Path(_)));
}

#[test]
fn test_unwrap_json_with_json() {
    // Test unwrap_json with Json<T>
    let ty: syn::Type = syn::parse_str("Json<String>").unwrap();
    let unwrapped = unwrap_json(&ty);
    // Should unwrap to String
    if let syn::Type::Path(type_path) = unwrapped {
        assert_eq!(
            type_path.path.segments.last().unwrap().ident.to_string(),
            "String"
        );
    } else {
        panic!("Expected Path type");
    }
}

#[test]
fn test_parse_return_type_tuple() {
    // Test parse_return_type with tuple type (exercises line 43 via extract_result_types)
    let known_schemas = HashSet::new();
    let struct_definitions = HashMap::new();
    let return_type = parse_return_type_str("-> (i32, String)");

    let responses = parse_return_type(&return_type, &known_schemas, &struct_definitions);

    // Tuple is not a Result, so it should be treated as regular response
    assert!(responses.contains_key("200"));
    assert_eq!(responses.len(), 1);
}

#[test]
fn test_extract_ok_payload_and_headers_tuple_without_headermap() {
    // Test line 95: tuple without HeaderMap returns None for headers
    let ty: syn::Type = syn::parse_str("(StatusCode, String)").unwrap();
    let (payload, headers) = extract_ok_payload_and_headers(&ty);

    // Payload should be String (last element unwrapped)
    if let syn::Type::Path(type_path) = &payload {
        assert_eq!(
            type_path.path.segments.last().unwrap().ident.to_string(),
            "String"
        );
    }
    // Headers should be None (no HeaderMap in tuple) - this is line 95
    assert!(headers.is_none());
}

#[test]
fn test_parse_return_type_result_with_ok_tuple_no_headermap() {
    // Test line 95 via full parse_return_type: Result<(StatusCode, Json<T>), E>
    let known_schemas = HashSet::new();
    let struct_definitions = HashMap::new();
    let return_type = parse_return_type_str("-> Result<(StatusCode, Json<String>), String>");

    let responses = parse_return_type(&return_type, &known_schemas, &struct_definitions);

    // Should have 200 and 400 responses
    assert!(responses.contains_key("200"));
    let ok_response = responses.get("200").unwrap();
    // Headers should be None
    assert!(ok_response.headers.is_none());
}

// ======== CookieJar tuple extraction tests ========

#[test]
fn test_extract_ok_payload_and_headers_cookie_jar_tuple() {
    // (CookieJar, Json<String>) → payload should be String, CookieJar filtered
    let ty: syn::Type = syn::parse_str("(CookieJar, Json<String>)").unwrap();
    let (payload, headers) = extract_ok_payload_and_headers(&ty);

    if let syn::Type::Path(type_path) = &payload {
        assert_eq!(
            type_path.path.segments.last().unwrap().ident.to_string(),
            "String"
        );
    } else {
        panic!("Expected Path type for payload");
    }
    assert!(headers.is_none());
}

#[test]
fn test_extract_ok_payload_and_headers_cookie_jar_with_status_code() {
    // (StatusCode, CookieJar, Json<i32>) → payload should be i32
    let ty: syn::Type = syn::parse_str("(StatusCode, CookieJar, Json<i32>)").unwrap();
    let (payload, headers) = extract_ok_payload_and_headers(&ty);

    if let syn::Type::Path(type_path) = &payload {
        assert_eq!(
            type_path.path.segments.last().unwrap().ident.to_string(),
            "i32"
        );
    } else {
        panic!("Expected Path type for payload");
    }
    assert!(headers.is_none());
}

#[test]
fn test_extract_ok_payload_and_headers_all_non_body_types() {
    // (StatusCode, CookieJar) → no body element found, returns original tuple
    let ty: syn::Type = syn::parse_str("(StatusCode, CookieJar)").unwrap();
    let (payload, headers) = extract_ok_payload_and_headers(&ty);
    // No body element found → falls through to return original type
    assert!(matches!(payload, syn::Type::Tuple(_)));
    assert!(headers.is_none());
}

#[test]
fn test_unwrap_json_qualified_path() {
    // vespera::axum::Json<String> → should unwrap to String via last-segment matching
    let ty: syn::Type = syn::parse_str("vespera::axum::Json<String>").unwrap();
    let unwrapped = unwrap_json(&ty);
    if let syn::Type::Path(type_path) = unwrapped {
        assert_eq!(
            type_path.path.segments.last().unwrap().ident.to_string(),
            "String"
        );
    } else {
        panic!("Expected Path type");
    }
}

#[test]
fn test_unwrap_json_non_generic_path() {
    // Type with segments but no angle brackets → returns original
    let ty: syn::Type = syn::parse_str("std::string::String").unwrap();
    let unwrapped = unwrap_json(&ty);
    if let syn::Type::Path(type_path) = unwrapped {
        assert_eq!(
            type_path.path.segments.last().unwrap().ident.to_string(),
            "String"
        );
    } else {
        panic!("Expected Path type");
    }
}

#[test]
fn test_parse_return_type_non_result_status_code() {
    // Direct StatusCode return (not in Result) → 200 with no content
    let known_schemas = HashSet::new();
    let struct_definitions = HashMap::new();
    let return_type = parse_return_type_str("-> StatusCode");

    let responses = parse_return_type(&return_type, &known_schemas, &struct_definitions);

    assert_eq!(responses.len(), 1);
    let ok_response = responses.get("200").unwrap();
    assert!(
        ok_response.content.is_none(),
        "StatusCode return should have no content"
    );
    assert!(ok_response.headers.is_none());
}

#[test]
fn test_is_non_body_type() {
    let status: syn::Type = syn::parse_str("StatusCode").unwrap();
    assert!(is_non_body_type(&status));

    let header_map: syn::Type = syn::parse_str("HeaderMap").unwrap();
    assert!(is_non_body_type(&header_map));

    let cookie_jar: syn::Type = syn::parse_str("CookieJar").unwrap();
    assert!(is_non_body_type(&cookie_jar));

    let string: syn::Type = syn::parse_str("String").unwrap();
    assert!(!is_non_body_type(&string));

    let json: syn::Type = syn::parse_str("Json<String>").unwrap();
    assert!(!is_non_body_type(&json));
}

#[test]
fn response_helpers_cover_non_string_tuple_and_reference_fallbacks() {
    let tuple: Type = syn::parse_quote!((String,));
    assert!(!is_string_like(&tuple));

    let metadata_only: Type = syn::parse_quote!((StatusCode, HeaderMap));
    assert!(std::ptr::eq(
        tuple_body(&metadata_only),
        &raw const metadata_only
    ));

    let reference_to_tuple: Type = syn::parse_quote!(&(String, i32));
    assert!(result_args(&reference_to_tuple).is_none());

    let content = content_for_type(
        &syn::parse_quote!(String),
        "text/plain",
        &HashSet::new(),
        &HashMap::new(),
    )
    .expect("string content");
    assert!(content.contains_key("text/plain"));
}