vdev/commands/check/
rust.rs

1use anyhow::Result;
2use std::ffi::OsString;
3
4use crate::{app, git, util::ChainArgs as _};
5
6/// Check the Rust code for errors
7#[derive(clap::Args, Debug)]
8#[command()]
9pub struct Cli {
10    #[arg(long, default_value_t = true)]
11    clippy: bool,
12
13    #[arg(value_name = "FEATURE")]
14    features: Vec<String>,
15
16    #[arg(long)]
17    fix: bool,
18}
19
20impl Cli {
21    /// Build the argument vector for cargo invocation.
22    fn build_args(&self) -> Vec<OsString> {
23        let tool = if self.clippy { "clippy" } else { "check" };
24
25        let pre_args = if self.fix {
26            vec!["--fix"]
27        } else {
28            Vec::default()
29        };
30
31        let clippy_args = if self.clippy {
32            vec!["--", "-D", "warnings"]
33        } else {
34            Vec::default()
35        };
36
37        let feature_args = if self.features.is_empty() {
38            vec!["--all-features".to_string()]
39        } else {
40            vec![
41                "--no-default-features".to_string(),
42                "--features".to_string(),
43                self.features.join(",").to_string(),
44            ]
45        };
46
47        [tool, "--workspace", "--all-targets"]
48            .chain_args(feature_args)
49            .chain_args(pre_args)
50            .chain_args(clippy_args)
51    }
52
53    pub fn exec(self) -> Result<()> {
54        app::exec("cargo", self.build_args(), true)?;
55
56        // If --fix was used, check for changes and commit them.
57        if self.fix {
58            let has_changes = !git::get_modified_files()?.is_empty();
59            if has_changes {
60                git::commit("chore(vdev): apply vdev rust check fixes")?;
61            }
62        }
63
64        Ok(())
65    }
66}