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 features = self.features.join(",");
24        let features = if self.features.is_empty() {
25            "default,all-integration-tests"
26        } else {
27            &features
28        };
29
30        let tool = if self.clippy { "clippy" } else { "check" };
31
32        let pre_args = if self.fix {
33            vec!["--fix"]
34        } else {
35            Vec::default()
36        };
37
38        let clippy_args = if self.clippy {
39            vec!["--", "-D", "warnings"]
40        } else {
41            Vec::default()
42        };
43
44        [
45            tool,
46            "--workspace",
47            "--all-targets",
48            "--no-default-features",
49            "--features",
50            features,
51        ]
52        .chain_args(pre_args)
53        .chain_args(clippy_args)
54    }
55
56    pub fn exec(self) -> Result<()> {
57        app::exec("cargo", self.build_args(), true)?;
58
59        // If --fix was used, check for changes and commit them.
60        if self.fix {
61            let has_changes = !git::get_modified_files()?.is_empty();
62            if has_changes {
63                git::commit("chore(vdev): apply vdev rust check fixes")?;
64            }
65        }
66
67        Ok(())
68    }
69}