Skip to content

Commit

Permalink
draft functions to get children of Group
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasmueboe committed Dec 21, 2024
1 parent 15358ee commit c5d7964
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion zarrs/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ use std::sync::Arc;

use derive_more::Display;
use thiserror::Error;
use zarrs_metadata::NodeMetadata;
use zarrs_storage::ListableStorageTraits;

use crate::{
array::{Array, ArrayCreateError},
config::{
global_config, MetadataConvertVersion, MetadataEraseVersion, MetadataRetrieveVersion,
},
Expand All @@ -40,7 +43,10 @@ use crate::{
v2_to_v3::group_metadata_v2_to_v3,
v3::{AdditionalFields, UnsupportedAdditionalFieldError},
},
node::{meta_key_v2_attributes, meta_key_v2_group, meta_key_v3, NodePath, NodePathError},
node::{
get_child_nodes, meta_key_v2_attributes, meta_key_v2_group, meta_key_v3, Node, NodePath,
NodePathError,
},
storage::{ReadableStorageTraits, StorageError, StorageHandle, WritableStorageTraits},
};

Expand Down Expand Up @@ -223,6 +229,33 @@ impl<TStorage: ?Sized + ReadableStorageTraits> Group<TStorage> {
}
}

impl<TStorage: ?Sized + ReadableStorageTraits + ListableStorageTraits> Group<TStorage> {
/// Return the children of the group
pub fn children(&self) -> Result<Vec<Node>, StorageError> {
get_child_nodes(&self.storage, &self.path)
}

Check warning on line 236 in zarrs/src/group.rs

View check run for this annotation

Codecov / codecov/patch

zarrs/src/group.rs#L234-L236

Added lines #L234 - L236 were not covered by tests

/// Return the children of the group that are [`Group`]s
pub fn child_groups(&self) -> Result<Vec<Self>, GroupCreateError> {
self.children()?
.into_iter()
.filter(|node| matches!(node.metadata(), NodeMetadata::Group(_)))
.map(|node| Group::open(self.storage.clone(), node.name().as_str()))
.collect()
}

Check warning on line 245 in zarrs/src/group.rs

View check run for this annotation

Codecov / codecov/patch

zarrs/src/group.rs#L239-L245

Added lines #L239 - L245 were not covered by tests
}

impl<TStorage: ?Sized + ReadableStorageTraits + ListableStorageTraits + 'static> Group<TStorage> {
/// Return the children of the group that are [`Array`]s
pub fn child_arrays(&self) -> Result<Vec<Array<TStorage>>, ArrayCreateError> {
self.children()?
.into_iter()
.filter(|node| matches!(node.metadata(), NodeMetadata::Array(_)))
.map(|node| Array::open(self.storage.clone(), node.name().as_str()))
.collect()
}

Check warning on line 256 in zarrs/src/group.rs

View check run for this annotation

Codecov / codecov/patch

zarrs/src/group.rs#L250-L256

Added lines #L250 - L256 were not covered by tests
}

#[cfg(feature = "async")]
impl<TStorage: ?Sized + AsyncReadableStorageTraits> Group<TStorage> {
/// Async variant of [`open`](Group::open).
Expand Down

0 comments on commit c5d7964

Please sign in to comment.