Skip to main content

ruvix_types/
rvf.rs

1//! RVF (RuVector Format) types for boot and component mounting.
2//!
3//! RuVix boots from a single signed RVF file and mounts additional
4//! RVF packages as components in the namespace.
5
6use crate::handle::Handle;
7
8/// Handle to a mounted RVF package.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10#[repr(transparent)]
11pub struct RvfMountHandle(pub Handle);
12
13impl RvfMountHandle {
14    /// Creates a new RVF mount handle.
15    #[inline]
16    #[must_use]
17    pub const fn new(id: u32, generation: u32) -> Self {
18        Self(Handle::new(id, generation))
19    }
20
21    /// Creates a null (invalid) RVF mount handle.
22    #[inline]
23    #[must_use]
24    pub const fn null() -> Self {
25        Self(Handle::null())
26    }
27
28    /// Checks if this handle is null.
29    #[inline]
30    #[must_use]
31    pub const fn is_null(&self) -> bool {
32        self.0.is_null()
33    }
34
35    /// Returns the raw handle.
36    #[inline]
37    #[must_use]
38    pub const fn raw(&self) -> Handle {
39        self.0
40    }
41}
42
43impl Default for RvfMountHandle {
44    fn default() -> Self {
45        Self::null()
46    }
47}
48
49/// Identifier for an RVF component within a mounted package.
50///
51/// Each RVF package can contain multiple components (WASM modules
52/// with WIT interfaces). Components are identified by index within
53/// the package's component graph.
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
55#[repr(C)]
56pub struct RvfComponentId {
57    /// The mount handle of the containing RVF package.
58    pub mount: RvfMountHandle,
59
60    /// Component index within the package (0-based).
61    pub component_index: u32,
62}
63
64impl RvfComponentId {
65    /// Creates a new component ID.
66    #[inline]
67    #[must_use]
68    pub const fn new(mount: RvfMountHandle, component_index: u32) -> Self {
69        Self {
70            mount,
71            component_index,
72        }
73    }
74
75    /// Creates a component ID for the root component (index 0).
76    #[inline]
77    #[must_use]
78    pub const fn root(mount: RvfMountHandle) -> Self {
79        Self {
80            mount,
81            component_index: 0,
82        }
83    }
84}
85
86/// RVF package verification status.
87#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
88#[repr(u8)]
89pub enum RvfVerifyStatus {
90    /// Package signature is valid (ML-DSA-65 verified).
91    SignatureValid = 0,
92
93    /// Signature verification failed.
94    SignatureInvalid = 1,
95
96    /// Package manifest is malformed.
97    ManifestInvalid = 2,
98
99    /// Required component is missing.
100    ComponentMissing = 3,
101
102    /// Proof policy cannot be satisfied.
103    ProofPolicyInvalid = 4,
104
105    /// Package requires capabilities not available.
106    CapabilitiesInsufficient = 5,
107}
108
109impl RvfVerifyStatus {
110    /// Returns true if the package is valid for mounting.
111    #[inline]
112    #[must_use]
113    pub const fn is_valid(&self) -> bool {
114        matches!(self, Self::SignatureValid)
115    }
116
117    /// Returns the status as a string.
118    #[inline]
119    #[must_use]
120    pub const fn as_str(&self) -> &'static str {
121        match self {
122            Self::SignatureValid => "Signature valid",
123            Self::SignatureInvalid => "Signature invalid",
124            Self::ManifestInvalid => "Manifest invalid",
125            Self::ComponentMissing => "Component missing",
126            Self::ProofPolicyInvalid => "Proof policy invalid",
127            Self::CapabilitiesInsufficient => "Capabilities insufficient",
128        }
129    }
130}
131
132/// RVF WIT (WASM Interface Types) type identifier.
133///
134/// Used for queue message schema validation.
135#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
136#[repr(transparent)]
137pub struct WitTypeId(pub u32);
138
139impl WitTypeId {
140    /// No schema (raw bytes).
141    pub const NONE: Self = Self(0);
142
143    /// Creates a new WIT type ID.
144    #[inline]
145    #[must_use]
146    pub const fn new(id: u32) -> Self {
147        Self(id)
148    }
149
150    /// Returns true if this is the NONE type (no validation).
151    #[inline]
152    #[must_use]
153    pub const fn is_none(&self) -> bool {
154        self.0 == 0
155    }
156}
157
158impl Default for WitTypeId {
159    fn default() -> Self {
160        Self::NONE
161    }
162}
163
164#[cfg(test)]
165mod tests {
166    use super::*;
167
168    #[test]
169    fn test_rvf_mount_handle() {
170        let h = RvfMountHandle::new(1, 2);
171        assert!(!h.is_null());
172    }
173
174    #[test]
175    fn test_rvf_component_id() {
176        let mount = RvfMountHandle::new(1, 0);
177        let component = RvfComponentId::new(mount, 3);
178        assert_eq!(component.component_index, 3);
179    }
180
181    #[test]
182    fn test_rvf_verify_status() {
183        assert!(RvfVerifyStatus::SignatureValid.is_valid());
184        assert!(!RvfVerifyStatus::SignatureInvalid.is_valid());
185    }
186}