about summary refs log tree commit diff
path: root/tvix/castore/src/proto
diff options
context:
space:
mode:
Diffstat (limited to 'tvix/castore/src/proto')
-rw-r--r--tvix/castore/src/proto/mod.rs16
1 files changed, 15 insertions, 1 deletions
diff --git a/tvix/castore/src/proto/mod.rs b/tvix/castore/src/proto/mod.rs
index c734e8be89ec..9c4df4df53c2 100644
--- a/tvix/castore/src/proto/mod.rs
+++ b/tvix/castore/src/proto/mod.rs
@@ -24,7 +24,7 @@ pub const FILE_DESCRIPTOR_SET: &[u8] = tonic::include_file_descriptor_set!("tvix
 #[cfg(test)]
 mod tests;
 
-/// Errors that can occur during the validation of Directory messages.
+/// Errors that can occur during the validation of [Directory] messages.
 #[derive(Debug, PartialEq, Eq, thiserror::Error)]
 pub enum ValidateDirectoryError {
     /// Elements are not in sorted order
@@ -43,6 +43,8 @@ pub enum ValidateDirectoryError {
 /// Errors that occur during Node validation
 #[derive(Debug, PartialEq, Eq, thiserror::Error)]
 pub enum ValidateNodeError {
+    #[error("No node set")]
+    NoNodeSet,
     /// Invalid digest length encountered
     #[error("Invalid Digest length: {0}")]
     InvalidDigestLen(usize),
@@ -103,6 +105,18 @@ impl NamedNode for node::Node {
     }
 }
 
+impl Node {
+    /// Ensures the node has a valid enum kind (is Some), and passes its
+    // per-enum validation.
+    pub fn validate(&self) -> Result<(), ValidateNodeError> {
+        if let Some(node) = self.node.as_ref() {
+            node.validate()
+        } else {
+            Err(ValidateNodeError::NoNodeSet)
+        }
+    }
+}
+
 impl node::Node {
     /// Returns the node with a new name.
     pub fn rename(self, name: bytes::Bytes) -> Self {