From 559e4ce916e64257c7501212d76b671c6c6e0a63 Mon Sep 17 00:00:00 2001 From: Lachlan Deakin Date: Sun, 21 Jan 2024 18:21:43 +1100 Subject: [PATCH] `Node::{new_with_store->new}` and `Node::{new->new_with_metadata}` --- CHANGELOG.md | 3 +- examples/array_write_read.rs | 2 +- examples/async_array_write_read.rs | 2 +- examples/rectangular_array_write_read.rs | 2 +- examples/sharded_array_write_read.rs | 2 +- examples/zip_array_write_read.rs | 2 +- src/node.rs | 47 ++++++++++++++++++------ src/node/node_metadata.rs | 2 +- src/storage/storage_async.rs | 2 +- src/storage/storage_sync.rs | 2 +- tests/hierarchy.rs | 2 +- 11 files changed, 46 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 863b0956..755bf6ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added - - Tests for `ByteRange`, `BytesRepresentation`, `StorePrefix`, `StoreKey`, `ArrayBuilder`, `ArraySubset`, `GroupBuilder`, `Group`, `NodeName`, `NodePath` + - Tests for `ByteRange`, `BytesRepresentation`, `StorePrefix`, `StoreKey`, `ArrayBuilder`, `ArraySubset`, `GroupBuilder`, `Group`, `NodeName`, `NodePath`, `Node` - `array_subset::IncompatibleStartEndIndicesError` ### Changed @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **Breaking**: `ArraySubset::new_with_start_end_{inc,exc}` now return `IncompatibleStartEndIndicesError` instead of `IncompatibleDimensionalityError` - It is now an error if any element of `end` is less than `start` - Remove `#[must_use]` from `GroupBuilder::{attributes,additional_fields}` + - **Breaking**: Rename `Node::new_with_store` to `Node::new`, and `Node::new` to `Node::new_with_metadata` for consistency with `Array`/`Group` ### Removed - **Breaking**: Remove `StorePrefixError::new`, deprecated since `v0.7.3` diff --git a/examples/array_write_read.rs b/examples/array_write_read.rs index 7bb5cec3..f4b96277 100644 --- a/examples/array_write_read.rs +++ b/examples/array_write_read.rs @@ -136,7 +136,7 @@ fn array_write_read() -> Result<(), Box> { println!("retrieve_array_subset [2..6, 3..5]:\n{data_subset:+4.1}\n"); // Show the hierarchy - let node = Node::new_with_store(&*store, "/").unwrap(); + let node = Node::new(&*store, "/").unwrap(); let tree = node.hierarchy_tree(); println!("hierarchy_tree:\n{}", tree); diff --git a/examples/async_array_write_read.rs b/examples/async_array_write_read.rs index 7e811f72..40917e55 100644 --- a/examples/async_array_write_read.rs +++ b/examples/async_array_write_read.rs @@ -171,7 +171,7 @@ async fn async_array_write_read() -> Result<(), Box> { println!("async_retrieve_array_subset [2..6, 3..5]:\n{data_subset:+4.1}\n"); // Show the hierarchy - let node = Node::async_new_with_store(&*store, "/").await.unwrap(); + let node = Node::async_new(&*store, "/").await.unwrap(); let tree = node.hierarchy_tree(); println!("hierarchy_tree:\n{}", tree); diff --git a/examples/rectangular_array_write_read.rs b/examples/rectangular_array_write_read.rs index 267f0dda..2e18ae92 100644 --- a/examples/rectangular_array_write_read.rs +++ b/examples/rectangular_array_write_read.rs @@ -115,7 +115,7 @@ fn rectangular_array_write_read() -> Result<(), Box> { println!("The middle 4x2 subset is:\n{:?}\n", data_4x2); // Show the hierarchy - let node = Node::new_with_store(&*store, "/").unwrap(); + let node = Node::new(&*store, "/").unwrap(); let tree = node.hierarchy_tree(); println!("The zarr hierarchy tree is:\n{}", tree); diff --git a/examples/sharded_array_write_read.rs b/examples/sharded_array_write_read.rs index 6571df7b..29194009 100644 --- a/examples/sharded_array_write_read.rs +++ b/examples/sharded_array_write_read.rs @@ -146,7 +146,7 @@ fn sharded_array_write_read() -> Result<(), Box> { } // Show the hierarchy - let node = Node::new_with_store(&*store_readable_listable, "/").unwrap(); + let node = Node::new(&*store_readable_listable, "/").unwrap(); let tree = node.hierarchy_tree(); println!("The zarr hierarchy tree is:\n{}", tree); diff --git a/examples/zip_array_write_read.rs b/examples/zip_array_write_read.rs index eefff06f..85b43912 100644 --- a/examples/zip_array_write_read.rs +++ b/examples/zip_array_write_read.rs @@ -177,7 +177,7 @@ fn zip_array_write_read() -> Result<(), Box> { read_array_from_store(array)?; // Show the hierarchy - let node = Node::new_with_store(&*store, "/").unwrap(); + let node = Node::new(&*store, "/").unwrap(); let tree = node.hierarchy_tree(); println!("The zarr hierarchy tree is:\n{}", tree); diff --git a/src/node.rs b/src/node.rs index 4517186f..bd6c7892 100644 --- a/src/node.rs +++ b/src/node.rs @@ -58,22 +58,12 @@ pub enum NodeCreateError { } impl Node { - /// Create a new node at `path` with `metadata` and `children`. - #[must_use] - pub fn new(path: NodePath, metadata: NodeMetadata, children: Vec) -> Self { - Self { - path, - metadata, - children, - } - } - /// Create a new node at `path` and read metadata and children from `storage`. /// /// # Errors /// /// Returns [`NodeCreateError`] if metadata is invalid or there is a failure to list child nodes. - pub fn new_with_store( + pub fn new( storage: &TStorage, path: &str, ) -> Result { @@ -102,7 +92,7 @@ impl Node { /// # Errors /// /// Returns [`NodeCreateError`] if metadata is invalid or there is a failure to list child nodes. - pub async fn async_new_with_store< + pub async fn async_new< TStorage: ?Sized + AsyncReadableStorageTraits + AsyncListableStorageTraits, >( storage: &TStorage, @@ -127,6 +117,16 @@ impl Node { Ok(node) } + /// Create a new node at `path` with `metadata` and `children`. + #[must_use] + pub fn new_with_metadata(path: NodePath, metadata: NodeMetadata, children: Vec) -> Self { + Self { + path, + metadata, + children, + } + } + /// Indicates if a node is the root. #[must_use] pub fn is_root(&self) -> bool { @@ -187,6 +187,8 @@ impl Node { #[cfg(test)] mod tests { + use crate::{group::GroupMetadata, storage::store::MemoryStore}; + use super::*; #[test] @@ -259,4 +261,25 @@ mod tests { }"#; serde_json::from_str::(JSON_GROUP).unwrap(); } + + #[test] + fn node_default() { + let store = std::sync::Arc::new(MemoryStore::new()); + let node_path = "/node"; + let node = Node::new(&*store, node_path).unwrap(); + assert_eq!( + node.metadata, + NodeMetadata::Group(GroupMetadata::V3(GroupMetadataV3::default())) + ); + } + + #[test] + fn node_root() { + let node = Node::new_with_metadata( + NodePath::root(), + NodeMetadata::Group(GroupMetadata::V3(GroupMetadataV3::default())), + vec![], + ); + assert!(node.is_root()); + } } diff --git a/src/node/node_metadata.rs b/src/node/node_metadata.rs index 17b8defe..745e6582 100644 --- a/src/node/node_metadata.rs +++ b/src/node/node_metadata.rs @@ -1,7 +1,7 @@ use crate::{array::ArrayMetadata, group::GroupMetadata}; /// Node metadata ([`ArrayMetadata`] or [`GroupMetadata`]). -#[derive(serde::Serialize, serde::Deserialize, Debug)] +#[derive(serde::Serialize, serde::Deserialize, Debug, PartialEq)] #[serde(untagged)] pub enum NodeMetadata { /// Array metadata. diff --git a/src/storage/storage_async.rs b/src/storage/storage_async.rs index 0c9163c5..6e7e1233 100644 --- a/src/storage/storage_async.rs +++ b/src/storage/storage_async.rs @@ -311,7 +311,7 @@ pub async fn async_get_child_nodes< NodeMetadata::Array(_) => Vec::default(), NodeMetadata::Group(_) => async_get_child_nodes(storage, &path).await?, }; - nodes.push(Node::new(path, child_metadata, children)); + nodes.push(Node::new_with_metadata(path, child_metadata, children)); } Ok(nodes) } diff --git a/src/storage/storage_sync.rs b/src/storage/storage_sync.rs index 484584d0..75431491 100644 --- a/src/storage/storage_sync.rs +++ b/src/storage/storage_sync.rs @@ -283,7 +283,7 @@ pub fn get_child_nodes Vec::default(), NodeMetadata::Group(_) => get_child_nodes(storage, &path)?, }; - nodes.push(Node::new(path, child_metadata, children)); + nodes.push(Node::new_with_metadata(path, child_metadata, children)); } Ok(nodes) } diff --git a/tests/hierarchy.rs b/tests/hierarchy.rs index d9c21ada..8ffa14d8 100644 --- a/tests/hierarchy.rs +++ b/tests/hierarchy.rs @@ -5,7 +5,7 @@ fn hierarchy_tree() { let store = FilesystemStore::new("./tests/data/hierarchy.zarr") .unwrap() .sorted(); - let node = Node::new_with_store(&store, "/").unwrap(); + let node = Node::new(&store, "/").unwrap(); let tree = node.hierarchy_tree(); println!("{:?}", tree); assert_eq!(