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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//! Parser module for analyzing function signatures and converting to OpenAPI structures
use std::collections::{BTreeMap, HashMap};
use syn::{Fields, FnArg, Pat, PatType, ReturnType, Type};
use vespera_core::{
route::{MediaType, Operation, Parameter, ParameterLocation, RequestBody, Response},
schema::{Reference, Schema, SchemaRef, SchemaType},
};
/// Extract path parameters from a path string
pub fn extract_path_parameters(path: &str) -> Vec<String> {
let mut params = Vec::new();
let segments: Vec<&str> = path.split('/').collect();
for segment in segments {
if segment.starts_with('{') && segment.ends_with('}') {
let param = segment.trim_start_matches('{').trim_end_matches('}');
params.push(param.to_string());
} else if segment.starts_with(':') {
let param = segment.trim_start_matches(':');
params.push(param.to_string());
}
}
params
}
/// Analyze function parameter and convert to OpenAPI Parameter
pub fn parse_function_parameter(
arg: &FnArg,
path_params: &[String],
known_schemas: &HashMap<String, String>,
) -> Option<Parameter> {
match arg {
FnArg::Receiver(_) => None,
FnArg::Typed(PatType { pat, ty, .. }) => {
// Extract parameter name from pattern
let param_name = match pat.as_ref() {
Pat::Ident(ident) => ident.ident.to_string(),
Pat::TupleStruct(tuple_struct) => {
// Handle Path(id) pattern
if tuple_struct.elems.len() == 1 {
match &tuple_struct.elems[0] {
Pat::Ident(ident) => ident.ident.to_string(),
_ => return None,
}
} else {
return None;
}
}
_ => return None,
};
// Check for common Axum extractors first (before checking path_params)
if let Type::Path(type_path) = ty.as_ref() {
let path = &type_path.path;
if !path.segments.is_empty() {
let segment = &path.segments[0];
let ident_str = segment.ident.to_string();
match ident_str.as_str() {
"Path" => {
// Path<T> extractor - use path parameter name from route if available
if let syn::PathArguments::AngleBracketed(args) = &segment.arguments
&& let Some(syn::GenericArgument::Type(inner_ty)) =
args.args.first()
{
// If there's exactly one path parameter, use its name
let name = if path_params.len() == 1 {
path_params[0].clone()
} else {
// Otherwise use the parameter name from the pattern
param_name
};
return Some(Parameter {
name,
r#in: ParameterLocation::Path,
description: None,
required: Some(true),
schema: Some(parse_type_to_schema_ref(inner_ty, known_schemas)),
example: None,
});
}
}
"Query" => {
// Query<T> extractor
if let syn::PathArguments::AngleBracketed(args) = &segment.arguments
&& let Some(syn::GenericArgument::Type(inner_ty)) =
args.args.first()
{
return Some(Parameter {
name: param_name.clone(),
r#in: ParameterLocation::Query,
description: None,
required: Some(true),
schema: Some(parse_type_to_schema_ref(inner_ty, known_schemas)),
example: None,
});
}
}
"Header" => {
// Header<T> extractor
if let syn::PathArguments::AngleBracketed(args) = &segment.arguments
&& let Some(syn::GenericArgument::Type(inner_ty)) =
args.args.first()
{
return Some(Parameter {
name: param_name.clone(),
r#in: ParameterLocation::Header,
description: None,
required: Some(true),
schema: Some(parse_type_to_schema_ref(inner_ty, known_schemas)),
example: None,
});
}
}
"Json" => {
// Json<T> extractor - this will be handled as RequestBody
return None;
}
_ => {}
}
}
}
// Check if it's a path parameter (by name match) - for non-extractor cases
if path_params.contains(¶m_name) {
return Some(Parameter {
name: param_name.clone(),
r#in: ParameterLocation::Path,
description: None,
required: Some(true),
schema: Some(parse_type_to_schema_ref(ty, known_schemas)),
example: None,
});
}
// Check if it's a primitive type (direct parameter)
if is_primitive_type(ty.as_ref()) {
return Some(Parameter {
name: param_name.clone(),
r#in: ParameterLocation::Query,
description: None,
required: Some(true),
schema: Some(parse_type_to_schema_ref(ty, known_schemas)),
example: None,
});
}
None
}
}
}
/// Check if a type is a primitive type
fn is_primitive_type(ty: &Type) -> bool {
match ty {
Type::Path(type_path) => {
let path = &type_path.path;
if path.segments.len() == 1 {
let ident = path.segments[0].ident.to_string();
matches!(
ident.as_str(),
"i8" | "i16"
| "i32"
| "i64"
| "u8"
| "u16"
| "u32"
| "u64"
| "f32"
| "f64"
| "bool"
| "String"
| "str"
)
} else {
false
}
}
_ => false,
}
}
/// Extract rename_all attribute from struct attributes
fn extract_rename_all(attrs: &[syn::Attribute]) -> Option<String> {
for attr in attrs {
if attr.path().is_ident("serde") {
// Parse the attribute tokens manually
// Format: #[serde(rename_all = "camelCase")]
let tokens = attr.meta.require_list().ok()?;
let token_str = tokens.tokens.to_string();
// Look for rename_all = "..." pattern
if let Some(start) = token_str.find("rename_all") {
let remaining = &token_str[start + "rename_all".len()..];
if let Some(equals_pos) = remaining.find('=') {
let value_part = &remaining[equals_pos + 1..].trim();
// Extract string value (remove quotes)
if value_part.starts_with('"') && value_part.ends_with('"') {
let value = &value_part[1..value_part.len() - 1];
return Some(value.to_string());
}
}
}
}
}
None
}
/// Convert field name according to rename_all rule
fn rename_field(field_name: &str, rename_all: Option<&str>) -> String {
match rename_all {
Some("camelCase") => {
// Convert snake_case to camelCase
let mut result = String::new();
let mut capitalize_next = false;
for ch in field_name.chars() {
if ch == '_' {
capitalize_next = true;
} else if capitalize_next {
result.push(ch.to_uppercase().next().unwrap_or(ch));
capitalize_next = false;
} else {
result.push(ch);
}
}
result
}
Some("snake_case") => {
// Convert camelCase to snake_case
let mut result = String::new();
for (i, ch) in field_name.chars().enumerate() {
if ch.is_uppercase() && i > 0 {
result.push('_');
}
result.push(ch.to_lowercase().next().unwrap_or(ch));
}
result
}
Some("kebab-case") => {
// Convert snake_case to kebab-case
field_name.replace('_', "-")
}
Some("PascalCase") => {
// Convert snake_case to PascalCase
let mut result = String::new();
let mut capitalize_next = true;
for ch in field_name.chars() {
if ch == '_' {
capitalize_next = true;
} else if capitalize_next {
result.push(ch.to_uppercase().next().unwrap_or(ch));
capitalize_next = false;
} else {
result.push(ch);
}
}
result
}
_ => field_name.to_string(),
}
}
/// Parse struct definition to OpenAPI Schema
pub fn parse_struct_to_schema(
struct_item: &syn::ItemStruct,
known_schemas: &HashMap<String, String>,
) -> Schema {
let mut properties = BTreeMap::new();
let mut required = Vec::new();
// Extract rename_all attribute from struct
let rename_all = extract_rename_all(&struct_item.attrs);
match &struct_item.fields {
Fields::Named(fields_named) => {
for field in &fields_named.named {
let rust_field_name = field
.ident
.as_ref()
.map(|i| i.to_string())
.unwrap_or_else(|| "unknown".to_string());
// Apply rename_all transformation if present
let field_name = rename_field(&rust_field_name, rename_all.as_deref());
let field_type = &field.ty;
let schema_ref = parse_type_to_schema_ref(field_type, known_schemas);
properties.insert(field_name.clone(), schema_ref);
// Check if field is Option<T>
let is_optional = matches!(
field_type,
Type::Path(type_path)
if type_path
.path
.segments
.first()
.map(|s| s.ident == "Option")
.unwrap_or(false)
);
if !is_optional {
required.push(field_name);
}
}
}
Fields::Unnamed(_) => {
// Tuple structs are not supported for now
}
Fields::Unit => {
// Unit structs have no fields
}
}
Schema {
schema_type: Some(SchemaType::Object),
properties: if properties.is_empty() {
None
} else {
Some(properties)
},
required: if required.is_empty() {
None
} else {
Some(required)
},
..Schema::object()
}
}
/// Parse Rust type to OpenAPI SchemaRef
pub fn parse_type_to_schema_ref(ty: &Type, known_schemas: &HashMap<String, String>) -> SchemaRef {
match ty {
Type::Path(type_path) => {
let path = &type_path.path;
if path.segments.is_empty() {
return SchemaRef::Inline(Box::new(Schema::new(SchemaType::Object)));
}
let segment = &path.segments[0];
let ident_str = segment.ident.to_string();
// Handle generic types
if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
match ident_str.as_str() {
"Vec" | "Option" => {
if let Some(syn::GenericArgument::Type(inner_ty)) = args.args.first() {
let inner_schema = parse_type_to_schema_ref(inner_ty, known_schemas);
if ident_str == "Vec" {
return SchemaRef::Inline(Box::new(Schema::array(inner_schema)));
} else {
// Option<T> -> nullable schema
if let SchemaRef::Inline(mut schema) = inner_schema {
schema.nullable = Some(true);
return SchemaRef::Inline(schema);
}
}
}
}
_ => {}
}
}
// Handle primitive types
match ident_str.as_str() {
"i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" => {
SchemaRef::Inline(Box::new(Schema::integer()))
}
"f32" | "f64" => SchemaRef::Inline(Box::new(Schema::number())),
"bool" => SchemaRef::Inline(Box::new(Schema::boolean())),
"String" | "str" => SchemaRef::Inline(Box::new(Schema::string())),
// Standard library types that should not be referenced
"HashMap" | "BTreeMap" | "Vec" | "Option" | "Result" | "Json" | "Path"
| "Query" | "Header" => {
// These are not schema types, return object schema
SchemaRef::Inline(Box::new(Schema::new(SchemaType::Object)))
}
_ => {
// Check if this is a known schema (struct with Schema derive)
if known_schemas.contains_key(&ident_str) {
SchemaRef::Ref(Reference::schema(&ident_str))
} else {
// For unknown custom types, return object schema instead of reference
// This prevents creating invalid references to non-existent schemas
SchemaRef::Inline(Box::new(Schema::new(SchemaType::Object)))
}
}
}
}
Type::Reference(type_ref) => {
// Handle &T, &mut T, etc.
parse_type_to_schema_ref(&type_ref.elem, known_schemas)
}
_ => SchemaRef::Inline(Box::new(Schema::new(SchemaType::Object))),
}
}
/// Analyze function signature and extract RequestBody
pub fn parse_request_body(
arg: &FnArg,
known_schemas: &HashMap<String, String>,
) -> Option<RequestBody> {
match arg {
FnArg::Receiver(_) => None,
FnArg::Typed(PatType { ty, .. }) => {
if let Type::Path(type_path) = ty.as_ref() {
let path = &type_path.path;
if path.segments.is_empty() {
return None;
}
let segment = &path.segments[0];
let ident_str = segment.ident.to_string();
if ident_str == "Json"
&& let syn::PathArguments::AngleBracketed(args) = &segment.arguments
&& let Some(syn::GenericArgument::Type(inner_ty)) = args.args.first()
{
let schema = parse_type_to_schema_ref(inner_ty, known_schemas);
let mut content = BTreeMap::new();
content.insert(
"application/json".to_string(),
MediaType {
schema: Some(schema),
example: None,
examples: None,
},
);
return Some(RequestBody {
description: None,
required: Some(true),
content,
});
}
}
None
}
}
}
/// Unwrap Json<T> to get T
fn unwrap_json(ty: &Type) -> &Type {
if let Type::Path(type_path) = ty {
let path = &type_path.path;
if !path.segments.is_empty() {
let segment = &path.segments[0];
if segment.ident == "Json" {
if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
if let Some(syn::GenericArgument::Type(inner_ty)) = args.args.first() {
return inner_ty;
}
}
}
}
}
ty
}
/// Extract Ok and Err types from Result<T, E> or Result<Json<T>, E>
/// Handles both Result and std::result::Result, and unwraps references
fn extract_result_types(ty: &Type) -> Option<(Type, Type)> {
// First unwrap Json if present
let unwrapped = unwrap_json(ty);
// Handle both Type::Path and Type::Reference (for &Result<...>)
let result_type = match unwrapped {
Type::Path(type_path) => type_path,
Type::Reference(type_ref) => {
// Unwrap reference and check if it's a Result
if let Type::Path(type_path) = type_ref.elem.as_ref() {
type_path
} else {
return None;
}
}
_ => return None,
};
let path = &result_type.path;
if path.segments.is_empty() {
return None;
}
// Check if any segment is "Result" (handles both Result and std::result::Result)
let is_result = path.segments.iter().any(|seg| seg.ident == "Result");
if is_result {
// Get the last segment (Result) to check for generics
if let Some(segment) = path.segments.last() {
if let syn::PathArguments::AngleBracketed(args) = &segment.arguments {
if args.args.len() >= 2 {
if let (
Some(syn::GenericArgument::Type(ok_ty)),
Some(syn::GenericArgument::Type(err_ty)),
) = (args.args.first(), args.args.get(1))
{
// Unwrap Json from Ok type if present
let ok_ty_unwrapped = unwrap_json(ok_ty);
return Some((ok_ty_unwrapped.clone(), err_ty.clone()));
}
}
}
}
}
None
}
/// Check if error type is a tuple (StatusCode, E) or (StatusCode, Json<E>)
/// Returns the error type E and a default status code (400)
fn extract_status_code_tuple(err_ty: &Type) -> Option<(u16, Type)> {
if let Type::Tuple(tuple) = err_ty {
if tuple.elems.len() == 2 {
// Check if first element is StatusCode
if let Type::Path(type_path) = &tuple.elems[0] {
let path = &type_path.path;
if !path.segments.is_empty() {
let segment = &path.segments[0];
// Check if it's StatusCode (could be qualified like axum::http::StatusCode)
let is_status_code = segment.ident == "StatusCode"
|| (path.segments.len() > 1
&& path.segments.iter().any(|s| s.ident == "StatusCode"));
if is_status_code {
// Use 400 as default status code
// The actual status code value is determined at runtime
if let Some(error_type) = tuple.elems.get(1) {
// Unwrap Json if present
let error_type_unwrapped = unwrap_json(error_type);
return Some((400, error_type_unwrapped.clone()));
}
}
}
}
}
}
None
}
/// Analyze return type and convert to Responses map
pub fn parse_return_type(
return_type: &ReturnType,
known_schemas: &HashMap<String, String>,
) -> BTreeMap<String, Response> {
let mut responses = BTreeMap::new();
match return_type {
ReturnType::Default => {
// No return type - just 200 with no content
responses.insert(
"200".to_string(),
Response {
description: "Successful response".to_string(),
headers: None,
content: None,
},
);
}
ReturnType::Type(_, ty) => {
// Check if it's a Result<T, E>
if let Some((ok_ty, err_ty)) = extract_result_types(ty) {
// Handle success response (200)
let ok_schema = parse_type_to_schema_ref(&ok_ty, known_schemas);
let mut ok_content = BTreeMap::new();
ok_content.insert(
"application/json".to_string(),
MediaType {
schema: Some(ok_schema),
example: None,
examples: None,
},
);
responses.insert(
"200".to_string(),
Response {
description: "Successful response".to_string(),
headers: None,
content: Some(ok_content),
},
);
// Handle error response
// Check if error is (StatusCode, E) tuple
if let Some((status_code, error_type)) = extract_status_code_tuple(&err_ty) {
// Use the status code from the tuple
let err_schema = parse_type_to_schema_ref(&error_type, known_schemas);
let mut err_content = BTreeMap::new();
err_content.insert(
"application/json".to_string(),
MediaType {
schema: Some(err_schema),
example: None,
examples: None,
},
);
responses.insert(
status_code.to_string(),
Response {
description: "Error response".to_string(),
headers: None,
content: Some(err_content),
},
);
} else {
// Regular error type - use default 400
// Unwrap Json if present
let err_ty_unwrapped = unwrap_json(&err_ty);
let err_schema = parse_type_to_schema_ref(err_ty_unwrapped, known_schemas);
let mut err_content = BTreeMap::new();
err_content.insert(
"application/json".to_string(),
MediaType {
schema: Some(err_schema),
example: None,
examples: None,
},
);
responses.insert(
"400".to_string(),
Response {
description: "Error response".to_string(),
headers: None,
content: Some(err_content),
},
);
}
} else {
// Not a Result type - regular response
let schema = parse_type_to_schema_ref(ty, known_schemas);
let mut content = BTreeMap::new();
content.insert(
"application/json".to_string(),
MediaType {
schema: Some(schema),
example: None,
examples: None,
},
);
responses.insert(
"200".to_string(),
Response {
description: "Successful response".to_string(),
headers: None,
content: Some(content),
},
);
}
}
}
responses
}
/// Build Operation from function signature
pub fn build_operation_from_function(
sig: &syn::Signature,
path: &str,
known_schemas: &HashMap<String, String>,
error_status: Option<&[u16]>,
) -> Operation {
let path_params = extract_path_parameters(path);
let mut parameters = Vec::new();
let mut request_body = None;
// Parse function parameters
for input in &sig.inputs {
// Check if it's a request body (Json<T>)
if let Some(body) = parse_request_body(input, known_schemas) {
request_body = Some(body);
} else if let Some(param) = parse_function_parameter(input, &path_params, known_schemas) {
parameters.push(param);
}
}
// Parse return type - may return multiple responses (for Result types)
let mut responses = parse_return_type(&sig.output, known_schemas);
// Add additional error status codes from error_status attribute
if let Some(status_codes) = error_status {
// Find the error response schema (usually 400 or the first error response)
let error_schema = responses
.iter()
.find(|(code, _)| code != &&"200".to_string())
.and_then(|(_, resp)| {
resp.content
.as_ref()?
.get("application/json")?
.schema
.clone()
});
if let Some(schema) = error_schema {
for &status_code in status_codes {
let status_str = status_code.to_string();
// Only add if not already present
if !responses.contains_key(&status_str) {
let mut err_content = BTreeMap::new();
err_content.insert(
"application/json".to_string(),
MediaType {
schema: Some(schema.clone()),
example: None,
examples: None,
},
);
responses.insert(
status_str,
Response {
description: "Error response".to_string(),
headers: None,
content: Some(err_content),
},
);
}
}
}
}
Operation {
operation_id: Some(sig.ident.to_string()),
tags: None,
summary: None,
description: None,
parameters: if parameters.is_empty() {
None
} else {
Some(parameters)
},
request_body,
responses,
security: None,
}
}