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