Skip to main content

varve_core/
platform.rs

1//! The platform dimension (REQ-PLATFORM-001, DD-001).
2//!
3//! A layer is one manifest for N platforms — the OCI index's own mechanism,
4//! not a substitution language. Deposit stamps every entry with a target
5//! triple; install selects only entries for the host (or an explicit
6//! override). Entries WITHOUT a platform annotation are platform-independent
7//! — layers deposited before this dimension existed keep working, and a
8//! depositor that makes no platform claim gets no platform filtering.
9//! A fully-stamped layer with nothing for the host fails closed: a
10//! wrong-architecture binary never reaches the core.
11
12/// Annotation carrying an entry's target triple.
13pub const ANN_PLATFORM: &str = "eu.pulseengine.platform";
14
15/// The host's target triple, in the same vocabulary deposits use.
16pub fn host_platform() -> String {
17    let arch = std::env::consts::ARCH;
18    match std::env::consts::OS {
19        "macos" => format!("{arch}-apple-darwin"),
20        "linux" => format!("{arch}-unknown-linux-gnu"),
21        "windows" => format!("{arch}-pc-windows-msvc"),
22        other => format!("{arch}-{other}"),
23    }
24}
25
26/// Does an entry's (optional) platform annotation admit this platform?
27pub fn entry_matches(entry_platform: Option<&str>, platform: &str) -> bool {
28    match entry_platform {
29        None => true,
30        // wasm32 targets are PORTABLE: the bytes run wherever a runner
31        // exists (REQ-RUNNER-001) — no per-platform gaps by construction.
32        Some(p) if p.starts_with("wasm32") => true,
33        Some(p) => p == platform,
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    // rivet: verifies REQ-PLATFORM-001
42    #[test]
43    fn unstamped_entries_are_platform_independent_and_stamped_ones_are_exact() {
44        assert!(entry_matches(None, "aarch64-apple-darwin"));
45        assert!(entry_matches(
46            Some("aarch64-apple-darwin"),
47            "aarch64-apple-darwin"
48        ));
49        assert!(!entry_matches(
50            Some("x86_64-unknown-linux-gnu"),
51            "aarch64-apple-darwin"
52        ));
53    }
54
55    // rivet: verifies REQ-RUNNER-001
56    #[test]
57    fn wasm32_entries_are_portable_to_every_host() {
58        assert!(entry_matches(Some("wasm32-wasip2"), "aarch64-apple-darwin"));
59        assert!(entry_matches(
60            Some("wasm32-wasip2"),
61            "x86_64-unknown-linux-gnu"
62        ));
63        assert!(entry_matches(Some("wasm32-unknown-unknown"), "anything"));
64    }
65
66    // rivet: verifies REQ-PLATFORM-001
67    #[test]
68    fn the_host_platform_is_a_target_triple() {
69        let host = host_platform();
70        assert!(
71            host.split('-').count() >= 2 && host.contains(std::env::consts::ARCH),
72            "host platform should be triple-shaped: {host}"
73        );
74    }
75}