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
use syn::punctuated::Punctuated;
use syn::{Attribute, Expr, ExprLit, Lit, Meta};
/// Helper to extract doc comments from attributes
pub fn extract_doc_comments(attrs: &[Attribute]) -> Vec<String> {
let mut doc_lines = Vec::new();
for attr in attrs {
if attr.path().is_ident("doc") {
if let Meta::NameValue(meta) = &attr.meta {
if let Expr::Lit(expr_lit) = &meta.value {
if let Lit::Str(lit_str) = &expr_lit.lit {
doc_lines.push(lit_str.value());
}
}
}
}
}
doc_lines
}
pub fn apply_casing(text: &str, case: &str) -> String {
match case {
"lowercase" => text.to_lowercase(),
"UPPERCASE" => text.to_uppercase(),
"PascalCase" => {
// Check if it contains underscores (snake_case -> PascalCase)
if text.contains('_') {
text.split('_')
.map(|part| {
let mut c = part.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().to_string() + c.as_str(),
}
})
.collect()
} else {
// Assume it is already Pascal or camel, just ensure first char is Upper
let mut c = text.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().to_string() + c.as_str(),
}
}
}
"camelCase" => {
// Check if it contains underscores (snake_case -> camelCase)
if text.contains('_') {
let parts: Vec<&str> = text.split('_').collect();
if parts.is_empty() {
return String::new();
}
let first = parts[0].to_lowercase();
let rest: String = parts[1..]
.iter()
.map(|part| {
let mut c = part.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_uppercase().to_string() + c.as_str(),
}
})
.collect();
first + &rest
} else {
// Just ensure first char is Lower
let mut c = text.chars();
match c.next() {
None => String::new(),
Some(f) => f.to_lowercase().to_string() + c.as_str(),
}
}
}
"snake_case" => {
let mut s = String::new();
for (i, c) in text.chars().enumerate() {
if c.is_uppercase() && i > 0 {
s.push('_');
}
if let Some(lower) = c.to_lowercase().next() {
s.push(lower);
}
}
s
}
"SCREAMING_SNAKE_CASE" => apply_casing(text, "snake_case").to_uppercase(),
"kebab-case" => apply_casing(text, "snake_case").replace('_', "-"),
"SCREAMING-KEBAB-CASE" => apply_casing(text, "kebab-case").to_uppercase(),
_ => text.to_string(),
}
}
/// Extracts doc comments and handles "@openapi rename/rename-all" + Serde logic.
pub fn extract_naming_and_doc(
attrs: &[Attribute],
default_name: &str,
) -> (
String,
String,
Option<String>,
Vec<String>,
Option<String>,
Option<String>,
) {
let mut doc_lines = Vec::new();
// We collect cleaned lines here (without @openapi tags)
let mut clean_doc_lines = Vec::new();
let mut final_name = default_name.to_string();
let mut rename_rule = None;
let mut serde_tag = None;
let mut serde_content = None;
// 1. Check Serde Attributes (Lower Precedence)
for attr in attrs {
if attr.path().is_ident("serde") {
if let Meta::List(list) = &attr.meta {
if let Ok(nested) =
list.parse_args_with(Punctuated::<Meta, syn::Token![,]>::parse_terminated)
{
for meta in nested {
if let Meta::NameValue(nv) = meta {
if nv.path.is_ident("rename") {
if let Expr::Lit(ExprLit {
lit: Lit::Str(s), ..
}) = nv.value
{
final_name = s.value();
}
} else if nv.path.is_ident("rename_all") {
if let Expr::Lit(ExprLit {
lit: Lit::Str(s), ..
}) = nv.value
{
rename_rule = Some(s.value());
}
} else if nv.path.is_ident("tag") {
if let Expr::Lit(ExprLit {
lit: Lit::Str(s), ..
}) = nv.value
{
serde_tag = Some(s.value());
}
} else if nv.path.is_ident("content") {
if let Expr::Lit(ExprLit {
lit: Lit::Str(s), ..
}) = nv.value
{
serde_content = Some(s.value());
}
}
}
}
}
}
}
}
// 2. Doc Comments (Higher Precedence)
let mut in_openapi_block = false;
for attr in attrs {
if attr.path().is_ident("doc") {
if let Meta::NameValue(meta) = &attr.meta {
if let Expr::Lit(expr_lit) = &meta.value {
if let Lit::Str(lit_str) = &expr_lit.lit {
let val = lit_str.value();
doc_lines.push(val.clone());
let trimmed = val.trim();
if trimmed.starts_with("@openapi") {
in_openapi_block = true;
let rest = trimmed.strip_prefix("@openapi").unwrap().trim();
if rest.starts_with("rename-all") {
let rule = rest
.strip_prefix("rename-all")
.unwrap()
.trim()
.trim_matches('"');
rename_rule = Some(rule.to_string());
} else if rest.starts_with("rename") {
let name_part = rest
.strip_prefix("rename")
.unwrap()
.trim()
.trim_matches('"');
final_name = name_part.to_string();
}
} else if !in_openapi_block {
clean_doc_lines.push(val.trim().to_string());
}
}
}
}
}
}
(
final_name,
clean_doc_lines.join(" "),
rename_rule,
doc_lines,
serde_tag,
serde_content,
)
}
use serde_json::{Value, json};
/// Extracts validation attributes from `#[validate(...)]` and maps them to OpenAPI properties.
pub fn extract_validation(attrs: &[Attribute]) -> Value {
let mut validation_schema = serde_json::Map::new();
for attr in attrs {
if attr.path().is_ident("validate") {
if let Meta::List(list) = &attr.meta {
if let Ok(nested) =
list.parse_args_with(Punctuated::<Meta, syn::Token![,]>::parse_terminated)
{
for meta in nested {
match meta {
// Helper: #[validate(email)]
Meta::Path(p) if p.is_ident("email") => {
validation_schema.insert("format".to_string(), json!("email"));
}
// Helper: #[validate(url)]
Meta::Path(p) if p.is_ident("url") => {
validation_schema.insert("format".to_string(), json!("uri"));
}
// Helper: #[validate(length(min = 1, max = 10))]
Meta::List(list) if list.path.is_ident("length") => {
if let Ok(args) = list.parse_args_with(
Punctuated::<Meta, syn::Token![,]>::parse_terminated,
) {
for arg in args {
if let Meta::NameValue(nv) = arg {
if let Expr::Lit(ExprLit {
lit: Lit::Int(i), ..
}) = nv.value
{
if let Ok(val) = i.base10_parse::<u64>() {
if nv.path.is_ident("min") {
validation_schema.insert(
"minLength".to_string(),
json!(val),
);
} else if nv.path.is_ident("max") {
validation_schema.insert(
"maxLength".to_string(),
json!(val),
);
}
}
}
}
}
}
}
// Helper: #[validate(range(min = 1, max = 10))]
Meta::List(list) if list.path.is_ident("range") => {
if let Ok(args) = list.parse_args_with(
Punctuated::<Meta, syn::Token![,]>::parse_terminated,
) {
for arg in args {
if let Meta::NameValue(nv) = arg {
if let Expr::Lit(ExprLit {
lit: Lit::Int(i), ..
}) = nv.value
{
if let Ok(val) = i.base10_parse::<i64>() {
if nv.path.is_ident("min") {
validation_schema.insert(
"minimum".to_string(),
json!(val),
);
} else if nv.path.is_ident("max") {
validation_schema.insert(
"maximum".to_string(),
json!(val),
);
}
}
}
}
}
}
}
// Helper: #[validate(regex = "path")] or #[validate(pattern = "...")]
Meta::NameValue(nv) => {
if nv.path.is_ident("pattern") {
if let Expr::Lit(ExprLit {
lit: Lit::Str(s), ..
}) = nv.value
{
validation_schema
.insert("pattern".to_string(), json!(s.value()));
}
}
}
_ => {}
}
}
}
}
}
}
Value::Object(validation_schema)
}