elif_openapi/
macros.rs

1/*!
2OpenAPI schema generation traits and utilities.
3
4This module provides traits and utilities for OpenAPI schema generation from Rust types.
5The derive macro implementation would be in a separate proc-macro crate.
6*/
7
8/// Trait for types that can generate OpenAPI schemas
9pub trait OpenApiSchema {
10    /// Generate OpenAPI schema for this type
11    fn openapi_schema() -> crate::specification::Schema;
12
13    /// Get the schema name/title
14    fn schema_name() -> String;
15}
16
17// Implementation would go here for procedural macros
18// This is simplified for the demo version
19
20// Manual implementations for common types
21impl OpenApiSchema for String {
22    fn openapi_schema() -> crate::specification::Schema {
23        crate::specification::Schema {
24            schema_type: Some("string".to_string()),
25            ..Default::default()
26        }
27    }
28
29    fn schema_name() -> String {
30        "String".to_string()
31    }
32}
33
34impl OpenApiSchema for i32 {
35    fn openapi_schema() -> crate::specification::Schema {
36        crate::specification::Schema {
37            schema_type: Some("integer".to_string()),
38            format: Some("int32".to_string()),
39            ..Default::default()
40        }
41    }
42
43    fn schema_name() -> String {
44        "i32".to_string()
45    }
46}
47
48impl OpenApiSchema for i64 {
49    fn openapi_schema() -> crate::specification::Schema {
50        crate::specification::Schema {
51            schema_type: Some("integer".to_string()),
52            format: Some("int64".to_string()),
53            ..Default::default()
54        }
55    }
56
57    fn schema_name() -> String {
58        "i64".to_string()
59    }
60}
61
62impl OpenApiSchema for bool {
63    fn openapi_schema() -> crate::specification::Schema {
64        crate::specification::Schema {
65            schema_type: Some("boolean".to_string()),
66            ..Default::default()
67        }
68    }
69
70    fn schema_name() -> String {
71        "bool".to_string()
72    }
73}
74
75impl<T: OpenApiSchema> OpenApiSchema for Option<T> {
76    fn openapi_schema() -> crate::specification::Schema {
77        let mut schema = T::openapi_schema();
78        schema.nullable = Some(true);
79        schema
80    }
81
82    fn schema_name() -> String {
83        format!("Option<{}>", T::schema_name())
84    }
85}
86
87impl<T: OpenApiSchema> OpenApiSchema for Vec<T> {
88    fn openapi_schema() -> crate::specification::Schema {
89        crate::specification::Schema {
90            schema_type: Some("array".to_string()),
91            items: Some(Box::new(T::openapi_schema())),
92            ..Default::default()
93        }
94    }
95
96    fn schema_name() -> String {
97        format!("Vec<{}>", T::schema_name())
98    }
99}