diff options
author | Florian Klink <flokli@flokli.de> | 2024-08-13T17·04+0300 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2024-08-13T18·39+0000 |
commit | c7845f3c882d0f1b215813bf0ef8231c2c03d7b6 (patch) | |
tree | 681774cbfa98e5021aa68b361bc937b88881b5c8 /tvix/castore/src/proto | |
parent | 2f4185ff1a54e1bdbaa062a9e4e1c8137d141762 (diff) |
refactor(tvix/castore): move *Node and Directory to crate root r/8486
*Node and Directory are types of the tvix-castore model, not the tvix DirectoryService model. A DirectoryService only happens to send Directories. Move types into individual files in a nodes/ subdirectory, as it's gotten too cluttered in a single file, and (re-)export all types from the crate root. This has the effect that we now cannot poke at private fields directly from other files inside `crate::directoryservice` (as it's not all in the same file anymore), but that's a good thing, it now forces us to go through the proper accessors. For the same reasons, we currently also need to introduce the `rename` functions on each *Node directly. A followup is gonna move the names out of the individual enum kinds, so we can better represent "unnamed nodes". Change-Id: Icdb34dcfe454c41c94f2396e8e99973d27db8418 Reviewed-on: https://cl.tvl.fyi/c/depot/+/12199 Reviewed-by: yuka <yuka@yuka.dev> Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/castore/src/proto')
-rw-r--r-- | tvix/castore/src/proto/mod.rs | 116 | ||||
-rw-r--r-- | tvix/castore/src/proto/tests/directory.rs | 22 |
2 files changed, 62 insertions, 76 deletions
diff --git a/tvix/castore/src/proto/mod.rs b/tvix/castore/src/proto/mod.rs index 7e98cd74c591..8f8f3c08347a 100644 --- a/tvix/castore/src/proto/mod.rs +++ b/tvix/castore/src/proto/mod.rs @@ -8,7 +8,7 @@ mod grpc_directoryservice_wrapper; pub use grpc_blobservice_wrapper::GRPCBlobServiceWrapper; pub use grpc_directoryservice_wrapper::GRPCDirectoryServiceWrapper; -use crate::directoryservice::NamedNode; +use crate::NamedNode; use crate::{B3Digest, ValidateDirectoryError, ValidateNodeError}; tonic::include_proto!("tvix.castore.v1"); @@ -82,22 +82,22 @@ fn update_if_lt_prev<'n>( Ok(()) } -impl TryFrom<&node::Node> for crate::directoryservice::Node { +impl TryFrom<&node::Node> for crate::Node { type Error = ValidateNodeError; - fn try_from(node: &node::Node) -> Result<crate::directoryservice::Node, ValidateNodeError> { + fn try_from(node: &node::Node) -> Result<crate::Node, ValidateNodeError> { Ok(match node { - node::Node::Directory(n) => crate::directoryservice::Node::Directory(n.try_into()?), - node::Node::File(n) => crate::directoryservice::Node::File(n.try_into()?), - node::Node::Symlink(n) => crate::directoryservice::Node::Symlink(n.try_into()?), + node::Node::Directory(n) => crate::Node::Directory(n.try_into()?), + node::Node::File(n) => crate::Node::File(n.try_into()?), + node::Node::Symlink(n) => crate::Node::Symlink(n.try_into()?), }) } } -impl TryFrom<&Node> for crate::directoryservice::Node { +impl TryFrom<&Node> for crate::Node { type Error = ValidateNodeError; - fn try_from(node: &Node) -> Result<crate::directoryservice::Node, ValidateNodeError> { + fn try_from(node: &Node) -> Result<crate::Node, ValidateNodeError> { match node { Node { node: None } => Err(ValidateNodeError::NoNodeSet), Node { node: Some(node) } => node.try_into(), @@ -105,13 +105,11 @@ impl TryFrom<&Node> for crate::directoryservice::Node { } } -impl TryFrom<&DirectoryNode> for crate::directoryservice::DirectoryNode { +impl TryFrom<&DirectoryNode> for crate::DirectoryNode { type Error = ValidateNodeError; - fn try_from( - node: &DirectoryNode, - ) -> Result<crate::directoryservice::DirectoryNode, ValidateNodeError> { - crate::directoryservice::DirectoryNode::new( + fn try_from(node: &DirectoryNode) -> Result<crate::DirectoryNode, ValidateNodeError> { + crate::DirectoryNode::new( node.name.clone(), node.digest.clone().try_into()?, node.size, @@ -119,21 +117,19 @@ impl TryFrom<&DirectoryNode> for crate::directoryservice::DirectoryNode { } } -impl TryFrom<&SymlinkNode> for crate::directoryservice::SymlinkNode { +impl TryFrom<&SymlinkNode> for crate::SymlinkNode { type Error = ValidateNodeError; - fn try_from( - node: &SymlinkNode, - ) -> Result<crate::directoryservice::SymlinkNode, ValidateNodeError> { - crate::directoryservice::SymlinkNode::new(node.name.clone(), node.target.clone()) + fn try_from(node: &SymlinkNode) -> Result<crate::SymlinkNode, ValidateNodeError> { + crate::SymlinkNode::new(node.name.clone(), node.target.clone()) } } -impl TryFrom<&FileNode> for crate::directoryservice::FileNode { +impl TryFrom<&FileNode> for crate::FileNode { type Error = ValidateNodeError; - fn try_from(node: &FileNode) -> Result<crate::directoryservice::FileNode, ValidateNodeError> { - crate::directoryservice::FileNode::new( + fn try_from(node: &FileNode) -> Result<crate::FileNode, ValidateNodeError> { + crate::FileNode::new( node.name.clone(), node.digest.clone().try_into()?, node.size, @@ -142,80 +138,70 @@ impl TryFrom<&FileNode> for crate::directoryservice::FileNode { } } -impl TryFrom<Directory> for crate::directoryservice::Directory { +impl TryFrom<Directory> for crate::Directory { type Error = ValidateDirectoryError; - fn try_from( - directory: Directory, - ) -> Result<crate::directoryservice::Directory, ValidateDirectoryError> { + fn try_from(directory: Directory) -> Result<crate::Directory, ValidateDirectoryError> { (&directory).try_into() } } -impl TryFrom<&Directory> for crate::directoryservice::Directory { +impl TryFrom<&Directory> for crate::Directory { type Error = ValidateDirectoryError; - fn try_from( - directory: &Directory, - ) -> Result<crate::directoryservice::Directory, ValidateDirectoryError> { - let mut dir = crate::directoryservice::Directory::new(); + fn try_from(directory: &Directory) -> Result<crate::Directory, ValidateDirectoryError> { + let mut dir = crate::Directory::new(); let mut last_file_name: &[u8] = b""; for file in directory.files.iter().map(move |file| { update_if_lt_prev(&mut last_file_name, &file.name).map(|()| file.clone()) }) { let file = file?; - dir.add(crate::directoryservice::Node::File( - (&file) - .try_into() - .map_err(|e| ValidateDirectoryError::InvalidNode(file.name.into(), e))?, - ))?; + dir.add(crate::Node::File((&file).try_into().map_err(|e| { + ValidateDirectoryError::InvalidNode(file.name.into(), e) + })?))?; } let mut last_directory_name: &[u8] = b""; for directory in directory.directories.iter().map(move |directory| { update_if_lt_prev(&mut last_directory_name, &directory.name).map(|()| directory.clone()) }) { let directory = directory?; - dir.add(crate::directoryservice::Node::Directory( - (&directory) - .try_into() - .map_err(|e| ValidateDirectoryError::InvalidNode(directory.name.into(), e))?, - ))?; + dir.add(crate::Node::Directory((&directory).try_into().map_err( + |e| ValidateDirectoryError::InvalidNode(directory.name.into(), e), + )?))?; } let mut last_symlink_name: &[u8] = b""; for symlink in directory.symlinks.iter().map(move |symlink| { update_if_lt_prev(&mut last_symlink_name, &symlink.name).map(|()| symlink.clone()) }) { let symlink = symlink?; - dir.add(crate::directoryservice::Node::Symlink( - (&symlink) - .try_into() - .map_err(|e| ValidateDirectoryError::InvalidNode(symlink.name.into(), e))?, - ))?; + dir.add(crate::Node::Symlink((&symlink).try_into().map_err( + |e| ValidateDirectoryError::InvalidNode(symlink.name.into(), e), + )?))?; } Ok(dir) } } -impl From<&crate::directoryservice::Node> for node::Node { - fn from(node: &crate::directoryservice::Node) -> node::Node { +impl From<&crate::Node> for node::Node { + fn from(node: &crate::Node) -> node::Node { match node { - crate::directoryservice::Node::Directory(n) => node::Node::Directory(n.into()), - crate::directoryservice::Node::File(n) => node::Node::File(n.into()), - crate::directoryservice::Node::Symlink(n) => node::Node::Symlink(n.into()), + crate::Node::Directory(n) => node::Node::Directory(n.into()), + crate::Node::File(n) => node::Node::File(n.into()), + crate::Node::Symlink(n) => node::Node::Symlink(n.into()), } } } -impl From<&crate::directoryservice::Node> for Node { - fn from(node: &crate::directoryservice::Node) -> Node { +impl From<&crate::Node> for Node { + fn from(node: &crate::Node) -> Node { Node { node: Some(node.into()), } } } -impl From<&crate::directoryservice::DirectoryNode> for DirectoryNode { - fn from(node: &crate::directoryservice::DirectoryNode) -> DirectoryNode { +impl From<&crate::DirectoryNode> for DirectoryNode { + fn from(node: &crate::DirectoryNode) -> DirectoryNode { DirectoryNode { digest: node.digest().clone().into(), size: node.size(), @@ -224,8 +210,8 @@ impl From<&crate::directoryservice::DirectoryNode> for DirectoryNode { } } -impl From<&crate::directoryservice::FileNode> for FileNode { - fn from(node: &crate::directoryservice::FileNode) -> FileNode { +impl From<&crate::FileNode> for FileNode { + fn from(node: &crate::FileNode) -> FileNode { FileNode { digest: node.digest().clone().into(), size: node.size(), @@ -235,8 +221,8 @@ impl From<&crate::directoryservice::FileNode> for FileNode { } } -impl From<&crate::directoryservice::SymlinkNode> for SymlinkNode { - fn from(node: &crate::directoryservice::SymlinkNode) -> SymlinkNode { +impl From<&crate::SymlinkNode> for SymlinkNode { + fn from(node: &crate::SymlinkNode) -> SymlinkNode { SymlinkNode { name: node.get_name().clone(), target: node.target().clone(), @@ -244,26 +230,26 @@ impl From<&crate::directoryservice::SymlinkNode> for SymlinkNode { } } -impl From<crate::directoryservice::Directory> for Directory { - fn from(directory: crate::directoryservice::Directory) -> Directory { +impl From<crate::Directory> for Directory { + fn from(directory: crate::Directory) -> Directory { (&directory).into() } } -impl From<&crate::directoryservice::Directory> for Directory { - fn from(directory: &crate::directoryservice::Directory) -> Directory { +impl From<&crate::Directory> for Directory { + fn from(directory: &crate::Directory) -> Directory { let mut directories = vec![]; let mut files = vec![]; let mut symlinks = vec![]; for node in directory.nodes() { match node { - crate::directoryservice::Node::File(n) => { + crate::Node::File(n) => { files.push(n.into()); } - crate::directoryservice::Node::Directory(n) => { + crate::Node::Directory(n) => { directories.push(n.into()); } - crate::directoryservice::Node::Symlink(n) => { + crate::Node::Symlink(n) => { symlinks.push(n.into()); } } diff --git a/tvix/castore/src/proto/tests/directory.rs b/tvix/castore/src/proto/tests/directory.rs index 78b2cf7668e3..7847b9c4c9cb 100644 --- a/tvix/castore/src/proto/tests/directory.rs +++ b/tvix/castore/src/proto/tests/directory.rs @@ -147,7 +147,7 @@ fn digest() { #[test] fn validate_empty() { let d = Directory::default(); - assert!(crate::directoryservice::Directory::try_from(d).is_ok()); + assert!(crate::Directory::try_from(d).is_ok()); } #[test] @@ -161,7 +161,7 @@ fn validate_invalid_names() { }], ..Default::default() }; - match crate::directoryservice::Directory::try_from(d).expect_err("must fail") { + match crate::Directory::try_from(d).expect_err("must fail") { ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName(_)) => { assert_eq!(n, b"") } @@ -178,7 +178,7 @@ fn validate_invalid_names() { }], ..Default::default() }; - match crate::directoryservice::Directory::try_from(d).expect_err("must fail") { + match crate::Directory::try_from(d).expect_err("must fail") { ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName(_)) => { assert_eq!(n, b".") } @@ -196,7 +196,7 @@ fn validate_invalid_names() { }], ..Default::default() }; - match crate::directoryservice::Directory::try_from(d).expect_err("must fail") { + match crate::Directory::try_from(d).expect_err("must fail") { ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName(_)) => { assert_eq!(n, b"..") } @@ -212,7 +212,7 @@ fn validate_invalid_names() { }], ..Default::default() }; - match crate::directoryservice::Directory::try_from(d).expect_err("must fail") { + match crate::Directory::try_from(d).expect_err("must fail") { ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName(_)) => { assert_eq!(n, b"\x00") } @@ -228,7 +228,7 @@ fn validate_invalid_names() { }], ..Default::default() }; - match crate::directoryservice::Directory::try_from(d).expect_err("must fail") { + match crate::Directory::try_from(d).expect_err("must fail") { ValidateDirectoryError::InvalidNode(n, ValidateNodeError::InvalidName(_)) => { assert_eq!(n, b"foo/bar") } @@ -247,7 +247,7 @@ fn validate_invalid_digest() { }], ..Default::default() }; - match crate::directoryservice::Directory::try_from(d).expect_err("must fail") { + match crate::Directory::try_from(d).expect_err("must fail") { ValidateDirectoryError::InvalidNode(_, ValidateNodeError::InvalidDigestLen(n)) => { assert_eq!(n, 2) } @@ -274,7 +274,7 @@ fn validate_sorting() { ], ..Default::default() }; - match crate::directoryservice::Directory::try_from(d).expect_err("must fail") { + match crate::Directory::try_from(d).expect_err("must fail") { ValidateDirectoryError::WrongSorting(s) => { assert_eq!(s, b"a"); } @@ -299,7 +299,7 @@ fn validate_sorting() { ], ..Default::default() }; - match crate::directoryservice::Directory::try_from(d).expect_err("must fail") { + match crate::Directory::try_from(d).expect_err("must fail") { ValidateDirectoryError::DuplicateName(s) => { assert_eq!(s, b"a"); } @@ -325,7 +325,7 @@ fn validate_sorting() { ..Default::default() }; - crate::directoryservice::Directory::try_from(d).expect("validate shouldn't error"); + crate::Directory::try_from(d).expect("validate shouldn't error"); } // [b, c] and [a] are both properly sorted. @@ -350,6 +350,6 @@ fn validate_sorting() { ..Default::default() }; - crate::directoryservice::Directory::try_from(d).expect("validate shouldn't error"); + crate::Directory::try_from(d).expect("validate shouldn't error"); } } |