1 use std::fmt::{Display, Formatter, Result}; 2 3 use serde::{Deserialize, Serialize}; 4 5 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] 6 #[serde(transparent)] 7 pub(crate) struct TargetTriple(String); 8 9 impl TargetTriple { 10 #[cfg(test)] from_bazel(bazel: String) -> Self11 pub(crate) fn from_bazel(bazel: String) -> Self { 12 Self(bazel) 13 } 14 to_bazel(&self) -> String15 pub(crate) fn to_bazel(&self) -> String { 16 self.0.clone() 17 } 18 to_cargo(&self) -> String19 pub(crate) fn to_cargo(&self) -> String { 20 // While Bazel is NixOS aware (via `@platforms//os:nixos`), `rustc` 21 // is not, so any target triples for `nixos` get remapped to `linux` 22 // for the purposes of determining `cargo metadata`, resolving `cfg` 23 // targets, etc. 24 self.0.replace("nixos", "linux") 25 } 26 } 27 28 impl Display for TargetTriple { fmt(&self, f: &mut Formatter<'_>) -> Result29 fn fmt(&self, f: &mut Formatter<'_>) -> Result { 30 let bazel = self.to_bazel(); 31 let cargo = self.to_cargo(); 32 match bazel == cargo { 33 true => write!(f, "{}", bazel), 34 false => write!(f, "{} (cargo: {})", bazel, cargo), 35 } 36 } 37 } 38