aboutsummaryrefslogtreecommitdiff
path: root/crate_universe/src/utils/target_triple.rs
blob: 1f76adca7490f7ab3e3c033700407e301c177e69 (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 struct TargetTriple(String);

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

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

    pub 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),
        }
    }
}