Skip to main content

lisette_stdlib/
target.rs

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