oas-forge 0.1.4

The zero-runtime OpenAPI 3.1 compiler for Rust. Extracts, links, and merges code-first documentation.
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
use crate::type_mapper::map_syn_type_to_openapi;
use crate::visitor::json_merge;
use regex::Regex;
use serde_json::{Value, json};
use std::collections::HashSet;
use syn;

/// Parses a block of doc comments (lines) into an OpenAPI PathItem (YAML/JSON).
/// Returns Some(yaml_string) if a @route is detected, otherwise None.
pub fn parse_route_dsl(doc_lines: &[String], operation_id: &str) -> Option<String> {
    // 1. Check if it's a route
    // (Optimization: peek first)
    if !doc_lines.iter().any(|l| l.trim().starts_with("@route")) {
        return None;
    }

    let mut operation = json!({
        "summary": Value::Null,
        "description": Value::Null,
        "operationId": operation_id,
        "tags": [],
        "parameters": [],
        "responses": {}
    });

    let mut method = String::new();
    let mut path = String::new();
    let mut description_buffer = Vec::new();
    let mut dsl_override_buffer = Vec::new();
    let mut collecting_openapi = false;
    let mut summary: Option<String> = None;
    let mut declared_path_params = HashSet::new();

    // Regex for inline path parameters: {name: Type "Desc"}
    let re = Regex::new(r#"\{(\w+)(?::\s*([^"}]+))?(?:\s*"([^"]+)")?\}"#).unwrap();

    for line in doc_lines {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }

        if trimmed.starts_with('@') {
            collecting_openapi = false;
        }

        if trimmed.starts_with("@route") {
            let parts: Vec<&str> = trimmed.split_whitespace().collect();
            if parts.len() >= 3 {
                method = parts[1].to_lowercase();
                let raw_path = parts[2..].join(" ");

                // Route Parser Logic (from visitor.rs)
                let mut new_path = String::new();
                let mut last_end = 0;

                for cap in re.captures_iter(&raw_path) {
                    let full_match = cap.get(0).unwrap();
                    let name = cap.get(1).unwrap().as_str();
                    let type_str = cap.get(2).map(|m| m.as_str().trim());
                    let desc = cap.get(3).map(|m| m.as_str().to_string());

                    new_path.push_str(&raw_path[last_end..full_match.start()]);
                    new_path.push('{');
                    new_path.push_str(name);
                    new_path.push('}');
                    last_end = full_match.end();

                    let is_bare = type_str.is_none() && desc.is_none();

                    if !is_bare {
                        declared_path_params.insert(name.to_string());
                        let t = type_str.unwrap_or("String");
                        let (schema, _) = if let Ok(ty) = syn::parse_str::<syn::Type>(t) {
                            map_syn_type_to_openapi(&ty)
                        } else {
                            (json!({ "type": "string" }), true)
                        };

                        let mut param_obj = json!({
                            "name": name,
                            "in": "path",
                            "required": true,
                            "schema": schema
                        });

                        if let Some(d) = desc {
                            if let Value::Object(m) = &mut param_obj {
                                m.insert("description".to_string(), json!(d));
                            }
                        }

                        if let Value::Array(params) = operation.get_mut("parameters").unwrap() {
                            params.push(param_obj);
                        }
                    }
                }
                new_path.push_str(&raw_path[last_end..]);
                path = new_path;
            }
        } else if trimmed.starts_with("@tag") {
            let tags: Vec<String> = trimmed
                .strip_prefix("@tag")
                .unwrap()
                .split(',')
                .map(|s| s.trim().to_string())
                .filter(|s| !s.is_empty())
                .collect();
            operation["tags"] = json!(tags);
        } else if trimmed.starts_with("@path-param")
            || trimmed.starts_with("@query-param")
            || trimmed.starts_with("@header-param")
            || trimmed.starts_with("@cookie-param")
        {
            // Param Logic
            let (param_type, rest) = if trimmed.starts_with("@path-param") {
                ("path", trimmed.strip_prefix("@path-param").unwrap())
            } else if trimmed.starts_with("@query-param") {
                ("query", trimmed.strip_prefix("@query-param").unwrap())
            } else if trimmed.starts_with("@header-param") {
                ("header", trimmed.strip_prefix("@header-param").unwrap())
            } else {
                ("cookie", trimmed.strip_prefix("@cookie-param").unwrap())
            };

            let rest = rest.trim();
            if let Some(colon_idx) = rest.find(':') {
                let name = rest[..colon_idx].trim();
                let type_part = rest[colon_idx + 1..].trim();

                let tokens_vec: Vec<&str> = type_part.split_whitespace().collect();
                let first = tokens_vec.first().copied().unwrap_or("");

                let (type_def, start_idx) = if first == "deprecated"
                    || first == "required"
                    || first.starts_with("example=")
                    || first.starts_with('"')
                {
                    ("String", 0)
                } else if !tokens_vec.is_empty() {
                    (first, 1)
                } else {
                    ("String", 0)
                };

                let (schema, mut is_required) =
                    if let Ok(ty) = syn::parse_str::<syn::Type>(type_def) {
                        map_syn_type_to_openapi(&ty)
                    } else {
                        (json!({ "type": "string" }), true)
                    };

                let mut deprecated = false;
                let mut example = None;
                let mut desc = None;

                let mut desc_tokens = Vec::new();
                let mut in_desc = false;

                // Attributes check in tokens
                for token in tokens_vec.iter().skip(start_idx) {
                    if in_desc {
                        desc_tokens.push(*token);
                        continue;
                    }

                    if *token == "deprecated" {
                        deprecated = true;
                    } else if *token == "required" {
                        is_required = true;
                    } else if token.starts_with("example=") {
                        let val = token.strip_prefix("example=").unwrap().trim_matches('"');
                        example = Some(val.to_string());
                    } else if token.starts_with('"') {
                        in_desc = true;
                        desc_tokens.push(*token);
                    }
                }

                if !desc_tokens.is_empty() {
                    desc = Some(desc_tokens.join(" ").trim_matches('"').to_string());
                }

                let mut param_obj = json!({
                    "name": name,
                    "in": param_type,
                    "required": is_required,
                    "schema": schema
                });

                if deprecated {
                    param_obj
                        .as_object_mut()
                        .unwrap()
                        .insert("deprecated".to_string(), json!(true));
                }
                if let Some(ex) = example {
                    param_obj
                        .as_object_mut()
                        .unwrap()
                        .insert("example".to_string(), json!(ex));
                }
                if param_type == "path" {
                    declared_path_params.insert(name.to_string());
                    param_obj
                        .as_object_mut()
                        .unwrap()
                        .insert("required".to_string(), json!(true));
                }
                if let Some(d) = desc {
                    param_obj
                        .as_object_mut()
                        .unwrap()
                        .insert("description".to_string(), json!(d));
                }

                if let Value::Array(params) = operation.get_mut("parameters").unwrap() {
                    params.push(param_obj);
                }
            }
        } else if trimmed.starts_with("@body") {
            // ... Body Logic (Ported) ...
            let rest = trimmed.strip_prefix("@body").unwrap().trim();
            let parts: Vec<&str> = rest.split_whitespace().collect();
            if !parts.is_empty() {
                let schema_ref = parts[0];
                let mime = if parts.len() > 1 {
                    parts[1]
                } else {
                    "application/json"
                };

                let is_std_generic = schema_ref.starts_with("Option<")
                    || schema_ref.starts_with("Vec<")
                    || schema_ref.starts_with("Box<")
                    || schema_ref.starts_with("Arc<")
                    || schema_ref.starts_with("Rc<")
                    || schema_ref.starts_with("Cow<");

                let schema = if !is_std_generic
                    && (schema_ref.contains('<')
                        || (schema_ref.starts_with('$') && schema_ref.contains('<')))
                {
                    json!({ "$ref": schema_ref })
                } else if let Ok(ty) = syn::parse_str::<syn::Type>(schema_ref) {
                    map_syn_type_to_openapi(&ty).0
                } else if let Some(stripped) = schema_ref.strip_prefix('$') {
                    json!({ "$ref": format!("#/components/schemas/{}", stripped) })
                } else {
                    json!({ "$ref": format!("#/components/schemas/{}", schema_ref) })
                };

                operation["requestBody"] = json!({
                    "content": { mime: { "schema": schema } }
                });
            }
        } else if trimmed.starts_with("@return") {
            // ... Return Logic (Ported) ...
            let rest = trimmed.strip_prefix("@return").unwrap().trim();
            if let Some(colon_idx) = rest.find(':') {
                let code = rest[..colon_idx].trim();
                let residue = rest[colon_idx + 1..].trim();

                let (type_str, desc, is_unit) = if residue.starts_with('"') {
                    ("()", Some(residue.trim_matches('"').to_string()), true)
                } else if residue.ends_with('"') && residue.contains(" \"") {
                    let desc_start = residue.rfind(" \"").unwrap();
                    (
                        residue[..desc_start].trim(),
                        Some(residue[desc_start + 2..residue.len() - 1].to_string()),
                        false,
                    )
                } else if let Some(quote_start) = residue.find('"') {
                    (
                        residue[..quote_start].trim(),
                        Some(residue[quote_start + 1..residue.len() - 1].to_string()),
                        false,
                    )
                } else {
                    (residue, None, false)
                };

                let effective_unit = is_unit || type_str == "()" || type_str == "unit";
                let is_std_generic = type_str.starts_with("Option<")
                    || type_str.starts_with("Vec<")
                    || type_str.starts_with("Box<")
                    || type_str.starts_with("Arc<")
                    || type_str.starts_with("Rc<")
                    || type_str.starts_with("Cow<");

                let schema = if effective_unit {
                    json!({})
                } else if type_str.starts_with('{') {
                    serde_yaml_ng::from_str(type_str)
                        .unwrap_or_else(|_| json!({ "$ref": type_str }))
                } else if !is_std_generic
                    && (type_str.contains('<')
                        || (type_str.starts_with('$') && type_str.contains('<')))
                {
                    json!({ "$ref": type_str })
                } else if let Ok(ty) = syn::parse_str::<syn::Type>(type_str) {
                    map_syn_type_to_openapi(&ty).0
                } else if let Some(stripped) = type_str.strip_prefix('$') {
                    json!({ "$ref": format!("#/components/schemas/{}", stripped) })
                } else if type_str == "String" || type_str == "str" {
                    json!({ "type": "string" })
                } else {
                    json!({ "$ref": format!("#/components/schemas/{}", type_str) })
                };

                let mut resp_obj = json!({ "description": desc.unwrap_or_default() });
                if !effective_unit {
                    resp_obj["content"] = json!({ "application/json": { "schema": schema } });
                }

                if let Value::Object(responses) = operation.get_mut("responses").unwrap() {
                    responses.insert(code.to_string(), resp_obj);
                }
            }
        } else if trimmed.starts_with("@security") {
            // ... Security Logic ...
            let rest = trimmed.strip_prefix("@security").unwrap().trim();
            let (scheme, scopes) = if let Some(paren_start) = rest.find('(') {
                let name = rest[..paren_start].trim();
                let inner = &rest[paren_start + 1..rest.len() - 1];
                let s: Vec<String> = inner
                    .split(',')
                    .map(|s| s.trim().trim_matches('"').to_string())
                    .collect();
                (name, s)
            } else {
                (rest, vec![])
            };

            if operation.get("security").is_none() {
                operation["security"] = json!([]);
            }
            if let Value::Array(sec) = operation.get_mut("security").unwrap() {
                sec.push(json!({ scheme: scopes }));
            }
        } else if !trimmed.starts_with('@') {
            // Override Logic
            let is_yaml_key = trimmed.starts_with("parameters:")
                || trimmed.starts_with("requestBody:")
                || trimmed.starts_with("responses:")
                || trimmed.starts_with("security:")
                || trimmed.starts_with("externalDocs:")
                || trimmed.starts_with("callbacks:")
                || trimmed.starts_with("servers:");

            if is_yaml_key {
                collecting_openapi = true;
            }

            if collecting_openapi {
                dsl_override_buffer.push(line.to_string());
            } else if summary.is_none() {
                summary = Some(trimmed.to_string());
            } else {
                description_buffer.push(line.to_string());
            }
        }
    }

    if let Some(s) = summary {
        operation["summary"] = json!(s);
    }
    if !description_buffer.is_empty() {
        // Calculate min indentation (ignoring empty lines)
        let min_indent = description_buffer
            .iter()
            .filter(|l| !l.trim().is_empty())
            .map(|l| l.len() - l.trim_start().len())
            .min()
            .unwrap_or(0);

        let cleaned_desc: Vec<String> = description_buffer
            .iter()
            .map(|l| {
                if l.len() >= min_indent {
                    l[min_indent..].to_string()
                } else {
                    l.clone()
                }
            })
            .collect();

        operation["description"] = json!(cleaned_desc.join("\n"));
    }

    // Merge Overrides
    if !dsl_override_buffer.is_empty() {
        let override_yaml = dsl_override_buffer.join("\n");
        if let Ok(val) = serde_yaml_ng::from_str::<Value>(&override_yaml) {
            if !val.is_null() {
                json_merge(&mut operation, val);
            }
        }
    }

    // Validation (Path Params)
    let validation_re = Regex::new(r"\{(\w+)\}").unwrap();
    if !method.is_empty() && !path.is_empty() {
        for cap in validation_re.captures_iter(&path) {
            let var = cap.get(1).unwrap().as_str();
            if !declared_path_params.contains(var) {
                log::error!(
                    "Missing definition for path parameter '{}' in route '{}'. Skipping route.",
                    var,
                    path
                );
                return None;
            }
        }
        for declared in &declared_path_params {
            if !path.contains(&format!("{{{}}}", declared)) {
                log::error!(
                    "Declared path parameter '{}' is unused in route '{}'. Skipping route.",
                    declared,
                    path
                );
                return None;
            }
        }

        // Clean nulls
        if let Value::Object(map) = &mut operation {
            map.retain(|_, v| !v.is_null());
        }

        let mut method_map = serde_json::Map::new();
        method_map.insert(method, operation);
        let mut path_map = serde_json::Map::new();
        path_map.insert(path, Value::Object(method_map));

        let path_item = json!({ "paths": Value::Object(path_map) });

        if let Ok(generated) = serde_yaml_ng::to_string(&path_item) {
            return Some(generated.trim_start_matches("---\n").to_string());
        }
    }

    None
}