cargo-quality 0.2.0

Professional Rust code quality analysis tool with hardcoded standards
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT

//! Cargo quality analysis tool for Rust code.
//!
//! This binary provides a command-line interface for analyzing and improving
//! Rust code quality according to project standards. It detects common issues
//! and suggests or applies fixes automatically.
//!
//! # Available Commands
//!
//! - `cargo qual check` - Analyze code without modifications
//! - `cargo qual fix` - Apply automatic fixes
//! - `cargo qual format` - Format code according to quality rules
//!
//! # Examples
//!
//! ```bash
//! cargo qual check src/
//! cargo qual fix --dry-run src/
//! cargo qual format .
//! ```

use std::{fs, path::PathBuf};

use masterror::AppResult;

use crate::{
    analyzer::{AnalysisResult, Fix, Issue},
    analyzers::get_analyzers,
    cli::{Command, QualityArgs, Shell},
    differ::{DiffResult, generate_diff, show_full, show_interactive, show_summary},
    error::{IoError, ParseError},
    file_utils::collect_rust_files,
    mod_rs::{ModRsResult, find_mod_rs_issues, fix_all_mod_rs},
    report::{GlobalReport, Report}
};

mod analyzer;
mod analyzers;
mod cli;
mod differ;
mod error;
mod file_utils;
mod formatter;
mod help;
mod mod_rs;
mod report;

fn main() -> AppResult<()> {
    let args = QualityArgs::parse_args();

    match args.command {
        Command::Check {
            path,
            verbose,
            analyzer,
            color
        } => check_quality(&path, verbose, analyzer.as_deref(), color)?,
        Command::Fix {
            path,
            dry_run,
            analyzer
        } => fix_quality(&path, dry_run, analyzer.as_deref())?,
        Command::Format {
            path
        } => format_quality(&path)?,
        Command::Fmt {
            path: _
        } => formatter::format_code()?,
        Command::Diff {
            path,
            summary,
            interactive,
            analyzer,
            color
        } => run_diff(&path, summary, interactive, analyzer.as_deref(), color)?,
        Command::Help => {
            help::display_help();
            return Ok(());
        }
        Command::Completions {
            shell
        } => {
            generate_completions(shell);
            return Ok(());
        }
        Command::Setup => {
            setup_completions()?;
            return Ok(());
        }
        Command::ModRs {
            path,
            fix
        } => run_mod_rs(&path, fix)?
    }

    Ok(())
}

/// Generate shell completions.
///
/// Outputs completion script for the specified shell to stdout.
///
/// # Arguments
///
/// * `shell` - Target shell for completion generation
fn generate_completions(shell: Shell) {
    use clap::CommandFactory;
    use clap_complete::{Shell as CompShell, generate};

    let mut cmd = QualityArgs::command();
    let bin_name = "cargo-qual";

    let comp_shell = match shell {
        Shell::Bash => CompShell::Bash,
        Shell::Fish => CompShell::Fish,
        Shell::Zsh => CompShell::Zsh,
        Shell::PowerShell => CompShell::PowerShell,
        Shell::Elvish => CompShell::Elvish
    };

    generate(comp_shell, &mut cmd, bin_name, &mut std::io::stdout());
}

/// Setup shell completions automatically.
///
/// Detects current shell and installs completions to standard location.
///
/// # Returns
///
/// `AppResult<()>` - Ok if setup succeeds
fn setup_completions() -> AppResult<()> {
    let shell_name = detect_shell();

    let Some((shell, comp_dir, file_name)) = get_completion_config(&shell_name) else {
        println!("❌ Unsupported shell: {}", shell_name);
        println!("Supported shells: bash, fish, zsh");
        println!("\nManual installation:");
        println!("  cargo qual completions <shell> > <completion-file>");
        return Ok(());
    };

    fs::create_dir_all(&comp_dir).map_err(IoError::from)?;
    let comp_file = comp_dir.join(file_name);

    if shell_name == "fish" {
        install_fish_completions(&comp_file)?;
    } else {
        install_generated_completions(shell, &comp_file)?;
    }

    println!(
        "{} completions installed to: {}",
        shell_name,
        comp_file.display()
    );
    println!(
        "\nCompletions will be available in new {} sessions",
        shell_name
    );
    println!("Or run: source {}", comp_file.display());

    Ok(())
}

/// Detects current shell from SHELL environment variable.
///
/// # Returns
///
/// Shell name (e.g., "bash", "fish", "zsh")
#[inline]
fn detect_shell() -> String {
    use std::env;

    let shell_path = env::var("SHELL").unwrap_or_else(|_| String::from("/bin/sh"));
    let shell_path_buf = PathBuf::from(&shell_path);
    shell_path_buf
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or("sh")
        .to_string()
}

/// Gets HOME directory path.
///
/// # Returns
///
/// Home directory path or "~" if not found
#[inline]
fn get_home_dir() -> String {
    use std::env;

    env::var("HOME").unwrap_or_else(|_| String::from("~"))
}

/// Gets XDG_CONFIG_HOME directory.
///
/// Falls back to ~/.config if not set.
///
/// # Returns
///
/// Config directory path
#[inline]
fn get_xdg_config_home() -> PathBuf {
    use std::env;

    env::var("XDG_CONFIG_HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|_| PathBuf::from(get_home_dir()).join(".config"))
}

/// Gets XDG_DATA_HOME directory.
///
/// Falls back to ~/.local/share if not set.
///
/// # Returns
///
/// Data directory path
#[inline]
fn get_xdg_data_home() -> PathBuf {
    use std::env;

    env::var("XDG_DATA_HOME")
        .map(PathBuf::from)
        .unwrap_or_else(|_| PathBuf::from(get_home_dir()).join(".local").join("share"))
}

/// Gets completion configuration for a shell.
///
/// Returns None for unsupported shells.
///
/// # Arguments
///
/// * `shell_name` - Shell name (e.g., "bash", "fish", "zsh")
///
/// # Returns
///
/// Option<(Shell, PathBuf, &'static str)> - Shell type, directory, filename
fn get_completion_config(shell_name: &str) -> Option<(Shell, PathBuf, &'static str)> {
    match shell_name {
        "fish" => {
            let dir = get_xdg_config_home().join("fish").join("completions");
            Some((Shell::Fish, dir, "cargo.fish"))
        }
        "bash" => {
            let dir = get_xdg_data_home()
                .join("bash-completion")
                .join("completions");
            Some((Shell::Bash, dir, "cargo-quality"))
        }
        "zsh" => {
            let dir = get_xdg_data_home().join("zsh").join("site-functions");
            Some((Shell::Zsh, dir, "_cargo-quality"))
        }
        _ => None
    }
}

/// Installs fish shell completions.
///
/// Uses hardcoded fish completion script.
///
/// # Arguments
///
/// * `comp_file` - Completion file path
///
/// # Returns
///
/// `AppResult<()>` - Ok if installation succeeds
fn install_fish_completions(comp_file: &std::path::Path) -> AppResult<()> {
    let fish_completions = r#"# Completion for cargo qual subcommand
complete -c cargo -n "__fish_seen_subcommand_from quality" -s h -l help -d 'Print help'
complete -c cargo -n "__fish_seen_subcommand_from quality" -f -a "check" -d 'Check code quality'
complete -c cargo -n "__fish_seen_subcommand_from quality" -f -a "fix" -d 'Fix quality issues'
complete -c cargo -n "__fish_seen_subcommand_from quality" -f -a "format" -d 'Format code'
complete -c cargo -n "__fish_seen_subcommand_from quality" -f -a "fmt" -d 'Run cargo +nightly fmt'
complete -c cargo -n "__fish_seen_subcommand_from quality" -f -a "diff" -d 'Show proposed changes'
complete -c cargo -n "__fish_seen_subcommand_from quality" -f -a "help" -d 'Display help'
complete -c cargo -n "__fish_seen_subcommand_from quality" -f -a "completions" -d 'Generate completions'
complete -c cargo -n "__fish_seen_subcommand_from quality" -f -a "setup" -d 'Setup completions'

# Diff options
complete -c cargo -n "__fish_seen_subcommand_from quality; and __fish_seen_subcommand_from diff" -s s -l summary -d 'Brief summary'
complete -c cargo -n "__fish_seen_subcommand_from quality; and __fish_seen_subcommand_from diff" -s i -l interactive -d 'Interactive mode'

# Check options
complete -c cargo -n "__fish_seen_subcommand_from quality; and __fish_seen_subcommand_from check" -s v -l verbose -d 'Detailed output'

# Fix options
complete -c cargo -n "__fish_seen_subcommand_from quality; and __fish_seen_subcommand_from fix" -s d -l dry-run -d 'Dry run'

# Completions options
complete -c cargo -n "__fish_seen_subcommand_from quality; and __fish_seen_subcommand_from completions" -f -a "bash fish zsh powershell elvish"
"#;
    fs::write(comp_file, fish_completions).map_err(IoError::from)?;
    Ok(())
}

/// Installs generated completions for bash/zsh.
///
/// Uses clap_complete to generate shell-specific completions.
///
/// # Arguments
///
/// * `shell` - Shell type
/// * `comp_file` - Completion file path
///
/// # Returns
///
/// `AppResult<()>` - Ok if installation succeeds
fn install_generated_completions(shell: Shell, comp_file: &std::path::Path) -> AppResult<()> {
    use clap::CommandFactory;
    use clap_complete::{Shell as CompShell, generate};

    let mut cmd = QualityArgs::command();
    let comp_shell = match shell {
        Shell::Bash => CompShell::Bash,
        Shell::Zsh => CompShell::Zsh,
        _ => unreachable!()
    };

    let mut file = fs::File::create(comp_file).map_err(IoError::from)?;
    generate(comp_shell, &mut cmd, "cargo-qual", &mut file);
    Ok(())
}

/// Run mod.rs analyzer.
///
/// Finds mod.rs files and optionally fixes them.
///
/// # Arguments
///
/// * `path` - Path to analyze
/// * `fix` - Apply fixes automatically
fn run_mod_rs(path: &str, fix: bool) -> AppResult<()> {
    let result = find_mod_rs_issues(path)?;

    if result.is_empty() {
        println!("No mod.rs files found");
        return Ok(());
    }

    if fix {
        let fixed = fix_all_mod_rs(path)?;
        println!("Fixed {} mod.rs files", fixed);
    } else {
        println!("Found {} mod.rs files:", result.len());
        for issue in &result.issues {
            println!(
                "  {} -> {}",
                issue.path.display(),
                issue.suggested.display()
            );
        }
        println!("\nRun with --fix to apply changes");
    }

    Ok(())
}

/// Check code quality without modifying files.
///
/// Analyzes all Rust files in the specified path and reports issues found
/// by each analyzer or a specific analyzer if provided. Prints detailed
/// reports for files with issues.
///
/// # Arguments
///
/// * `path` - File or directory path to analyze
/// * `verbose` - Print confirmation for files without issues
/// * `analyzer_name` - Optional analyzer name to run (e.g., "inline_comments")
///
/// # Returns
///
/// `AppResult<()>` - Ok if analysis completes, error on IO or parse failures
///
/// # Examples
///
/// ```no_run
/// use cargo_quality::check_quality;
/// check_quality("src/", true, None, false).unwrap();
/// check_quality("src/", false, Some("inline_comments"), true).unwrap();
/// ```
fn check_quality(
    path: &str,
    verbose: bool,
    analyzer_name: Option<&str>,
    color: bool
) -> AppResult<()> {
    let files = collect_rust_files(path)?;
    let all_analyzers = get_analyzers();

    let analyzers: Vec<_> = if let Some(name) = analyzer_name {
        all_analyzers
            .into_iter()
            .filter(|a| a.name() == name)
            .collect()
    } else {
        all_analyzers
    };

    if let Some(name) = analyzer_name
        && analyzers.is_empty()
        && name != "mod_rs"
    {
        eprintln!("Unknown analyzer: {}. Available analyzers:", name);
        for analyzer in get_analyzers() {
            eprintln!("  - {}", analyzer.name());
        }
        eprintln!("  - mod_rs");
        return Ok(());
    }

    let mut global_report = GlobalReport::new();

    let should_check_mod_rs = analyzer_name.is_none() || analyzer_name == Some("mod_rs");
    if should_check_mod_rs {
        let mod_rs_result = find_mod_rs_issues(path)?;
        if !mod_rs_result.is_empty() {
            add_mod_rs_to_report(&mod_rs_result, &mut global_report);
        }
    }

    if analyzer_name != Some("mod_rs") {
        for file_path in files {
            let content = fs::read_to_string(&file_path).map_err(IoError::from)?;
            let ast = syn::parse_file(&content).map_err(ParseError::from)?;

            let mut report = Report::new(file_path.display().to_string());

            for analyzer in &analyzers {
                let result = analyzer.analyze(&ast, &content)?;
                report.add_result(analyzer.name().to_string(), result);
            }

            if report.total_issues() > 0 || verbose {
                global_report.add_report(report);
            }
        }
    }

    if global_report.total_issues() > 0 {
        if let Some(analyzer) = analyzer_name {
            print!("{}", global_report.display_analyzer(analyzer, color));
        } else if verbose {
            print!("{}", global_report.display_verbose(color));
        } else {
            print!("{}", global_report.display_compact(color));
        }
    } else {
        print!("{}", global_report.display_compact(color));
    }

    Ok(())
}

/// Adds mod.rs issues to the global report.
///
/// Converts ModRsResult into Report format for unified display.
///
/// # Arguments
///
/// * `mod_rs_result` - Result from mod_rs analysis
/// * `global_report` - Global report to add issues to
fn add_mod_rs_to_report(mod_rs_result: &ModRsResult, global_report: &mut GlobalReport) {
    for issue in &mod_rs_result.issues {
        let mut report = Report::new(issue.path.display().to_string());

        let analysis_result = AnalysisResult {
            issues:        vec![Issue {
                line:    issue.line,
                column:  issue.column,
                message: issue.message.clone(),
                fix:     Fix::Simple(issue.suggested.display().to_string())
            }],
            fixable_count: 1
        };

        report.add_result("mod_rs".to_string(), analysis_result);
        global_report.add_report(report);
    }
}

/// Fix quality issues automatically.
///
/// Applies automatic fixes from all analyzers or a specific analyzer to Rust
/// files in the specified path. Can run in dry-run mode to preview changes
/// without modifying files.
///
/// # Arguments
///
/// * `path` - File or directory path to fix
/// * `dry_run` - If true, report fixes but do not modify files
/// * `analyzer_name` - Optional analyzer name to run (e.g., "path_import")
///
/// # Returns
///
/// `AppResult<()>` - Ok if fixes applied successfully, error on IO or parse
/// failures
///
/// # Examples
///
/// ```no_run
/// use cargo_quality::fix_quality;
/// fix_quality("src/", true, None).unwrap();
/// fix_quality("src/", false, Some("path_import")).unwrap();
/// ```
fn fix_quality(path: &str, dry_run: bool, analyzer_name: Option<&str>) -> AppResult<()> {
    let all_analyzers = get_analyzers();

    let analyzers: Vec<_> = if let Some(name) = analyzer_name {
        all_analyzers
            .into_iter()
            .filter(|a| a.name() == name)
            .collect()
    } else {
        all_analyzers
    };

    if let Some(name) = analyzer_name
        && analyzers.is_empty()
        && name != "mod_rs"
    {
        eprintln!("Unknown analyzer: {}. Available analyzers:", name);
        for analyzer in get_analyzers() {
            eprintln!("  - {}", analyzer.name());
        }
        eprintln!("  - mod_rs");
        return Ok(());
    }

    let should_fix_mod_rs = analyzer_name.is_none() || analyzer_name == Some("mod_rs");
    if should_fix_mod_rs {
        let mod_rs_result = find_mod_rs_issues(path)?;
        if !mod_rs_result.is_empty() {
            if dry_run {
                for issue in &mod_rs_result.issues {
                    println!(
                        "Would fix: {} -> {}",
                        issue.path.display(),
                        issue.suggested.display()
                    );
                }
            } else {
                let fixed = fix_all_mod_rs(path)?;
                if fixed > 0 {
                    println!("Fixed {} mod.rs files", fixed);
                }
            }
        }
    }

    if analyzer_name != Some("mod_rs") {
        let files = collect_rust_files(path)?;
        for file_path in files {
            let content = fs::read_to_string(&file_path).map_err(IoError::from)?;
            let mut ast = syn::parse_file(&content).map_err(ParseError::from)?;

            let mut total_fixed = 0;

            for analyzer in &analyzers {
                let fixed = analyzer.fix(&mut ast)?;
                total_fixed += fixed;
            }

            if total_fixed > 0 {
                println!("Fixed {} issues in {}", total_fixed, file_path.display());

                if !dry_run {
                    let formatted = prettyplease::unparse(&ast);
                    fs::write(&file_path, formatted).map_err(IoError::from)?;
                }
            }
        }
    }

    Ok(())
}

/// Format code according to quality rules.
///
/// Wrapper around `fix_quality` that applies all fixes without dry-run mode.
///
/// # Arguments
///
/// * `path` - File or directory path to format
///
/// # Returns
///
/// `AppResult<()>` - Ok if formatting succeeds, error otherwise
fn format_quality(path: &str) -> AppResult<()> {
    fix_quality(path, false, None)
}

/// Show diff of proposed quality fixes.
///
/// Displays changes that would be made by quality analyzers. Supports three
/// modes:
/// - Full: Complete unified diff output
/// - Summary: Brief statistics by file and analyzer
/// - Interactive: User selects which changes to apply
///
/// # Arguments
///
/// * `path` - File or directory path to analyze
/// * `summary` - Show brief summary instead of full diff
/// * `interactive` - Enable interactive mode for selecting changes
/// * `analyzer_name` - Optional analyzer name to run (e.g., "path_import")
///
/// # Returns
///
/// `AppResult<()>` - Ok if diff generated successfully, error otherwise
///
/// # Examples
///
/// ```no_run
/// use cargo_quality::run_diff;
/// run_diff("src/", false, false, None, false).unwrap();
/// run_diff("src/", true, false, Some("path_import"), false).unwrap();
/// ```
fn run_diff(
    path: &str,
    summary: bool,
    interactive: bool,
    analyzer_name: Option<&str>,
    color: bool
) -> AppResult<()> {
    let files = collect_rust_files(path)?;
    let all_analyzers = get_analyzers();

    let analyzers: Vec<_> = if let Some(name) = analyzer_name {
        all_analyzers
            .into_iter()
            .filter(|a| a.name() == name)
            .collect()
    } else {
        all_analyzers
    };

    if let Some(name) = analyzer_name
        && analyzers.is_empty()
    {
        eprintln!("Unknown analyzer: {}. Available analyzers:", name);
        for analyzer in get_analyzers() {
            eprintln!("  - {}", analyzer.name());
        }
        return Ok(());
    }

    let mut result = DiffResult::new();

    for file_path in files {
        let file_diff = generate_diff(file_path.to_str().unwrap_or(""), &analyzers)?;
        result.add_file(file_diff);
    }

    if result.total_changes() == 0 {
        println!("No changes proposed");
        return Ok(());
    }

    if summary {
        show_summary(&result, color);
    } else if interactive {
        let _selected = show_interactive(&result, color)?;
    } else {
        show_full(&result, color);
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::fs;

    use tempfile::TempDir;

    use super::*;

    #[test]
    fn test_check_quality() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.rs");
        fs::write(
            &file_path,
            "fn main() { let x = std::fs::read_to_string(\"f\"); }"
        )
        .unwrap();

        let result = check_quality(temp_dir.path().to_str().unwrap(), false, None, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_check_quality_verbose() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("clean.rs");
        fs::write(&file_path, "fn main() {}").unwrap();

        let result = check_quality(temp_dir.path().to_str().unwrap(), true, None, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_fix_quality_dry_run() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.rs");
        fs::write(&file_path, "fn main() {}").unwrap();

        let result = fix_quality(temp_dir.path().to_str().unwrap(), true, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_format_quality() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.rs");
        fs::write(&file_path, "fn main() {}").unwrap();

        let result = format_quality(temp_dir.path().to_str().unwrap());
        assert!(result.is_ok());
    }

    #[test]
    fn test_check_quality_parse_error() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("bad.rs");
        fs::write(&file_path, "fn main() { invalid rust syntax +++").unwrap();

        let result = check_quality(temp_dir.path().to_str().unwrap(), false, None, false);
        assert!(result.is_err());
    }

    #[test]
    fn test_fix_quality_parse_error() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("bad.rs");
        fs::write(&file_path, "fn main() { invalid rust +++").unwrap();

        let result = fix_quality(temp_dir.path().to_str().unwrap(), false, None);
        assert!(result.is_err());
    }

    #[test]
    fn test_fix_quality_with_fixes() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.rs");
        fs::write(
            &file_path,
            "fn main() { let x = std::fs::read_to_string(\"f\"); }"
        )
        .unwrap();

        let result = fix_quality(temp_dir.path().to_str().unwrap(), false, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_check_quality_no_files() {
        let temp_dir = TempDir::new().unwrap();
        let result = check_quality(temp_dir.path().to_str().unwrap(), false, None, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_fix_quality_no_files() {
        let temp_dir = TempDir::new().unwrap();
        let result = fix_quality(temp_dir.path().to_str().unwrap(), true, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_format_quality_with_changes() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.rs");
        fs::write(
            &file_path,
            "fn main() { let x = std::fs::read_to_string(\"f\"); }"
        )
        .unwrap();

        let result = format_quality(temp_dir.path().to_str().unwrap());
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_diff_full() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.rs");
        fs::write(
            &file_path,
            "fn main() { let x = std::fs::read_to_string(\"f\"); }"
        )
        .unwrap();

        let result = run_diff(temp_dir.path().to_str().unwrap(), false, false, None, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_diff_summary() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.rs");
        fs::write(
            &file_path,
            "fn main() { let x = std::fs::read_to_string(\"f\"); }"
        )
        .unwrap();

        let result = run_diff(temp_dir.path().to_str().unwrap(), true, false, None, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_diff_no_changes() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.rs");
        fs::write(&file_path, "fn main() {}").unwrap();

        let result = run_diff(temp_dir.path().to_str().unwrap(), false, false, None, false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_run_diff_parse_error() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("test.rs");
        fs::write(&file_path, "fn main() { invalid +++").unwrap();

        let result = run_diff(temp_dir.path().to_str().unwrap(), false, false, None, false);
        assert!(result.is_err());
    }
}