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 <cbrewster@hey.com>
Autosubmit: flokli <flokli@flokli.de>
This commit is contained in:
Florian Klink 2023-10-05 18:00:51 +03:00 committed by clbot
parent a2c81de4cb
commit 96c0b3f069

View file

@ -3,7 +3,10 @@ use data_encoding::BASE64;
// https://github.com/hyperium/tonic/issues/1056 // https://github.com/hyperium/tonic/issues/1056
use nix_compat::store_path::{self, StorePath}; use nix_compat::store_path::{self, StorePath};
use thiserror::Error; 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; mod grpc_pathinfoservice_wrapper;
@ -135,37 +138,31 @@ impl PathInfo {
None => { None => {
return Err(ValidatePathInfoError::NoNodePresent()); return Err(ValidatePathInfoError::NoNodePresent());
} }
Some(castorepb::node::Node::Directory(directory_node)) => { Some(node) => {
// ensure the digest has the appropriate size. match node {
if TryInto::<B3Digest>::try_into(directory_node.digest.clone()).is_err() { // for a directory root node, ensure the digest has the appropriate size.
castorepb::node::Node::Directory(directory_node) => {
if TryInto::<B3Digest>::try_into(directory_node.digest.clone()).is_err()
{
return Err(ValidatePathInfoError::InvalidNodeDigestLen( return Err(ValidatePathInfoError::InvalidNodeDigestLen(
directory_node.digest.len(), directory_node.digest.len(),
)); ));
} }
// parse the name
parse_node_name_root(
&directory_node.name,
ValidatePathInfoError::InvalidNodeName,
)?
} }
Some(castorepb::node::Node::File(file_node)) => { // 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. // ensure the digest has the appropriate size.
if TryInto::<B3Digest>::try_into(file_node.digest.clone()).is_err() { if TryInto::<B3Digest>::try_into(file_node.digest.clone()).is_err() {
return Err(ValidatePathInfoError::InvalidNodeDigestLen( return Err(ValidatePathInfoError::InvalidNodeDigestLen(
file_node.digest.len(), file_node.digest.len(),
)); ));
} }
// parse the name
parse_node_name_root(&file_node.name, ValidatePathInfoError::InvalidNodeName)?
} }
Some(castorepb::node::Node::Symlink(symlink_node)) => { // nothing to do specifically for symlinks
// parse the name castorepb::node::Node::Symlink(_) => {}
parse_node_name_root( }
&symlink_node.name, // parse the name of the node itself and return
ValidatePathInfoError::InvalidNodeName, parse_node_name_root(&node.get_name(), ValidatePathInfoError::InvalidNodeName)?
)?
} }
}, },
}; };