Skip to main content

chaud_hot/cargo/
run.rs

1use super::metadata::ManifestPath;
2use std::env;
3use std::ffi::OsString;
4use std::process::{Command, Stdio};
5
6#[derive(Debug)]
7pub struct Cargo {
8    cargo: OsString,
9    mani: ManifestPath,
10}
11
12#[derive(Copy, Clone)]
13pub enum StdioMode {
14    /// Standard output is captured, standard error is ignored.
15    QuietCapture,
16    /// Standard output is captured, standard error is inherited.
17    LoudCapture,
18}
19
20impl Cargo {
21    pub fn new(mani: ManifestPath) -> Self {
22        let cargo = env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
23
24        Self { cargo, mani }
25    }
26
27    pub fn mani(&self) -> &ManifestPath {
28        &self.mani
29    }
30
31    pub fn cmd(&self, sub: &str, mode: StdioMode) -> Command {
32        let mut cmd = Command::new(&self.cargo);
33        cmd.args([sub, "--manifest-path", self.mani.path().as_str()])
34            .stdout(Stdio::null());
35
36        match mode {
37            StdioMode::QuietCapture => cmd.stdout(Stdio::piped()).stderr(Stdio::null()),
38            StdioMode::LoudCapture => cmd.stdout(Stdio::piped()).stderr(Stdio::inherit()),
39        };
40
41        cmd
42    }
43}