Skip to main content

lisette_stdlib/
target.rs

1use std::fmt;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
4pub struct Target {
5    pub goos: &'static str,
6    pub goarch: &'static str,
7}
8
9impl Target {
10    pub const fn new(goos: &'static str, goarch: &'static str) -> Self {
11        Self { goos, goarch }
12    }
13
14    pub fn cache_segment(self) -> String {
15        format!("{}_{}", self.goos, self.goarch)
16    }
17
18    pub fn host() -> Self {
19        let goos = match std::env::consts::OS {
20            "macos" => "darwin",
21            other => other,
22        };
23        let goarch = match std::env::consts::ARCH {
24            "x86_64" => "amd64",
25            "aarch64" => "arm64",
26            "x86" => "386",
27            other => other,
28        };
29        Self { goos, goarch }
30    }
31}
32
33impl Default for Target {
34    fn default() -> Self {
35        Self::host()
36    }
37}
38
39impl fmt::Display for Target {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        write!(f, "{}/{}", self.goos, self.goarch)
42    }
43}
44
45/// Format a `(goos, goarch)` slice as a comma-separated `goos/goarch` list,
46/// for "Available on: ..." diagnostics.
47pub fn format_targets(targets: &[(&str, &str)]) -> String {
48    targets
49        .iter()
50        .map(|(goos, goarch)| format!("{}/{}", goos, goarch))
51        .collect::<Vec<_>>()
52        .join(", ")
53}