aboutsummaryrefslogtreecommitdiff
path: root/crate_universe/src/utils/target_triple.rs
blob: 4f2e1dfd0c1f7a0a2cf98e05548338cc28362d2e (plain)
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
use std::fmt::{Display, Formatter, Result};

use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub(crate) struct TargetTriple(String);

impl TargetTriple {
    #[cfg(test)]
    pub(crate) fn from_bazel(bazel: String) -> Self {
        Self(bazel)
    }

    pub(crate) fn to_bazel(&self) -> String {
        self.0.clone()
    }

    pub(crate) fn to_cargo(&self) -> String {
        // While Bazel is NixOS aware (via `@platforms//os:nixos`), `rustc`
        // is not, so any target triples for `nixos` get remapped to `linux`
        // for the purposes of determining `cargo metadata`, resolving `cfg`
        // targets, etc.
        self.0.replace("nixos", "linux")
    }
}

impl Display for TargetTriple {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        let bazel = self.to_bazel();
        let cargo = self.to_cargo();
        match bazel == cargo {
            true => write!(f, "{}", bazel),
            false => write!(f, "{} (cargo: {})", bazel, cargo),
        }
    }
}