Skip to main content

cargo_mate/sweeping/src/
mod.rs

1pub mod away;
2pub mod encryption;
3pub mod embedder;
4use anyhow::Result;
5use clap::{Parser, Subcommand};
6#[derive(Parser, Debug)]
7#[command(name = "sweep")]
8#[command(
9    about = "🧹 Sweep away println! and eprintln! debug statements from Rust code"
10)]
11pub struct SweepCli {
12    #[command(subcommand)]
13    command: SweepCommands,
14    #[arg(short, long)]
15    verbose: bool,
16}
17#[derive(Subcommand, Debug)]
18pub enum SweepCommands {
19    Scan { #[arg(default_value = ".")] path: String, #[arg(long)] include_tests: bool },
20    Sweep {
21        #[arg(default_value = ".")]
22        path: String,
23        #[arg(short, long)]
24        dry_run: bool,
25    },
26    Convert { #[arg(default_value = ".")] path: String },
27}
28pub fn run_sweep_standalone() -> Result<()> {
29    let cli = SweepCli::parse();
30    let args = vec!["sweep"];
31    match cli.command {
32        SweepCommands::Scan { path, include_tests } => {
33            let mut cmd_args = vec!["scan", & path];
34            if include_tests {
35                cmd_args.push("--include-tests");
36            }
37            let output = embedder::execute_sweep_binary(&cmd_args)?;
38            print_output(&output);
39        }
40        SweepCommands::Sweep { path, dry_run } => {
41            let mut cmd_args = vec!["sweep", & path];
42            if dry_run {
43                cmd_args.push("--dry-run");
44            }
45            let output = embedder::execute_sweep_binary(&cmd_args)?;
46            print_output(&output);
47        }
48        SweepCommands::Convert { path } => {
49            let cmd_args = vec!["convert", & path];
50            let output = embedder::execute_sweep_binary(&cmd_args)?;
51            print_output(&output);
52        }
53    }
54    Ok(())
55}
56fn print_output(output: &std::process::Output) {
57    if !output.stdout.is_empty() {
58        print!("{}", String::from_utf8_lossy(& output.stdout));
59    }
60    if !output.stderr.is_empty() {
61        eprint!("{}", String::from_utf8_lossy(& output.stderr));
62    }
63}
64#[cfg(test)]
65mod tests {
66    use super::*;
67    #[test]
68    fn test_sweep_cli_parsing() {
69        let args = vec!["sweep", "scan", "."];
70        let cli = SweepCli::try_parse_from(args).unwrap();
71        match cli.command {
72            SweepCommands::Scan { path, include_tests } => {
73                assert_eq!(path, ".");
74                assert!(! include_tests);
75            }
76            _ => panic!("Expected scan command"),
77        }
78    }
79}