cli_xtask/subcommand/
tidy.rs

1use crate::{args::FeatureArgs, config::Config, Result, Run, SubcommandRun};
2
3/// Arguments definition of the `tidy` subcommand.
4#[cfg_attr(doc, doc = include_str!("../../doc/cargo-xtask-tidy.md"))]
5#[derive(Debug, Clone, Default, clap::Args)]
6#[non_exhaustive]
7pub struct Tidy {
8    /// Fix code even if a VCS was not detected.
9    #[clap(long)]
10    allow_no_vcs: bool,
11    /// Fix code even if the working directory is dirty.
12    #[clap(long)]
13    allow_dirty: bool,
14    /// Fix code even if the working directory has staged changes.
15    #[clap(long)]
16    allow_staged: bool,
17    /// Features to run the cargo commands with.
18    #[clap(flatten)]
19    pub feature_args: FeatureArgs,
20}
21
22impl Run for Tidy {
23    fn run(&self, config: &Config) -> Result<()> {
24        self.run(config)
25    }
26
27    fn to_subcommands(&self) -> Option<SubcommandRun> {
28        Some(self.to_subcommands())
29    }
30}
31
32impl Tidy {
33    /// Returns a list of all subcommands to run.
34    pub fn subcommands(&self) -> Vec<Box<dyn Run>> {
35        let Self {
36            allow_no_vcs,
37            allow_dirty,
38            allow_staged,
39            feature_args,
40        } = self;
41
42        let _ = allow_no_vcs; // supress unused-variables warning
43        let _ = allow_dirty; // supress unused-variables warning
44        let _ = allow_staged; // supress unused-variables warning
45        let _ = feature_args.clone(); // supress unused-variables warning
46
47        vec![
48            // cargo fmt
49            #[cfg(feature = "subcommand-fmt")]
50            Box::new(super::Fmt {
51                env_args: Default::default(),
52                package_args: feature_args.package_args.clone(),
53                extra_options: vec![],
54            }),
55            // cargo clippy --fix
56            #[cfg(feature = "subcommand-clippy")]
57            Box::new({
58                let mut clippy_options = vec![];
59                if *allow_no_vcs {
60                    clippy_options.push("--allow-no-vcs");
61                }
62                if *allow_dirty {
63                    clippy_options.push("--allow-dirty");
64                }
65                if *allow_staged {
66                    clippy_options.push("--allow-staged");
67                }
68
69                super::Clippy {
70                    env_args: Default::default(),
71                    feature_args: feature_args.clone(),
72                    extra_options: ["--fix", "--all-targets"]
73                        .into_iter()
74                        .chain(clippy_options)
75                        .map(String::from)
76                        .collect(),
77                }
78            }),
79            // cargo sync-rdme
80            #[cfg(feature = "subcommand-sync-rdme")]
81            Box::new({
82                let mut sync_rdme_options = vec![];
83                if *allow_no_vcs {
84                    sync_rdme_options.push("--allow-no-vcs");
85                }
86                if *allow_dirty {
87                    sync_rdme_options.push("--allow-dirty");
88                }
89                if *allow_staged {
90                    sync_rdme_options.push("--allow-staged");
91                }
92                super::SyncRdme {
93                    env_args: Default::default(),
94                    package_args: feature_args.package_args.clone(),
95                    extra_options: sync_rdme_options.into_iter().map(String::from).collect(),
96                }
97            }),
98        ]
99    }
100
101    /// Returns the subcommands that this command will run.
102    pub fn to_subcommands(&self) -> SubcommandRun {
103        SubcommandRun::new(self.subcommands())
104    }
105
106    /// Runs the `tidy` subcommand.
107    #[tracing::instrument(name = "tidy", skip_all, err)]
108    pub fn run(&self, config: &Config) -> Result<()> {
109        self.to_subcommands().run(config)
110    }
111}