refactor: derive filesystem access mode ordering

This commit is contained in:
viyatb-oai
2026-03-11 18:12:37 -07:00
parent f03a477d71
commit 94331f1d51
2 changed files with 17 additions and 25 deletions
+2 -2
View File
@@ -577,9 +577,9 @@
"FileSystemAccessMode": {
"description": "Access mode for a filesystem entry.\n\nWhen two equally specific entries target the same path, we compare these by conflict precedence rather than by capability breadth: `none` beats `write`, and `write` beats `read`.",
"enum": [
"none",
"read",
"write"
"write",
"none"
],
"type": "string"
},
+15 -23
View File
@@ -1,4 +1,3 @@
use std::cmp::Ordering;
use std::collections::HashSet;
use std::ffi::OsStr;
use std::io;
@@ -40,24 +39,29 @@ impl NetworkSandboxPolicy {
/// When two equally specific entries target the same path, we compare these by
/// conflict precedence rather than by capability breadth: `none` beats
/// `write`, and `write` beats `read`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Display, JsonSchema, TS)]
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Serialize,
Deserialize,
Display,
JsonSchema,
TS,
)]
#[serde(rename_all = "lowercase")]
#[strum(serialize_all = "lowercase")]
pub enum FileSystemAccessMode {
None,
Read,
Write,
None,
}
impl FileSystemAccessMode {
fn precedence_rank(self) -> u8 {
match self {
FileSystemAccessMode::Read => 0,
FileSystemAccessMode::Write => 1,
FileSystemAccessMode::None => 2,
}
}
pub fn can_read(self) -> bool {
!matches!(self, FileSystemAccessMode::None)
}
@@ -67,18 +71,6 @@ impl FileSystemAccessMode {
}
}
impl PartialOrd for FileSystemAccessMode {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for FileSystemAccessMode {
fn cmp(&self, other: &Self) -> Ordering {
self.precedence_rank().cmp(&other.precedence_rank())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, TS)]
#[serde(tag = "kind", rename_all = "snake_case")]
#[ts(tag = "kind")]