@@ -59,34 +59,17 @@ pub fn generate_openapi_doc_with_metadata(
5959 if let syn:: Item :: Struct ( struct_item) = & parsed {
6060 // Find the file where this struct is defined
6161 // Try to find a route file that contains this struct
62+ // Find the route file that contains this struct definition
6263 let struct_file = metadata
6364 . routes
6465 . iter ( )
6566 . find_map ( |route| {
66- // Check if the file contains the struct definition
67- if let Ok ( file_content) = std:: fs:: read_to_string ( & route. file_path ) {
68- // Check if the struct name appears in the file (more specific check)
69- // Look for "struct StructName" pattern
70- let struct_pattern = format ! ( "struct {}" , struct_meta. name) ;
71- if file_content. contains ( & struct_pattern) {
72- return Some ( route. file_path . clone ( ) ) ;
73- }
74- }
75- None
67+ std:: fs:: read_to_string ( & route. file_path )
68+ . ok ( )
69+ . filter ( |content| content. contains ( & format ! ( "struct {}" , struct_meta. name) ) )
70+ . map ( |_| route. file_path . clone ( ) )
7671 } )
77- . or_else ( || {
78- // Fallback: try all route files to find the struct
79- for route in & metadata. routes {
80- if let Ok ( file_content) = std:: fs:: read_to_string ( & route. file_path ) {
81- let struct_pattern = format ! ( "struct {}" , struct_meta. name) ;
82- if file_content. contains ( & struct_pattern) {
83- return Some ( route. file_path . clone ( ) ) ;
84- }
85- }
86- }
87- // Last resort: use first route file if available
88- metadata. routes . first ( ) . map ( |r| r. file_path . clone ( ) )
89- } ) ;
72+ . or_else ( || metadata. routes . first ( ) . map ( |r| r. file_path . clone ( ) ) ) ;
9073
9174 if let Some ( file_path) = struct_file
9275 && let Ok ( file_content) = std:: fs:: read_to_string ( & file_path)
@@ -362,22 +345,15 @@ fn extract_value_from_expr(expr: &syn::Expr) -> Option<serde_json::Value> {
362345 // Literal values
363346 Expr :: Lit ( ExprLit { lit, .. } ) => match lit {
364347 Lit :: Str ( s) => Some ( serde_json:: Value :: String ( s. value ( ) ) ) ,
365- Lit :: Int ( i) => {
366- if let Ok ( val) = i. base10_parse :: < i64 > ( ) {
367- Some ( serde_json:: Value :: Number ( val. into ( ) ) )
368- } else {
369- None
370- }
371- }
372- Lit :: Float ( f) => {
373- if let Ok ( val) = f. base10_parse :: < f64 > ( ) {
374- Some ( serde_json:: Value :: Number (
375- serde_json:: Number :: from_f64 ( val) . unwrap_or ( serde_json:: Number :: from ( 0 ) ) ,
376- ) )
377- } else {
378- None
379- }
380- }
348+ Lit :: Int ( i) => i
349+ . base10_parse :: < i64 > ( )
350+ . ok ( )
351+ . map ( |v| serde_json:: Value :: Number ( v. into ( ) ) ) ,
352+ Lit :: Float ( f) => f
353+ . base10_parse :: < f64 > ( )
354+ . ok ( )
355+ . and_then ( serde_json:: Number :: from_f64)
356+ . map ( serde_json:: Value :: Number ) ,
381357 Lit :: Bool ( b) => Some ( serde_json:: Value :: Bool ( b. value ) ) ,
382358 _ => None ,
383359 } ,
@@ -415,23 +391,19 @@ fn extract_value_from_expr(expr: &syn::Expr) -> Option<serde_json::Value> {
415391fn get_type_default ( ty : & syn:: Type ) -> Option < serde_json:: Value > {
416392 use syn:: Type ;
417393 match ty {
418- Type :: Path ( type_path) => {
419- if let Some ( segment) = type_path. path . segments . last ( ) {
420- match segment. ident . to_string ( ) . as_str ( ) {
421- "String" => Some ( serde_json:: Value :: String ( String :: new ( ) ) ) ,
422- "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" => {
423- Some ( serde_json:: Value :: Number ( serde_json:: Number :: from ( 0 ) ) )
424- }
425- "f32" | "f64" => Some ( serde_json:: Value :: Number (
426- serde_json:: Number :: from_f64 ( 0.0 ) . unwrap_or ( serde_json:: Number :: from ( 0 ) ) ,
427- ) ) ,
428- "bool" => Some ( serde_json:: Value :: Bool ( false ) ) ,
429- _ => None ,
394+ Type :: Path ( type_path) => type_path. path . segments . last ( ) . and_then ( |segment| {
395+ match segment. ident . to_string ( ) . as_str ( ) {
396+ "String" => Some ( serde_json:: Value :: String ( String :: new ( ) ) ) ,
397+ "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" => {
398+ Some ( serde_json:: Value :: Number ( serde_json:: Number :: from ( 0 ) ) )
430399 }
431- } else {
432- None
400+ "f32" | "f64" => Some ( serde_json:: Value :: Number (
401+ serde_json:: Number :: from_f64 ( 0.0 ) . unwrap_or ( serde_json:: Number :: from ( 0 ) ) ,
402+ ) ) ,
403+ "bool" => Some ( serde_json:: Value :: Bool ( false ) ) ,
404+ _ => None ,
433405 }
434- }
406+ } ) ,
435407 _ => None ,
436408 }
437409}
0 commit comments