cargo_zigbuild/
check.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 check` for more detailed information."
14)]
15pub struct Check {
16    #[command(flatten)]
17    pub cargo: cargo_options::Check,
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
28impl Check {
29    /// Create a new check from manifest path
30    #[allow(clippy::field_reassign_with_default)]
31    pub fn new(manifest_path: Option<PathBuf>) -> Self {
32        let mut build = Self::default();
33        build.manifest_path = manifest_path;
34        build
35    }
36
37    /// Execute `cargo check` command
38    pub fn execute(&self) -> Result<()> {
39        let mut run = self.build_command()?;
40
41        let mut child = run.spawn().context("Failed to run cargo check")?;
42        let status = child.wait().expect("Failed to wait on cargo check process");
43        if !status.success() {
44            process::exit(status.code().unwrap_or(1));
45        }
46        Ok(())
47    }
48
49    /// Generate cargo subcommand
50    pub fn build_command(&self) -> Result<Command> {
51        let mut build = self.cargo.command();
52        if !self.disable_zig_linker {
53            Zig::apply_command_env(
54                self.manifest_path.as_deref(),
55                self.release,
56                &self.cargo.common,
57                &mut build,
58                self.enable_zig_ar,
59            )?;
60        }
61
62        Ok(build)
63    }
64}
65
66impl Deref for Check {
67    type Target = cargo_options::Check;
68
69    fn deref(&self) -> &Self::Target {
70        &self.cargo
71    }
72}
73
74impl DerefMut for Check {
75    fn deref_mut(&mut self) -> &mut Self::Target {
76        &mut self.cargo
77    }
78}
79
80impl From<cargo_options::Check> for Check {
81    fn from(cargo: cargo_options::Check) -> Self {
82        Self {
83            cargo,
84            ..Default::default()
85        }
86    }
87}