1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use std::process::Command;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
    #[error("I/O error when executing `rustc -vV`. {0}")]
    Io(#[from] std::io::Error),
    #[error("Output of `rustc -vV` was not valid UTF-8. {0}")]
    Utf8(#[from] std::str::Utf8Error),
    #[error("Unexpected output structure for `rustc -vV` after successful execution")]
    UnexpectedOutputStructure,
}
pub fn from_cli() -> Result<String, Error> {
    let output = Command::new("rustc")
        .arg("-vV")
        .output()
        .map_err(Error::from)?;
    let output_ref = std::str::from_utf8(&output.stdout).map_err(Error::from)?;
    match output_ref.lines().find_map(|l| l.strip_prefix("host: ")) {
        Some(host) => Ok(host.to_string()),
        None => Err(Error::UnexpectedOutputStructure),
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    #[cfg(all(
        target_arch = "x86_64",
        target_vendor = "pc",
        target_os = "windows",
        target_env = "msvc"
    ))]
    fn test_from_cli() {
        let host = from_cli().unwrap();
        assert_eq!(host, "x86_64-pc-windows-msvc");
    }
}