cargo-mate 1.8.0

Rust development companion that enhances cargo with intelligent workflows, state management, performance optimization, and comprehensive project monitoring.
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
use super::{Tool, ToolError, Result, OutputFormat, parse_output_format};
use clap::{Arg, ArgMatches, Command};
use std::path::Path;
use std::fs;
use std::collections::HashMap;
use colored::*;
use syn::{parse_file, visit::Visit, ItemMacro, Macro};
use quote::{quote, ToTokens};
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MacroCall {
    pub name: String,
    pub args: String,
    pub span_placeholder: String,
    pub file: String,
    pub line: usize,
    pub column: usize,
}
#[derive(Debug, Clone)]
pub struct ExpansionStep {
    pub before: String,
    pub after: String,
    pub macro_name: String,
    pub description: String,
    pub line: usize,
}
#[derive(Debug, Clone)]
pub struct ValidationIssue {
    pub issue: String,
    pub severity: String,
    pub line: usize,
    pub suggestion: String,
}
#[derive(Debug, Clone)]
pub struct MacroDependency {
    pub name: String,
    pub dependency_type: String,
    pub location: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum MacroType {
    Declarative,
    Procedural,
    BuiltIn,
    Unknown,
}
pub struct MacroExpandTool;
impl MacroExpandTool {
    pub fn new() -> Self {
        Self
    }
    fn parse_macro_calls(&self, file_path: &str) -> Result<Vec<MacroCall>> {
        if !Path::new(file_path).exists() {
            return Err(
                ToolError::InvalidArguments(format!("File not found: {}", file_path)),
            );
        }
        let content = fs::read_to_string(file_path)?;
        let ast = parse_file(&content)
            .map_err(|e| ToolError::ExecutionFailed(
                format!("Failed to parse Rust file: {}", e),
            ))?;
        let mut visitor = MacroCallVisitor::new(file_path.to_string());
        visitor.visit_file(&ast);
        Ok(visitor.macro_calls)
    }
    fn expand_macro_step_by_step(
        &self,
        macro_call: &MacroCall,
    ) -> Result<Vec<ExpansionStep>> {
        let mut steps = Vec::new();
        if let Ok(macro_def) = self
            .find_macro_definition(&macro_call.name, &macro_call.file)
        {
            steps
                .push(ExpansionStep {
                    before: macro_call.args.clone(),
                    after: self.show_pattern_match(&macro_call.args, &macro_def)?,
                    macro_name: macro_call.name.clone(),
                    description: "Pattern matching and hygiene application".to_string(),
                    line: macro_call.line,
                });
            steps
                .push(ExpansionStep {
                    before: steps.last().unwrap().after.clone(),
                    after: self.apply_hygiene(&steps.last().unwrap().after)?,
                    macro_name: macro_call.name.clone(),
                    description: "Hygiene application".to_string(),
                    line: macro_call.line,
                });
        } else {
            steps
                .push(ExpansionStep {
                    before: macro_call.args.clone(),
                    after: format!("/* {} expanded */", macro_call.name),
                    macro_name: macro_call.name.clone(),
                    description: "Macro expansion (definition not found)".to_string(),
                    line: macro_call.line,
                });
        }
        Ok(steps)
    }
    fn find_macro_definition(
        &self,
        macro_name: &str,
        file_path: &str,
    ) -> Result<String> {
        let content = fs::read_to_string(file_path)?;
        let macro_pattern = format!(
            "macro_rules!\\s*{}\\s*\\{{", regex::escape(macro_name)
        );
        let re = regex::Regex::new(&macro_pattern).unwrap();
        if let Some(mat) = re.find(&content) {
            let start = mat.start();
            let mut brace_count = 0;
            let mut end = start;
            for (i, c) in content[start..].chars().enumerate() {
                match c {
                    '{' => brace_count += 1,
                    '}' => brace_count -= 1,
                    _ => {}
                }
                end = start + i;
                if brace_count == 0 {
                    break;
                }
            }
            Ok(content[start..=end].to_string())
        } else {
            Err(
                ToolError::ExecutionFailed(
                    format!("Macro definition for '{}' not found", macro_name),
                ),
            )
        }
    }
    fn show_pattern_match(&self, args: &str, macro_def: &str) -> Result<String> {
        let args = args.trim_matches(&['(', ')'][..]);
        if macro_def.contains("$x:expr") {
            let elements: Vec<&str> = args.split(',').collect();
            let mut result = "{\n    let mut v = Vec::new();".to_string();
            for elem in elements {
                result.push_str(&format!("\n    v.push({});", elem.trim()));
            }
            result.push_str("\n    v\n}");
            Ok(result)
        } else {
            Ok(format!("/* Pattern matched: {} */", args))
        }
    }
    fn apply_hygiene(&self, code: &str) -> Result<String> {
        let mut result = code
            .replace("Vec::", "::alloc::vec::Vec::")
            .replace("vec!", "::alloc::vec!");
        if result.contains("::alloc::vec::Vec::new()") {
            result = result
                .replace("::alloc::vec::Vec::new()", "::alloc::vec::Vec::new");
        }
        Ok(result)
    }
    fn generate_expanded_code(&self, file_path: &str) -> Result<String> {
        let macro_calls = self.parse_macro_calls(file_path)?;
        let content = fs::read_to_string(file_path)?;
        if macro_calls.is_empty() {
            return Ok(content);
        }
        let mut expanded = content.clone();
        for call in macro_calls {
            if let Ok(steps) = self.expand_macro_step_by_step(&call) {
                if let Some(final_step) = steps.last() {
                    let macro_call_pattern = format!("{}!{}", call.name, call.args);
                    expanded = expanded.replace(&macro_call_pattern, &final_step.after);
                }
            }
        }
        Ok(expanded)
    }
    fn highlight_differences(&self, original: &str, expanded: &str) -> Result<String> {
        let original_lines: Vec<&str> = original.lines().collect();
        let expanded_lines: Vec<&str> = expanded.lines().collect();
        let mut result = String::new();
        for (i, (orig, exp)) in original_lines
            .iter()
            .zip(expanded_lines.iter())
            .enumerate()
        {
            if orig != exp {
                result
                    .push_str(
                        &format!(
                            "{} {} {}\n", format!("{}:", i + 1) .yellow(), "-".red(),
                            orig.red()
                        ),
                    );
                result
                    .push_str(
                        &format!(
                            "{} {} {}\n", format!("{}:", i + 1) .yellow(), "+".green(),
                            exp.green()
                        ),
                    );
            } else {
                result
                    .push_str(
                        &format!("{}   {}\n", format!("{}:", i + 1) .blue(), orig),
                    );
            }
        }
        Ok(result)
    }
    fn extract_macro_dependencies(
        &self,
        macro_call: &MacroCall,
    ) -> Vec<MacroDependency> {
        let mut deps = Vec::new();
        if macro_call.args.contains("vec!") {
            deps.push(MacroDependency {
                name: "vec".to_string(),
                dependency_type: "Built-in macro".to_string(),
                location: "std library".to_string(),
            });
        }
        if macro_call.name.contains("println") || macro_call.name.contains("print") {
            deps.push(MacroDependency {
                name: "print".to_string(),
                dependency_type: "Built-in macro".to_string(),
                location: "std library".to_string(),
            });
        }
        deps
    }
    fn validate_macro_expansion(
        &self,
        expanded_code: &str,
    ) -> Result<Vec<ValidationIssue>> {
        let mut issues = Vec::new();
        if let Err(_) = syn::parse_file(expanded_code) {
            issues
                .push(ValidationIssue {
                    issue: "Expanded code contains syntax errors".to_string(),
                    severity: "High".to_string(),
                    line: 0,
                    suggestion: "Review macro definition and arguments".to_string(),
                });
        }
        if expanded_code.contains("unresolved name") {
            issues
                .push(ValidationIssue {
                    issue: "Unresolved names in expanded code".to_string(),
                    severity: "Medium".to_string(),
                    line: 0,
                    suggestion: "Check macro hygiene and imports".to_string(),
                });
        }
        Ok(issues)
    }
    fn classify_macro(&self, macro_name: &str) -> MacroType {
        match macro_name {
            "println" | "print" | "format" | "vec" | "assert" | "panic" => {
                MacroType::BuiltIn
            }
            name if name.contains("macro_rules!") => MacroType::Declarative,
            name if name.contains("#[") => MacroType::Procedural,
            _ => MacroType::Unknown,
        }
    }
    fn format_expanded_code(&self, code: &str, format: &str) -> Result<String> {
        match format {
            "rust" => Ok(code.to_string()),
            "html" => self.format_as_html(code),
            "json" => self.format_as_json(code),
            _ => Ok(code.to_string()),
        }
    }
    fn format_as_html(&self, code: &str) -> Result<String> {
        let html = format!(
            "<!DOCTYPE html>
<html>
<head>
    <title>Macro Expansion</title>
    <style>
        .code {{ font-family: 'Monaco', 'Menlo', monospace; background: #f5f5f5; padding: 1em; }}
        .expanded {{ color: #28a745; }}
        .original {{ color: #dc3545; }}
    </style>
</head>
<body>
    <h1>Macro Expansion Result</h1>
    <pre class=\"code\">{}</pre>
</body>
</html>",
            code.replace("<", "&lt;").replace(">", "&gt;")
        );
        Ok(html)
    }
    fn format_as_json(&self, code: &str) -> Result<String> {
        let json = serde_json::json!(
            { "expanded_code" : code, "timestamp" : chrono::Utc::now().to_rfc3339(),
            "tool" : "macro-expand" }
        );
        Ok(serde_json::to_string_pretty(&json).unwrap())
    }
}
struct MacroCallVisitor {
    macro_calls: Vec<MacroCall>,
    current_file: String,
}
impl MacroCallVisitor {
    fn new(file_path: String) -> Self {
        Self {
            macro_calls: Vec::new(),
            current_file: file_path,
        }
    }
}
impl<'ast> Visit<'ast> for MacroCallVisitor {
    fn visit_macro(&mut self, node: &'ast Macro) {
        if let Some(last_segment) = node.path.segments.last() {
            let macro_name = last_segment.ident.to_string();
            let args = quote!(# node).to_string();
            self.macro_calls
                .push(MacroCall {
                    name: macro_name,
                    args: format!("({})", args),
                    span_placeholder: "span_info_unavailable".to_string(),
                    file: self.current_file.clone(),
                    line: 0,
                    column: 0,
                });
        }
        syn::visit::visit_macro(self, node);
    }
}
impl Tool for MacroExpandTool {
    fn name(&self) -> &'static str {
        "macro-expand"
    }
    fn description(&self) -> &'static str {
        "Better macro expansion viewer with step-by-step expansion and syntax highlighting"
    }
    fn command(&self) -> Command {
        Command::new(self.name())
            .about(self.description())
            .long_about(
                "Provide a better macro expansion viewer with syntax highlighting, step-by-step expansion, and interactive exploration.\n\
                 \n\
                 This tool helps you understand complex macros by:\n\
                 • Expanding procedural and declarative macros\n\
                 • Showing intermediate expansion steps\n\
                 • Syntax highlighting for expanded code\n\
                 • Comparing original vs expanded code\n\
                 \n\
                 EXAMPLES:\n\
                 cm tool macro-expand --input src/lib.rs --step-by-step\n\
                 cm tool macro-expand --input src/main.rs --macro my_macro --highlight\n\
                 cm tool macro-expand --input src/lib.rs --diff --validate",
            )
            .args(
                &[
                    Arg::new("input")
                        .long("input")
                        .short('i')
                        .help("Input Rust file to analyze")
                        .required(true),
                    Arg::new("macro")
                        .long("macro")
                        .short('m')
                        .help("Specific macro to expand (expand all if not specified)"),
                    Arg::new("step-by-step")
                        .long("step-by-step")
                        .help("Show step-by-step expansion")
                        .action(clap::ArgAction::SetTrue),
                    Arg::new("highlight")
                        .long("highlight")
                        .help("Highlight expanded code with syntax highlighting")
                        .action(clap::ArgAction::SetTrue),
                    Arg::new("diff")
                        .long("diff")
                        .help("Show diff between original and expanded")
                        .action(clap::ArgAction::SetTrue),
                    Arg::new("validate")
                        .long("validate")
                        .help("Validate that expanded code compiles")
                        .action(clap::ArgAction::SetTrue),
                    Arg::new("interactive")
                        .long("interactive")
                        .help("Interactive macro exploration mode")
                        .action(clap::ArgAction::SetTrue),
                    Arg::new("output")
                        .long("output")
                        .short('o')
                        .help("Output file for expanded code")
                        .default_value("expanded.rs"),
                    Arg::new("format")
                        .long("format")
                        .short('f')
                        .help("Output format: rust, html, json")
                        .default_value("rust"),
                ],
            )
            .args(&super::common_options())
    }
    fn execute(&self, matches: &ArgMatches) -> Result<()> {
        let input = matches.get_one::<String>("input").unwrap();
        let specific_macro = matches.get_one::<String>("macro");
        let step_by_step = matches.get_flag("step-by-step");
        let highlight = matches.get_flag("highlight");
        let diff = matches.get_flag("diff");
        let validate = matches.get_flag("validate");
        let interactive = matches.get_flag("interactive");
        let output_file = matches.get_one::<String>("output").unwrap();
        let format = matches.get_one::<String>("format").unwrap();
        let verbose = matches.get_flag("verbose");
        let dry_run = matches.get_flag("dry-run");
        let output_format = parse_output_format(matches);
        if dry_run {
            println!("🔍 Would analyze macro expansion in: {}", input);
            return Ok(());
        }
        match output_format {
            OutputFormat::Human => {
                println!(
                    "🔍 {} - {}", "Macro Expansion Analysis".bold(), self.description()
                    .cyan()
                );
                match self.parse_macro_calls(input) {
                    Ok(macro_calls) => {
                        println!("\n📁 File: {}", input.bold());
                        println!(
                            "🔍 Macros Found: {}", macro_calls.len().to_string().cyan()
                        );
                        if macro_calls.is_empty() {
                            println!("✅ No macro calls found in the file.");
                            return Ok(());
                        }
                        let filtered_calls: Vec<_> = if let Some(macro_name) = specific_macro {
                            macro_calls
                                .into_iter()
                                .filter(|call| call.name == *macro_name)
                                .collect()
                        } else {
                            macro_calls
                        };
                        if filtered_calls.is_empty() {
                            if let Some(name) = specific_macro {
                                println!(
                                    "❌ Macro '{}' not found in the file.", name.red()
                                );
                            }
                            return Ok(());
                        }
                        let mut macro_types = HashMap::new();
                        for call in &filtered_calls {
                            let macro_type = self.classify_macro(&call.name);
                            macro_types
                                .entry(format!("{:?}", macro_type))
                                .or_insert_with(Vec::new)
                                .push(call.name.clone());
                        }
                        println!("\n📊 Expansion Summary:");
                        for (type_name, names) in &macro_types {
                            println!("{}: {}", type_name, names.len());
                        }
                        for (i, call) in filtered_calls.iter().enumerate() {
                            println!("\n🔬 Macro: {}!{}", call.name.bold(), call.args);
                            println!(
                                "   📍 Line {}, Column {}", call.line, call.column
                            );
                            if step_by_step {
                                match self.expand_macro_step_by_step(call) {
                                    Ok(steps) => {
                                        for (step_num, step) in steps.iter().enumerate() {
                                            println!(
                                                "\n   Step {} - {}:", step_num + 1, step.description.bold()
                                            );
                                            println!("   ```rust");
                                            for line in step.after.lines() {
                                                println!("   {}", line);
                                            }
                                            println!("   ```");
                                        }
                                    }
                                    Err(e) => {
                                        if verbose {
                                            println!("   ⚠️  Could not expand step by step: {}", e);
                                        }
                                    }
                                }
                            }
                            let deps = self.extract_macro_dependencies(call);
                            if !deps.is_empty() {
                                println!("\n   📚 Dependencies:");
                                for dep in &deps {
                                    println!(
                                        "{} ({})", dep.name.cyan(), dep.dependency_type
                                    );
                                }
                            }
                            if validate {
                                if let Ok(steps) = self.expand_macro_step_by_step(call) {
                                    if let Some(final_step) = steps.last() {
                                        match self.validate_macro_expansion(&final_step.after) {
                                            Ok(issues) => {
                                                if issues.is_empty() {
                                                    println!(
                                                        "\n   ✅ Validation: Expanded code compiles successfully"
                                                    );
                                                } else {
                                                    for issue in issues {
                                                        let severity_color = match issue.severity.as_str() {
                                                            "High" => issue.severity.red().bold(),
                                                            "Medium" => issue.severity.yellow().bold(),
                                                            _ => issue.severity.green().bold(),
                                                        };
                                                        println!(
                                                            "\n   🚨 Validation Issue [{}]: {}", severity_color, issue
                                                            .issue
                                                        );
                                                        println!("      💡 {}", issue.suggestion.cyan());
                                                    }
                                                }
                                            }
                                            Err(e) => {
                                                if verbose {
                                                    println!("\n   ⚠️  Validation failed: {}", e);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            if let Ok(steps) = self.expand_macro_step_by_step(call) {
                                if let Some(final_step) = steps.last() {
                                    let original_size = call.args.len();
                                    let expanded_size = final_step.after.len();
                                    let ratio = if original_size > 0 {
                                        expanded_size as f64 / original_size as f64
                                    } else {
                                        1.0
                                    };
                                    println!("\n📈 Expansion Metrics:");
                                    println!("   • Original size: {} chars", original_size);
                                    println!("   • Expanded size: {} chars", expanded_size);
                                    println!("   • Expansion ratio: {:.1}x", ratio);
                                }
                            }
                            if i < filtered_calls.len() - 1 {
                                println!("{}", "".repeat(50).blue());
                            }
                        }
                        if let Ok(expanded) = self.generate_expanded_code(input) {
                            if diff {
                                println!("\n📋 Original vs Expanded Comparison:");
                                match self
                                    .highlight_differences(
                                        &fs::read_to_string(input).unwrap_or_default(),
                                        &expanded,
                                    )
                                {
                                    Ok(diff_output) => {
                                        for line in diff_output.lines().take(20) {
                                            println!("   {}", line);
                                        }
                                        if diff_output.lines().count() > 20 {
                                            println!(
                                                "   ... (truncated - {} more lines)", diff_output.lines()
                                                .count() - 20
                                            );
                                        }
                                    }
                                    Err(e) => {
                                        if verbose {
                                            println!("   ⚠️  Could not generate diff: {}", e);
                                        }
                                    }
                                }
                            }
                            if let Err(e) = fs::write(output_file, &expanded) {
                                if verbose {
                                    println!("⚠️  Could not write to output file: {}", e);
                                }
                            } else if verbose {
                                println!(
                                    "\n💾 Expanded code written to: {}", output_file
                                );
                            }
                        }
                    }
                    Err(e) => {
                        return Err(
                            ToolError::ExecutionFailed(
                                format!("Failed to analyze macros: {}", e),
                            ),
                        );
                    }
                }
            }
            OutputFormat::Json => {
                let macro_calls = self.parse_macro_calls(input)?;
                let mut json_output = serde_json::json!(
                    { "file" : input, "macro_calls" : macro_calls.len(), "macros" :
                    macro_calls, }
                );
                if let Ok(expanded) = self.generate_expanded_code(input) {
                    json_output["expanded_code"] = expanded.into();
                }
                println!("{}", serde_json::to_string_pretty(& json_output).unwrap());
            }
            OutputFormat::Table => {
                let macro_calls = self.parse_macro_calls(input)?;
                println!(
                    "┌─ Macro Expansion Analysis ──────────────────────┐"
                );
                println!("│ File: {:<45} │", input);
                println!("│ Macros Found: {:<36} │", macro_calls.len());
                for call in macro_calls.iter().take(5) {
                    println!(
                        "│ • {:<45} │", format!("{}!{}", call.name, call.args)
                    );
                }
                if macro_calls.len() > 5 {
                    println!(
                        "│ ... and {} more {:<32} │", macro_calls.len() - 5, ""
                    );
                }
                println!(
                    "└──────────────────────────────────────────────────┘"
                );
            }
        }
        Ok(())
    }
}