Skip to main content

cargo_zigbuild/
doc.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 doc` for more detailed information."
14)]
15pub struct Doc {
16    #[command(flatten)]
17    pub cargo: cargo_options::Doc,
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 Doc {
33    /// Create a new doc 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 doc` 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 doc")?;
46        let status = child.wait().expect("Failed to wait on cargo doc process");
47        if !status.success() {
48            process::exit(status.code().unwrap_or(1));
49        }
50        Ok(())
51    }
52
53    /// Generate cargo subcommand
54    pub fn build_command(&self) -> Result<Command> {
55        let mut build = self.cargo.command();
56        if !self.disable_zig_linker {
57            Zig::apply_command_env(
58                self.manifest_path.as_deref(),
59                self.release,
60                &self.cargo.common,
61                &mut build,
62                self.enable_zig_ar,
63                self.macosx_deployment_target.as_deref(),
64            )?;
65        }
66
67        Ok(build)
68    }
69}
70
71impl Deref for Doc {
72    type Target = cargo_options::Doc;
73
74    fn deref(&self) -> &Self::Target {
75        &self.cargo
76    }
77}
78
79impl DerefMut for Doc {
80    fn deref_mut(&mut self) -> &mut Self::Target {
81        &mut self.cargo
82    }
83}
84
85impl From<cargo_options::Doc> for Doc {
86    fn from(cargo: cargo_options::Doc) -> Self {
87        Self {
88            cargo,
89            ..Default::default()
90        }
91    }
92}