Skip to main content

cargo_zigbuild/
clippy.rs

1use std::ops::{Deref, DerefMut};
2use std::path::PathBuf;
3use std::process::{self, Command};
4
5use anyhow::{Context, Result};
6use clap::Parser;
7
8use crate::Zig;
9
10#[derive(Clone, Debug, Default, Parser)]
11#[command(
12    display_order = 1,
13    after_help = "Run `cargo help clippy` for more detailed information."
14)]
15pub struct Clippy {
16    #[command(flatten)]
17    pub cargo: cargo_options::Clippy,
18
19    /// Disable zig linker
20    #[arg(skip)]
21    pub disable_zig_linker: bool,
22
23    /// Enable zig ar
24    #[arg(skip)]
25    pub enable_zig_ar: bool,
26
27    /// Minimum macOS version, overrides `MACOSX_DEPLOYMENT_TARGET`
28    #[arg(skip)]
29    pub macosx_deployment_target: Option<String>,
30}
31
32impl Clippy {
33    /// Create a new run from manifest path
34    #[allow(clippy::field_reassign_with_default)]
35    pub fn new(manifest_path: Option<PathBuf>) -> Self {
36        let mut build = Self::default();
37        build.manifest_path = manifest_path;
38        build
39    }
40
41    /// Execute `cargo clippy` command
42    pub fn execute(&self) -> Result<()> {
43        let mut run = self.build_command()?;
44
45        let mut child = run.spawn().context("Failed to run cargo clippy")?;
46        let status = child
47            .wait()
48            .expect("Failed to wait on cargo clippy process");
49        if !status.success() {
50            process::exit(status.code().unwrap_or(1));
51        }
52        Ok(())
53    }
54
55    /// Generate cargo subcommand
56    pub fn build_command(&self) -> Result<Command> {
57        let mut build = self.cargo.command();
58        if !self.disable_zig_linker {
59            Zig::apply_command_env(
60                self.manifest_path.as_deref(),
61                self.release,
62                &self.cargo.common,
63                &mut build,
64                self.enable_zig_ar,
65                self.macosx_deployment_target.as_deref(),
66            )?;
67        }
68
69        Ok(build)
70    }
71}
72
73impl Deref for Clippy {
74    type Target = cargo_options::Clippy;
75
76    fn deref(&self) -> &Self::Target {
77        &self.cargo
78    }
79}
80
81impl DerefMut for Clippy {
82    fn deref_mut(&mut self) -> &mut Self::Target {
83        &mut self.cargo
84    }
85}
86
87impl From<cargo_options::Clippy> for Clippy {
88    fn from(cargo: cargo_options::Clippy) -> Self {
89        Self {
90            cargo,
91            ..Default::default()
92        }
93    }
94}