microsandbox_image/platform.rs
1//! Target platform for OCI image resolution.
2
3//--------------------------------------------------------------------------------------------------
4// Types
5//--------------------------------------------------------------------------------------------------
6
7/// Re-export of [`oci_spec::image::Os`].
8pub type Os = oci_spec::image::Os;
9
10/// Re-export of [`oci_spec::image::Arch`].
11pub type Arch = oci_spec::image::Arch;
12
13/// Target platform for OCI image resolution.
14///
15/// Used to select the correct manifest from a multi-platform OCI index.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct Platform {
18 /// Operating system (always `linux` for microsandbox).
19 pub os: Os,
20 /// CPU architecture (e.g., `amd64`, `arm64`).
21 pub arch: Arch,
22 /// Optional architecture variant (e.g., `v7` for armv7).
23 pub variant: Option<String>,
24}
25
26//--------------------------------------------------------------------------------------------------
27// Methods
28//--------------------------------------------------------------------------------------------------
29
30impl Platform {
31 /// Create a new platform.
32 pub fn new(os: impl Into<Os>, arch: impl Into<Arch>) -> Self {
33 Self {
34 os: os.into(),
35 arch: arch.into(),
36 variant: None,
37 }
38 }
39
40 /// Create a new platform with variant.
41 pub fn with_variant(
42 os: impl Into<Os>,
43 arch: impl Into<Arch>,
44 variant: impl Into<String>,
45 ) -> Self {
46 Self {
47 os: os.into(),
48 arch: arch.into(),
49 variant: Some(variant.into()),
50 }
51 }
52
53 /// Returns the platform for the current host, with OS forced to `linux`.
54 ///
55 /// Architecture detected via `std::env::consts::ARCH`:
56 /// `x86_64` -> `amd64`, `aarch64` -> `arm64`.
57 pub fn host_linux() -> Self {
58 let arch = match std::env::consts::ARCH {
59 "x86_64" => "amd64",
60 "aarch64" => "arm64",
61 other => other,
62 };
63 Self::new("linux", arch)
64 }
65}
66
67//--------------------------------------------------------------------------------------------------
68// Trait Implementations
69//--------------------------------------------------------------------------------------------------
70
71impl Default for Platform {
72 fn default() -> Self {
73 Self::host_linux()
74 }
75}
76
77//--------------------------------------------------------------------------------------------------
78// Tests
79//--------------------------------------------------------------------------------------------------
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn test_host_linux() {
87 let p = Platform::host_linux();
88 assert_eq!(p.os, Os::Linux);
89 assert!(p.arch == Arch::Amd64 || p.arch == Arch::ARM64);
90 }
91
92 #[test]
93 fn test_default_is_host_linux() {
94 let p = Platform::default();
95 assert_eq!(p.os, Os::Linux);
96 }
97}