From f5686796880df2c1bc2ed29bee7a50778c19ba77 Mon Sep 17 00:00:00 2001 From: Pedro Henrique Penna Date: Wed, 21 Oct 2026 08:47:42 -0300 Subject: [PATCH] nvx-rs: parse the mount mode, owner, and kind names with one function MountAccess, MountOwner, and MountKind, the settings of a sandbox share in host/sandbox/mount.rs, each had a from_name that matched its two names as string literals and refused any other name with a message of one shape, which listed the names again: unsupported sandbox mount SETTING 'NAME'; choose FIRST or SECOND So each setting spelled its names three times: in as_str, in the match of from_name, and in its message. A private function, setting_from_name, now takes the setting's name, its two values, and its as_str. It returns the value whose as_str is the name, and otherwise refuses the name with the message, which it builds from the setting and the values' as_str names. Each from_name is one call of it, so as_str is the one place that spells a setting's names, as Guest::from_name and LayerRole::from_name already find their values by their names. Behavior is unchanged: the same names give the same values, and any other name fails with ErrorKind::Unsupported and the same message, byte for byte, since each from_name passes its values in the order in which its message listed them. The public signatures and documentation of the three methods stay as they were. The share tests check the owner's and the mode's messages, and the launch tests the kind's. The golden sandbox tests parse the owner and the kind of each share that they replay with these methods, and the CLI's lifecycle scenarios with one, several, caller-owned, policy, and file shares read recorded shares through all three. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- nvx-rs/src/host/sandbox/mount.rs | 55 ++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/nvx-rs/src/host/sandbox/mount.rs b/nvx-rs/src/host/sandbox/mount.rs index c90e30184..5ec97f304 100644 --- a/nvx-rs/src/host/sandbox/mount.rs +++ b/nvx-rs/src/host/sandbox/mount.rs @@ -77,14 +77,12 @@ impl MountAccess { /// /// Fails with [`ErrorKind::Unsupported`](crate::ErrorKind::Unsupported) for another name. pub fn from_name(name: &str) -> Result { - match name { - "ro" => Ok(Self::ReadOnly), - "rw" => Ok(Self::ReadWrite), - _ => Err(refused(format!( - "unsupported sandbox mount mode {}; choose ro or rw", - repr(name) - ))), - } + setting_from_name( + "mode", + [Self::ReadOnly, Self::ReadWrite], + Self::as_str, + name, + ) } } @@ -117,14 +115,7 @@ impl MountOwner { /// /// Fails with [`ErrorKind::Unsupported`](crate::ErrorKind::Unsupported) for another name. pub fn from_name(name: &str) -> Result { - match name { - "vmm" => Ok(Self::Vmm), - "caller" => Ok(Self::Caller), - _ => Err(refused(format!( - "unsupported sandbox mount owner {}; choose vmm or caller", - repr(name) - ))), - } + setting_from_name("owner", [Self::Vmm, Self::Caller], Self::as_str, name) } } @@ -157,14 +148,7 @@ impl MountKind { /// /// Fails with [`ErrorKind::Unsupported`](crate::ErrorKind::Unsupported) for another name. pub fn from_name(name: &str) -> Result { - match name { - "directory" => Ok(Self::Directory), - "file" => Ok(Self::File), - _ => Err(refused(format!( - "unsupported sandbox mount kind {}; choose directory or file", - repr(name) - ))), - } + setting_from_name("kind", [Self::Directory, Self::File], Self::as_str, name) } } @@ -174,6 +158,29 @@ impl fmt::Display for MountKind { } } +/// Returns the one of `values`, the two values of a share's `setting`, whose name, as `as_str` +/// gives it, is `name`, as a recorded share gives it. +/// +/// Fails with [`ErrorKind::Unsupported`](crate::ErrorKind::Unsupported) for another name, with a +/// message that names the setting and its two values. +fn setting_from_name( + setting: &str, + values: [T; 2], + as_str: fn(T) -> &'static str, + name: &str, +) -> Result { + values + .into_iter() + .find(|&value| as_str(value) == name) + .ok_or_else(|| { + let [first, second] = values.map(as_str); + refused(format!( + "unsupported sandbox mount {setting} {}; choose {first} or {second}", + repr(name) + )) + }) +} + /// A kind of access-policy path. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum PolicyKind {