buildkit_rs_llb/
platform.rs

1use std::{borrow::Cow, fmt};
2
3#[derive(Debug, Clone, PartialEq, Eq, Hash)]
4pub struct Platform<'a> {
5    /// The architecture of the platform
6    pub architecture: Cow<'a, str>,
7    /// The name of the operating system
8    pub os: Cow<'a, str>,
9    /// The variant of the architecture
10    pub variant: Option<Cow<'a, str>>,
11}
12
13impl<'a> Platform<'a> {
14    pub const fn new(os: &'a str, arch: &'a str, variant: Option<&'a str>) -> Self {
15        Self {
16            architecture: Cow::Borrowed(arch),
17            os: Cow::Borrowed(os),
18            // We have to manually map since `map` is not const yet
19            // TODO: Replace with `map` when it is const
20            variant: match variant {
21                Some(variant) => Some(Cow::Borrowed(variant)),
22                None => None,
23            },
24        }
25    }
26}
27
28impl Platform<'_> {
29    pub fn into_static(self) -> Platform<'static> {
30        Platform {
31            architecture: Cow::Owned(self.architecture.into_owned()),
32            os: Cow::Owned(self.os.into_owned()),
33            variant: match self.variant {
34                Some(variant) => Some(Cow::Owned(variant.into_owned())),
35                None => None,
36            },
37        }
38    }
39}
40
41impl fmt::Display for Platform<'_> {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        write!(f, "{}-{}", self.os, self.architecture)?;
44        if let Some(variant) = &self.variant {
45            write!(f, "-{}", variant)?;
46        }
47        Ok(())
48    }
49}
50
51pub const LINUX_AMD64: Platform = Platform::new("linux", "amd64", None);
52pub const LINUX_ARMHF: Platform = Platform::new("linux", "arm", Some("v7"));
53pub const LINUX_ARM: Platform = LINUX_ARMHF; // ALIAS FOR LINUX_ARMHF
54pub const LINUX_ARMEL: Platform = Platform::new("linux", "arm", Some("v6"));
55pub const LINUX_ARM64: Platform = Platform::new("linux", "arm64", None);
56pub const LINUX_S390X: Platform = Platform::new("linux", "s390x", None);
57pub const LINUX_PPC64: Platform = Platform::new("linux", "ppc64", None);
58pub const LINUX_PPC64LE: Platform = Platform::new("linux", "ppc64le", None);
59pub const DARWIN: Platform = Platform::new("darwin", "amd64", None);
60pub const WINDOWS: Platform = Platform::new("windows", "amd64", None);