mrapids 0.1.31

Your OpenAPI, but executable
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
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
use crate::cli::{ListCommand, ListFormat, ListResource};
use crate::core::output::is_json_mode;
use crate::core::parser::parse_spec;
use crate::utils::security::validate_file_path;
use anyhow::Result;
use colored::*;
use serde_json::json;
use std::fs;
use std::path::Path;

pub fn list_command(mut cmd: ListCommand) -> Result<()> {
    // Global --json flag overrides per-command format
    if is_json_mode() {
        cmd.format = ListFormat::Json;
    }

    match cmd.resource {
        ListResource::Operations => list_operations(&cmd)?,
        ListResource::Requests => list_requests(&cmd)?,
        ListResource::All => {
            list_operations(&cmd)?;
            println!();
            list_requests(&cmd)?;
        }
    }

    Ok(())
}

fn list_operations(cmd: &ListCommand) -> Result<()> {
    // Use spec from command if provided, otherwise look in default locations
    let spec_file = if let Some(spec) = &cmd.spec {
        spec.clone()
    } else {
        // Try to find spec file in common locations
        let spec_paths = vec![
            Path::new("specs/api.yaml"),
            Path::new("specs/api.json"),
            Path::new("api.yaml"),
            Path::new("api.json"),
            Path::new("openapi.yaml"),
            Path::new("openapi.json"),
            Path::new("swagger.yaml"),
            Path::new("swagger.json"),
        ];

        let spec_path = spec_paths.iter().find(|p| p.exists());

        if spec_path.is_none() {
            eprintln!(
                "{} No API specification found. Provide a spec file or run 'mrapids init' first.",
                "⚠️".yellow()
            );
            eprintln!("  Usage: mrapids list operations <spec-file>");
            return Ok(());
        }

        spec_path.unwrap().to_path_buf()
    };

    let spec_file = &spec_file;

    // Validate spec file path
    validate_file_path(spec_file)?;

    // Load and parse the spec
    let content = fs::read_to_string(spec_file)?;
    let spec = parse_spec(&content)?;

    // Collect operations
    let mut operations = Vec::new();
    let mut index = 1;

    for operation in &spec.operations {
        // Apply filters
        if let Some(ref filter_method) = cmd.method {
            if operation.method.to_uppercase() != filter_method.to_uppercase() {
                continue;
            }
        }

        if let Some(ref filter_text) = cmd.filter {
            let search_text = filter_text.to_lowercase();
            let matches = operation.operation_id.to_lowercase().contains(&search_text)
                || operation.path.to_lowercase().contains(&search_text)
                || operation
                    .summary
                    .as_ref()
                    .map(|s| s.to_lowercase().contains(&search_text))
                    .unwrap_or(false);

            if !matches {
                continue;
            }
        }

        // Tag filtering not supported in simplified model
        if cmd.tag.is_some() {
            // Skip tag filtering for now
        }

        operations.push((
            index,
            operation.operation_id.clone(),
            operation.method.to_uppercase(),
            operation.path.clone(),
            operation.summary.clone().unwrap_or_default(),
            has_example(&Some(operation.operation_id.clone())),
        ));
        index += 1;
    }

    // Display results based on format
    match cmd.format {
        ListFormat::Table => display_operations_table(&operations, &spec),
        ListFormat::Simple => display_operations_simple(&operations),
        ListFormat::Json => display_operations_json(&operations),
        ListFormat::Yaml => display_operations_yaml(&operations),
    }

    Ok(())
}

fn list_requests(cmd: &ListCommand) -> Result<()> {
    let requests_dir = Path::new("requests");

    if !requests_dir.exists() {
        eprintln!(
            "{} No saved requests found. Run 'mrapids analyze' to generate examples.",
            "📋".cyan()
        );
        return Ok(());
    }

    // Collect request files
    let mut requests = Vec::new();

    // Check main requests directory
    if let Ok(entries) = fs::read_dir(requests_dir) {
        for entry in entries.flatten() {
            if let Some(ext) = entry.path().extension() {
                if ext == "yaml" || ext == "yml" {
                    if let Ok(content) = fs::read_to_string(entry.path()) {
                        if let Ok(config) = serde_yaml::from_str::<serde_yaml::Value>(&content) {
                            let name = entry
                                .file_name()
                                .to_string_lossy()
                                .replace(".yaml", "")
                                .replace(".yml", "");
                            let method =
                                config.get("method").and_then(|v| v.as_str()).unwrap_or("?");
                            let path = config.get("path").and_then(|v| v.as_str()).unwrap_or("?");
                            let description = config
                                .get("description")
                                .and_then(|v| v.as_str())
                                .unwrap_or("");

                            // Apply filters
                            if let Some(ref filter_method) = cmd.method {
                                if method != filter_method.to_uppercase() {
                                    continue;
                                }
                            }

                            if let Some(ref filter_text) = cmd.filter {
                                let search_text = filter_text.to_lowercase();
                                if !name.to_lowercase().contains(&search_text)
                                    && !path.to_lowercase().contains(&search_text)
                                    && !description.to_lowercase().contains(&search_text)
                                {
                                    continue;
                                }
                            }

                            requests.push((
                                name,
                                method.to_string(),
                                path.to_string(),
                                description.to_string(),
                            ));
                        }
                    }
                }
            }
        }
    }

    // Check examples directory
    let examples_dir = requests_dir.join("examples");
    if examples_dir.exists() {
        if let Ok(entries) = fs::read_dir(examples_dir) {
            for entry in entries.flatten() {
                if let Some(ext) = entry.path().extension() {
                    if ext == "yaml" || ext == "yml" {
                        if let Ok(content) = fs::read_to_string(entry.path()) {
                            if let Ok(config) = serde_yaml::from_str::<serde_yaml::Value>(&content)
                            {
                                let name = format!(
                                    "examples/{}",
                                    entry
                                        .file_name()
                                        .to_string_lossy()
                                        .replace(".yaml", "")
                                        .replace(".yml", "")
                                );
                                let method =
                                    config.get("method").and_then(|v| v.as_str()).unwrap_or("?");
                                let path =
                                    config.get("path").and_then(|v| v.as_str()).unwrap_or("?");
                                let description = config
                                    .get("description")
                                    .and_then(|v| v.as_str())
                                    .unwrap_or("");

                                // Apply filters
                                if let Some(ref filter_method) = cmd.method {
                                    if method != filter_method.to_uppercase() {
                                        continue;
                                    }
                                }

                                if let Some(ref filter_text) = cmd.filter {
                                    let search_text = filter_text.to_lowercase();
                                    if !name.to_lowercase().contains(&search_text)
                                        && !path.to_lowercase().contains(&search_text)
                                        && !description.to_lowercase().contains(&search_text)
                                    {
                                        continue;
                                    }
                                }

                                requests.push((
                                    name,
                                    method.to_string(),
                                    path.to_string(),
                                    description.to_string(),
                                ));
                            }
                        }
                    }
                }
            }
        }
    }

    // Display results
    match cmd.format {
        ListFormat::Table => display_requests_table(&requests),
        ListFormat::Simple => display_requests_simple(&requests),
        ListFormat::Json => display_requests_json(&requests),
        ListFormat::Yaml => display_requests_yaml(&requests),
    }

    Ok(())
}

fn display_operations_table(
    operations: &[(usize, String, String, String, String, bool)],
    spec: &crate::core::parser::UnifiedSpec,
) {
    if operations.is_empty() {
        println!("{} No operations found matching criteria", "📋".cyan());
        return;
    }

    let title = format!(
        "{} - Available Operations ({} total)",
        spec.info.title,
        operations.len()
    );

    // Calculate column widths
    let max_op_len = operations
        .iter()
        .map(|(_, op, _, _, _, _)| op.len())
        .max()
        .unwrap_or(20)
        .min(30);
    let max_path_len = operations
        .iter()
        .map(|(_, _, _, path, _, _)| path.len())
        .max()
        .unwrap_or(20)
        .min(40);

    // Print table
    println!("┌{:─<72}┐", "");
    println!("│ {:^70} │", title.bright_yellow());
    println!(
        "{:─<4}┬{:─<width_op$}┬{:─<8}┬{:─<width_path$}┬{:─<10}┤",
        "",
        "",
        "",
        "",
        "",
        width_op = max_op_len + 2,
        width_path = max_path_len + 2
    );
    println!(
        "{} │ {:^width_op$} │ {:^8} │ {:^width_path$} │ {:^10} │",
        "#".bright_cyan(),
        "Operation ID".bright_cyan(),
        "Method".bright_cyan(),
        "Path".bright_cyan(),
        "Example".bright_cyan(),
        width_op = max_op_len,
        width_path = max_path_len
    );
    println!(
        "{:─<4}┼{:─<width_op$}┼{:─<8}┼{:─<width_path$}┼{:─<10}┤",
        "",
        "",
        "",
        "",
        "",
        width_op = max_op_len + 2,
        width_path = max_path_len + 2
    );

    for (idx, op_id, method, path, _desc, has_ex) in operations {
        let method_colored = match method.as_str() {
            "GET" => method.green(),
            "POST" => method.yellow(),
            "PUT" => method.blue(),
            "DELETE" => method.red(),
            "PATCH" => method.magenta(),
            _ => method.normal(),
        };

        let example_mark = if *has_ex { "".green() } else { "-".dimmed() };

        println!(
            "{:>2} │ {:<width_op$} │ {:^8} │ {:<width_path$} │ {:^10} │",
            idx,
            op_id,
            method_colored,
            path,
            example_mark,
            width_op = max_op_len,
            width_path = max_path_len
        );
    }

    println!(
        "{:─<4}┴{:─<width_op$}┴{:─<8}┴{:─<width_path$}┴{:─<10}┘",
        "",
        "",
        "",
        "",
        "",
        width_op = max_op_len + 2,
        width_path = max_path_len + 2
    );

    println!("\n{} Usage:", "💡".bright_yellow());
    println!("  mrapids run <operation-id>         # Run by operation ID");
    println!("  mrapids run-op <number>            # Run by number from list");
    println!("  mrapids analyze --operation <id>   # Generate example for specific operation");
}

fn display_operations_simple(operations: &[(usize, String, String, String, String, bool)]) {
    for (idx, op_id, method, path, desc, _) in operations {
        println!(
            "{:>3}. {} {} {} {}",
            idx,
            method.bright_cyan(),
            path.yellow(),
            format!("[{}]", op_id).dimmed(),
            if !desc.is_empty() {
                format!("- {}", desc).dimmed()
            } else {
                "".normal()
            }
        );
    }
}

fn display_operations_json(operations: &[(usize, String, String, String, String, bool)]) {
    let json_ops: Vec<_> = operations
        .iter()
        .map(|(idx, op_id, method, path, desc, has_ex)| {
            json!({
                "index": idx,
                "operationId": op_id,
                "method": method,
                "path": path,
                "description": desc,
                "hasExample": has_ex
            })
        })
        .collect();

    println!("{}", serde_json::to_string_pretty(&json_ops).unwrap());
}

fn display_operations_yaml(operations: &[(usize, String, String, String, String, bool)]) {
    let yaml_ops: Vec<_> = operations
        .iter()
        .map(|(idx, op_id, method, path, desc, has_ex)| {
            json!({
                "index": idx,
                "operationId": op_id,
                "method": method,
                "path": path,
                "description": desc,
                "hasExample": has_ex
            })
        })
        .collect();

    println!("{}", serde_yaml::to_string(&yaml_ops).unwrap());
}

fn display_requests_table(requests: &[(String, String, String, String)]) {
    if requests.is_empty() {
        println!("{} No saved requests found", "📋".cyan());
        return;
    }

    println!("┌{:─<72}┐", "");
    println!(
        "│ {:^70} │",
        format!("Saved Requests ({} total)", requests.len()).bright_yellow()
    );
    println!("├{:─<25}┬{:─<8}┬{:─<35}┤", "", "", "");
    println!(
        "│ {:^23} │ {:^8} │ {:^33} │",
        "Name".bright_cyan(),
        "Method".bright_cyan(),
        "Path".bright_cyan()
    );
    println!("├{:─<25}┼{:─<8}┼{:─<35}┤", "", "", "");

    for (name, method, path, _desc) in requests {
        let method_colored = match method.as_str() {
            "GET" => method.green(),
            "POST" => method.yellow(),
            "PUT" => method.blue(),
            "DELETE" => method.red(),
            "PATCH" => method.magenta(),
            _ => method.normal(),
        };

        println!("│ {:<23} │ {:^8} │ {:<33} │", name, method_colored, path);
    }

    println!("└{:─<25}┴{:─<8}┴{:─<35}┘", "", "", "");

    println!("\n{} Usage:", "💡".bright_yellow());
    println!("  mrapids run <request-name>     # Run saved request");
    println!("  mrapids run <name> --data file # Use different data file");
}

fn display_requests_simple(requests: &[(String, String, String, String)]) {
    for (name, method, path, desc) in requests {
        println!(
            "{} {} {} {}",
            name.yellow(),
            method.bright_cyan(),
            path,
            if !desc.is_empty() {
                format!("- {}", desc).dimmed()
            } else {
                "".normal()
            }
        );
    }
}

fn display_requests_json(requests: &[(String, String, String, String)]) {
    let json_reqs: Vec<_> = requests
        .iter()
        .map(|(name, method, path, desc)| {
            json!({
                "name": name,
                "method": method,
                "path": path,
                "description": desc
            })
        })
        .collect();

    println!("{}", serde_json::to_string_pretty(&json_reqs).unwrap());
}

fn display_requests_yaml(requests: &[(String, String, String, String)]) {
    let yaml_reqs: Vec<_> = requests
        .iter()
        .map(|(name, method, path, desc)| {
            json!({
                "name": name,
                "method": method,
                "path": path,
                "description": desc
            })
        })
        .collect();

    println!("{}", serde_yaml::to_string(&yaml_reqs).unwrap());
}

fn has_example(op_id: &Option<String>) -> bool {
    if let Some(id) = op_id {
        let example_file =
            Path::new("requests/examples").join(format!("{}.yaml", to_kebab_case(id)));
        example_file.exists()
    } else {
        false
    }
}

fn to_kebab_case(s: &str) -> String {
    let mut result = String::new();
    let mut prev_is_lower = false;

    for ch in s.chars() {
        if ch == '/' || ch == '\\' {
            // Replace path separators with hyphens
            result.push('-');
            prev_is_lower = false;
        } else if ch == ' ' || ch == '_' {
            // Replace spaces and underscores with hyphens
            result.push('-');
            prev_is_lower = false;
        } else if ch.is_uppercase() && prev_is_lower {
            result.push('-');
            result.push(ch.to_lowercase().next().unwrap());
            prev_is_lower = false;
        } else if ch.is_alphanumeric() {
            result.push(ch.to_lowercase().next().unwrap());
            prev_is_lower = ch.is_lowercase();
        }
        // Skip other special characters
    }

    // Remove multiple consecutive hyphens and trim
    let cleaned = result
        .split('-')
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join("-");

    cleaned
}