From 96c0b3f069b007c9c2f05d3252a0715ea6d90fff Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Thu, 5 Oct 2023 18:00:51 +0300 Subject: refactor(tvix/store/proto): use NamedNode trait This saves us writing the name parsing code three times. We can also delay parsing until we did other (cheaper) checks. Change-Id: I1abe3f20dba4215b38839cf7466297e028d64656 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9548 Tested-by: BuildkiteCI Reviewed-by: Connor Brewster Autosubmit: flokli --- tvix/store/src/proto/mod.rs | 59 +++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 31 deletions(-) (limited to 'tvix/store/src/proto') diff --git a/tvix/store/src/proto/mod.rs b/tvix/store/src/proto/mod.rs index 3bc4c2114e..c1d9d0c46e 100644 --- a/tvix/store/src/proto/mod.rs +++ b/tvix/store/src/proto/mod.rs @@ -3,7 +3,10 @@ use data_encoding::BASE64; // https://github.com/hyperium/tonic/issues/1056 use nix_compat::store_path::{self, StorePath}; use thiserror::Error; -use tvix_castore::{proto as castorepb, B3Digest, B3_LEN}; +use tvix_castore::{ + proto::{self as castorepb, NamedNode}, + B3Digest, B3_LEN, +}; mod grpc_pathinfoservice_wrapper; @@ -135,37 +138,31 @@ impl PathInfo { None => { return Err(ValidatePathInfoError::NoNodePresent()); } - Some(castorepb::node::Node::Directory(directory_node)) => { - // ensure the digest has the appropriate size. - if TryInto::::try_into(directory_node.digest.clone()).is_err() { - return Err(ValidatePathInfoError::InvalidNodeDigestLen( - directory_node.digest.len(), - )); + Some(node) => { + match node { + // for a directory root node, ensure the digest has the appropriate size. + castorepb::node::Node::Directory(directory_node) => { + if TryInto::::try_into(directory_node.digest.clone()).is_err() + { + return Err(ValidatePathInfoError::InvalidNodeDigestLen( + directory_node.digest.len(), + )); + } + } + // for a file root node, ensure the digest has the appropriate size. + castorepb::node::Node::File(file_node) => { + // ensure the digest has the appropriate size. + if TryInto::::try_into(file_node.digest.clone()).is_err() { + return Err(ValidatePathInfoError::InvalidNodeDigestLen( + file_node.digest.len(), + )); + } + } + // nothing to do specifically for symlinks + castorepb::node::Node::Symlink(_) => {} } - - // parse the name - parse_node_name_root( - &directory_node.name, - ValidatePathInfoError::InvalidNodeName, - )? - } - Some(castorepb::node::Node::File(file_node)) => { - // ensure the digest has the appropriate size. - if TryInto::::try_into(file_node.digest.clone()).is_err() { - return Err(ValidatePathInfoError::InvalidNodeDigestLen( - file_node.digest.len(), - )); - } - - // parse the name - parse_node_name_root(&file_node.name, ValidatePathInfoError::InvalidNodeName)? - } - Some(castorepb::node::Node::Symlink(symlink_node)) => { - // parse the name - parse_node_name_root( - &symlink_node.name, - ValidatePathInfoError::InvalidNodeName, - )? + // parse the name of the node itself and return + parse_node_name_root(&node.get_name(), ValidatePathInfoError::InvalidNodeName)? } }, }; -- cgit 1.4.1