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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
use anyhow::Result;
use clap::Parser;
use std::fs;
use std::path::PathBuf;
use walkdir::WalkDir;
#[derive(Parser, Debug, Clone)]
#[command(
    name = "strip",
    about = "Strip comments, blank lines, and attributes from Rust source code",
    long_about = r#"Remove comments, blank lines, attributes, and other non-essential elements from Rust source code.

MODES:
    Basic:    Remove comments and optionally blank lines
    Minify:   Single-line output where possible
    Aggressive: Maximum stripping - removes attributes, docs, and compresses whitespace

BACKUP SAFETY:
    ✅ By default, backups are created in ~/.shipwreck/strip/
    ❌ Use --no-backup to disable backups
    ⚠️ Use --force to allow overwriting the same file

EXAMPLES:
    cm strip src/main.rs                           # Basic stripping to stdout (with backup)
    cm strip src/main.rs --output main.stripped.rs # Strip to new file (with backup)
    cm strip src/ -r                              # Process directory (with backups)
    cm strip src/ -r -a                           # Aggressive stripping (with backups)
    cm strip main.rs --minify                     # Single-line output (with backup)
    cm strip src/ -r --strip-attrs --strip-docs   # Remove specific elements (with backups)
    cm strip src/ -r --no-backup                  # Process without backups (dangerous!)"#
)]
pub struct StripArgs {
    pub input: PathBuf,
    #[arg(short, long)]
    pub output: Option<PathBuf>,
    #[arg(long)]
    pub target: Option<PathBuf>,
    #[arg(short = 'b', long)]
    pub remove_blanks: bool,
    #[arg(short, long)]
    pub recursive: bool,
    #[arg(long)]
    pub force: bool,
    #[arg(long)]
    pub no_backup: bool,
    #[arg(long)]
    pub src: bool,
    #[arg(long, default_value = "10")]
    pub max_depth: usize,
    #[arg(long, short = 'a')]
    pub aggressive: bool,
    #[arg(long)]
    pub minify: bool,
    #[arg(long, short = 't')]
    pub tease: bool,
    #[arg(long)]
    pub strip_attrs: bool,
    #[arg(long)]
    pub strip_docs: bool,
    #[arg(long)]
    pub inline_uses: bool,
    #[arg(short = 'v', long)]
    pub verbose: bool,
}
pub fn handle_strip_command(args: StripArgs) -> Result<()> {
    show_active_options(&args);
    let input_path = determine_input_path(&args)?;
    if !input_path.exists() {
        return Err(
            anyhow::anyhow!("Input path does not exist: {}", input_path.display()),
        );
    }
    // SAFETY: Validate that we're only processing Rust files
    // This prevents accidental processing of Cargo.toml, .json, or other files
    if !input_path.is_dir() {
        validate_rust_file(&input_path)?;
    }

    // Extra safety check: warn about critical files in directory
    if input_path.is_dir() {
        println!("⚠️  Processing directory: {}", input_path.display());
        println!("   Only .rs files will be processed, all other files are skipped");
    }

    let backup_dir = create_backup_directory()?;
    if args.recursive || input_path.is_dir() {
        process_directory(&input_path, &args, &backup_dir)?;
    } else {
        process_single_file(&input_path, &args, args.output.as_ref(), &backup_dir)?;
    }
    Ok(())
}
fn show_active_options(args: &StripArgs) {
    let mut options = Vec::new();
    if args.tease {
        options.push("🌶️ TEASE mode (remove all comments + blanks)");
    } else if args.aggressive {
        options.push("🔥 Aggressive mode");
    } else {
        if args.remove_blanks {
            options.push("📝 Remove blank lines");
        }
        if args.strip_attrs {
            options.push("🏷️  Strip attributes");
        }
        if args.strip_docs {
            options.push("📖 Strip doc comments");
        }
        if args.minify {
            options.push("🎯 Minify output");
        }
        if args.inline_uses {
            options.push("🔗 Inline use statements");
        }
    }
    if args.no_backup {
        options.push("❌ No backups");
    } else {
        options.push("💾 Auto-backup (default)");
    }
    if args.force {
        options.push("⚠️  Force overwrite");
    }
    if args.recursive {
        options.push("📁 Recursive");
    }
    if !options.is_empty() {
        println!("🚀 Active options:");
        for option in options {
            println!("   {}", option);
        }
        println!();
    }
}
fn determine_input_path(args: &StripArgs) -> Result<PathBuf> {
    if args.src {
        Ok(PathBuf::from("src"))
    } else if let Some(target) = &args.target {
        Ok(target.clone())
    } else {
        Ok(args.input.clone())
    }
}
fn validate_rust_file(path: &PathBuf) -> Result<()> {
    let extension = path.extension().and_then(|s| s.to_str());
    if extension != Some("rs") {
        return Err(anyhow::anyhow!(
            "❌ Can only strip Rust (.rs) files. Got: {:?}\n   File: {}",
            extension.unwrap_or("no extension"),
            path.display()
        ));
    }
    Ok(())
}

fn create_backup_directory() -> Result<PathBuf> {
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    let backup_dir = PathBuf::from(home).join(".shipwreck").join("strip");
    if !backup_dir.exists() {
        fs::create_dir_all(&backup_dir)?;
        println!("📁 Created backup directory: {}", backup_dir.display());
    }
    Ok(backup_dir)
}
fn create_backup(original_path: &PathBuf, backup_dir: &PathBuf) -> Result<PathBuf> {
    let timestamp = chrono::Utc::now().format("%Y%m%d_%H%M%S");
    let file_name = original_path
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("unknown");
    let backup_name = format!("{}_{}.backup", file_name, timestamp);
    let backup_path = backup_dir.join(backup_name);
    fs::copy(original_path, &backup_path)?;
    println!("🔄 Backup created: {}", backup_path.display());
    Ok(backup_path)
}
fn strip_rust(source: &str, args: &StripArgs) -> Result<String> {
    let source_to_parse = if args.tease {
        strip_all_comments_manual(source)
    } else {
        source.to_string()
    };
    let mut syntax_tree = syn::parse_file(&source_to_parse)?;
    if args.aggressive || args.strip_attrs {
        strip_attributes(&mut syntax_tree);
    }
    if args.aggressive || args.strip_docs {
        strip_doc_comments(&mut syntax_tree);
    }
    if args.aggressive || args.inline_uses {
        inline_use_statements(&mut syntax_tree);
    }
    let mut output = if args.minify || args.aggressive {
        prettyplease::unparse(&syntax_tree)
            .split_whitespace()
            .collect::<Vec<_>>()
            .join(" ")
    } else {
        prettyplease::unparse(&syntax_tree)
    };
    if args.aggressive {
        output = output
            .replace(" ;", ";")
            .replace(" ,", ",")
            .replace(" :", ":")
            .replace(" {", "{")
            .replace("{ ", "{")
            .replace(" }", "}")
            .replace("} ", "}")
            .replace(" (", "(")
            .replace("( ", "(")
            .replace(" )", ")")
            .replace(") ", ")")
            .replace(" ->", "->")
            .replace("-> ", "->");
    }
    if args.tease || args.remove_blanks {
        let lines: Vec<String> = output.lines().map(|line| line.to_string()).collect();
        let mut filtered_lines = Vec::new();
        let mut prev_empty = false;
        for line in lines {
            let is_empty = line.trim().is_empty();
            if !is_empty || !prev_empty {
                filtered_lines.push(line);
            }
            prev_empty = is_empty;
        }
        output = filtered_lines.join("\n");
    }
    Ok(output)
}
fn strip_attributes(syntax_tree: &mut syn::File) {
    use syn::visit_mut::{self, VisitMut};
    struct AttrStripper;
    impl VisitMut for AttrStripper {
        fn visit_item_mut(&mut self, item: &mut syn::Item) {
            match item {
                syn::Item::Fn(f) => {
                    f.attrs
                        .retain(|attr| {
                            attr.path().is_ident("test") || attr.path().is_ident("cfg")
                        });
                }
                _ => {
                    if let Some(attrs) = get_attrs_mut(item) {
                        attrs.clear();
                    }
                }
            }
            visit_mut::visit_item_mut(self, item);
        }
    }
    AttrStripper.visit_file_mut(syntax_tree);
}
fn strip_doc_comments(syntax_tree: &mut syn::File) {
    use syn::visit_mut::{self, VisitMut};
    struct DocStripper;
    impl VisitMut for DocStripper {
        fn visit_item_mut(&mut self, item: &mut syn::Item) {
            match item {
                syn::Item::Fn(f) => {
                    f.attrs.retain(|attr| !is_doc_attr(attr));
                }
                syn::Item::Struct(s) => {
                    s.attrs.retain(|attr| !is_doc_attr(attr));
                }
                syn::Item::Enum(e) => {
                    e.attrs.retain(|attr| !is_doc_attr(attr));
                }
                syn::Item::Trait(t) => {
                    t.attrs.retain(|attr| !is_doc_attr(attr));
                }
                syn::Item::Impl(i) => {
                    i.attrs.retain(|attr| !is_doc_attr(attr));
                }
                syn::Item::Mod(m) => {
                    m.attrs.retain(|attr| !is_doc_attr(attr));
                }
                syn::Item::Type(t) => {
                    t.attrs.retain(|attr| !is_doc_attr(attr));
                }
                syn::Item::Const(c) => {
                    c.attrs.retain(|attr| !is_doc_attr(attr));
                }
                syn::Item::Static(s) => {
                    s.attrs.retain(|attr| !is_doc_attr(attr));
                }
                _ => {
                    if let Some(attrs) = get_attrs_mut(item) {
                        attrs.retain(|attr| !is_doc_attr(attr));
                    }
                }
            }
            visit_mut::visit_item_mut(self, item);
        }
    }
    DocStripper.visit_file_mut(syntax_tree);
}
fn inline_use_statements(_syntax_tree: &mut syn::File) {}
fn strip_all_comments_manual(source: &str) -> String {
    let mut result = String::new();
    let mut chars = source.chars().peekable();
    let mut in_string = false;
    let mut in_char = false;
    let mut escape_next = false;
    while let Some(ch) = chars.next() {
        if escape_next {
            result.push(ch);
            escape_next = false;
            continue;
        }
        if ch == '\\' && (in_string || in_char) {
            result.push(ch);
            escape_next = true;
            continue;
        }
        if ch == '"' && !in_char {
            in_string = !in_string;
            result.push(ch);
            continue;
        }
        if ch == '\'' && !in_string {
            in_char = !in_char;
            result.push(ch);
            continue;
        }
        if !in_string && !in_char {
            if ch == '/' {
                if let Some(&next_ch) = chars.peek() {
                    if next_ch == '/' {
                        chars.next();
                        while let Some(comment_ch) = chars.next() {
                            if comment_ch == '\n' {
                                result.push('\n');
                                break;
                            }
                        }
                        continue;
                    } else if next_ch == '*' {
                        chars.next();
                        let mut prev_ch = ' ';
                        while let Some(comment_ch) = chars.next() {
                            if prev_ch == '*' && comment_ch == '/' {
                                break;
                            }
                            prev_ch = comment_ch;
                        }
                        result.push(' ');
                        continue;
                    }
                }
            }
        }
        result.push(ch);
    }
    result
}
fn is_doc_attr(attr: &syn::Attribute) -> bool {
    attr.path().is_ident("doc")
}
fn get_attrs_mut(item: &mut syn::Item) -> Option<&mut Vec<syn::Attribute>> {
    match item {
        syn::Item::Const(item) => Some(&mut item.attrs),
        syn::Item::Enum(item) => Some(&mut item.attrs),
        syn::Item::ExternCrate(item) => Some(&mut item.attrs),
        syn::Item::Fn(item) => Some(&mut item.attrs),
        syn::Item::ForeignMod(item) => Some(&mut item.attrs),
        syn::Item::Impl(item) => Some(&mut item.attrs),
        syn::Item::Macro(item) => Some(&mut item.attrs),
        syn::Item::Mod(item) => Some(&mut item.attrs),
        syn::Item::Static(item) => Some(&mut item.attrs),
        syn::Item::Struct(item) => Some(&mut item.attrs),
        syn::Item::Trait(item) => Some(&mut item.attrs),
        syn::Item::TraitAlias(item) => Some(&mut item.attrs),
        syn::Item::Type(item) => Some(&mut item.attrs),
        syn::Item::Union(item) => Some(&mut item.attrs),
        syn::Item::Use(item) => Some(&mut item.attrs),
        syn::Item::Verbatim(_) => None,
        _ => None,
    }
}
fn process_single_file(
    input_path: &PathBuf,
    args: &StripArgs,
    output_path: Option<&PathBuf>,
    backup_dir: &PathBuf,
) -> Result<()> {
    println!("📝 Processing single file: {}", input_path.display());
    let original_content = fs::read_to_string(input_path)?;
    if !args.no_backup {
        create_backup(input_path, backup_dir)?;
    }
    let stripped_content = strip_rust(&original_content, args)?;
    let final_output_path = if let Some(output) = output_path {
        output.clone()
    } else {
        let file_stem = input_path
            .file_stem()
            .and_then(|s| s.to_str())
            .unwrap_or("unknown");
        let extension = input_path.extension().and_then(|s| s.to_str()).unwrap_or("rs");
        input_path.with_file_name(format!("{}.stripped.{}", file_stem, extension))
    };
    if final_output_path == *input_path && !args.force {
        println!(
            "⚠️  Output path is same as input. Use --force to overwrite or specify different output path."
        );
        println!("   Suggested: --output {}", final_output_path.display());
        return Ok(());
    }
    fs::write(&final_output_path, stripped_content)?;
    if final_output_path != *input_path {
        println!("✅ Stripped code written to: {}", final_output_path.display());
    } else {
        println!("✅ File overwritten: {}", input_path.display());
    }
    let original_lines = original_content.lines().count();
    let stripped_lines = fs::read_to_string(&final_output_path)?.lines().count();
    let reduction = if original_lines > 0 && stripped_lines <= original_lines {
        ((original_lines - stripped_lines) as f64 / original_lines as f64 * 100.0) as i32
    } else if original_lines > 0 && stripped_lines > original_lines {
        0
    } else {
        0
    };
    println!(
        "📊 Lines: {}{} ({}% reduction)", original_lines, stripped_lines,
        reduction
    );
    Ok(())
}
fn process_directory(
    dir: &PathBuf,
    args: &StripArgs,
    backup_dir: &PathBuf,
) -> Result<()> {
    println!(
        "📁 Processing directory: {} (max depth: {})", dir.display(), args.max_depth
    );
    let output_base = if let Some(output) = &args.output {
        if !output.exists() {
            fs::create_dir_all(output)?;
            println!("📁 Created output directory: {}", output.display());
        }
        Some(output.clone())
    } else {
        None
    };
    let mut processed_count = 0;
    let mut skipped_count = 0;
    let mut error_count = 0;
    let walker = WalkDir::new(dir)
        .max_depth(args.max_depth)
        .into_iter()
        .filter_map(|e| e.ok());
    for entry in walker {
        let path = entry.path();
        let extension = path.extension().and_then(|s| s.to_str());

        // SAFETY: Explicitly check for and skip important project files
        if let Some(file_name) = path.file_name().and_then(|n| n.to_str()) {
            if file_name == "Cargo.toml" || file_name == "Cargo.lock"
                || file_name.ends_with(".json") || file_name.ends_with(".toml") {
                if args.verbose {
                    println!("⏭️  Skipping protected file: {}", path.display());
                }
                skipped_count += 1;
                continue;
            }
        }

        if extension == Some("rs") {
            let output_path = if let Some(ref output_base) = output_base {
                let relative = path.strip_prefix(dir).unwrap_or(path);
                let output_file = output_base.join(relative);
                if let Some(parent) = output_file.parent() {
                    fs::create_dir_all(parent)?;
                }
                Some(output_file)
            } else {
                None
            };
            match process_single_file(
                &path.to_path_buf(),
                args,
                output_path.as_ref(),
                backup_dir,
            ) {
                Ok(_) => {
                    processed_count += 1;
                }
                Err(e) => {
                    println!("❌ Error processing {}: {}", path.display(), e);
                    error_count += 1;
                }
            }
        } else if path.is_dir() {
            continue;
        } else {
            if args.verbose {
                println!("⏭️  Skipping non-Rust file: {}", path.display());
            }
            skipped_count += 1;
        }
    }
    println!("📊 Directory processing complete:");
    println!("   ✅ Files processed: {}", processed_count);
    if error_count > 0 {
        println!("   ❌ Errors: {}", error_count);
    }
    if skipped_count > 0 {
        println!("   ⏭️  Files skipped: {}", skipped_count);
    }
    Ok(())
}
#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::path::PathBuf;
    use tempfile::TempDir;
    #[test]
    fn test_strip_comments() {
        let source = r#"
// This is a comment
fn main() {
    // Another comment
    println!("Hello"); // Inline comment
    /* Block comment */
}
"#;
        let args = StripArgs {
            input: PathBuf::from("test.rs"),
            output: None,
            target: None,
            remove_blanks: false,
            recursive: false,
            force: false,
            no_backup: false,
            src: false,
            max_depth: 10,
            aggressive: false,
            minify: false,
            tease: false,
            strip_attrs: false,
            strip_docs: false,
            inline_uses: false,
            verbose: false,
        };
        let result = strip_rust(source, &args).unwrap();
        assert!(! result.contains("//"));
        assert!(! result.contains("/*"));
        assert!(result.contains("fn main()"));
        assert!(result.contains("println!"));
    }
    #[test]
    fn test_strip_blank_lines() {
        let source = r#"fn main() {

    println!("Hello");

}
"#;
        let args = StripArgs {
            input: PathBuf::from("test.rs"),
            output: None,
            target: None,
            remove_blanks: true,
            recursive: false,
            force: false,
            no_backup: false,
            src: false,
            max_depth: 10,
            aggressive: false,
            minify: false,
            tease: false,
            strip_attrs: false,
            strip_docs: false,
            inline_uses: false,
            verbose: false,
        };
        let result = strip_rust(source, &args).unwrap();
        let lines: Vec<&str> = result.lines().collect();
        let consecutive_empty = lines
            .windows(2)
            .any(|window| window[0].trim().is_empty() && window[1].trim().is_empty());
        assert!(! consecutive_empty);
    }
    #[test]
    fn test_aggressive_stripping() {
        let source = r#"
/// This is a doc comment
#[derive(Debug)]
fn main() {
    println!("Hello");
}
"#;
        let args = StripArgs {
            input: PathBuf::from("test.rs"),
            output: None,
            target: None,
            remove_blanks: false,
            recursive: false,
            force: false,
            no_backup: false,
            src: false,
            max_depth: 10,
            aggressive: true,
            minify: false,
            tease: false,
            strip_attrs: false,
            strip_docs: false,
            inline_uses: false,
            verbose: false,
        };
        let result = strip_rust(source, &args).unwrap();
        assert!(! result.contains("///"));
        assert!(! result.contains("#[derive(Debug)]"));
        assert!(result.contains("fn main()"));
    }
    #[test]
    fn test_determine_input_path() {
        let mut args = StripArgs {
            input: PathBuf::from("test.rs"),
            output: None,
            target: None,
            remove_blanks: false,
            recursive: false,
            force: false,
            no_backup: false,
            src: false,
            max_depth: 10,
            aggressive: false,
            minify: false,
            tease: false,
            strip_attrs: false,
            strip_docs: false,
            inline_uses: false,
            verbose: false,
        };
        assert_eq!(determine_input_path(& args).unwrap(), PathBuf::from("test.rs"));
        args.src = true;
        assert_eq!(determine_input_path(& args).unwrap(), PathBuf::from("src"));
        args.src = false;
        args.target = Some(PathBuf::from("target/dir"));
        assert_eq!(determine_input_path(& args).unwrap(), PathBuf::from("target/dir"));
    }
    #[test]
    fn test_backup_creation() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test.rs");
        let backup_dir = temp_dir.path().join("backups");
        fs::create_dir_all(&backup_dir).unwrap();
        fs::write(&test_file, "fn main() {}").unwrap();
        let backup_path = create_backup(&test_file, &backup_dir).unwrap();
        assert!(backup_path.exists());
        assert!(backup_path.to_string_lossy().contains("test.rs"));
        assert!(backup_path.to_string_lossy().contains(".backup"));
    }
}