Skip to content

Commit

Permalink
Refactor GroupMetadata{V3,V2} constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Nov 10, 2024
1 parent be1727e commit 5020dfc
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 28 deletions.
26 changes: 14 additions & 12 deletions zarrs/src/array/array_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,18 +314,20 @@ impl ArrayBuilder {
self.bytes_to_bytes_codecs.clone(),
);

let array_metadata = ArrayMetadata::V3(ArrayMetadataV3::new(
self.shape.clone(),
self.data_type.metadata(),
self.chunk_grid.create_metadata(),
self.chunk_key_encoding.create_metadata(),
self.data_type.metadata_fill_value(&self.fill_value),
codec_chain.create_metadatas(),
self.attributes.clone(),
self.storage_transformers.create_metadatas(),
self.dimension_names.clone(),
self.additional_fields.clone(),
));
let array_metadata = ArrayMetadata::V3(
ArrayMetadataV3::new(
self.shape.clone(),
self.data_type.metadata(),
self.chunk_grid.create_metadata(),
self.chunk_key_encoding.create_metadata(),
self.data_type.metadata_fill_value(&self.fill_value),
codec_chain.create_metadatas(),
)
.with_attributes(self.attributes.clone())
.with_additional_fields(self.additional_fields.clone())
.with_dimension_names(self.dimension_names.clone())
.with_storage_transformers(self.storage_transformers.create_metadatas()),
);

Ok(Array {
storage,
Expand Down
12 changes: 8 additions & 4 deletions zarrs_metadata/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Changed
- **Breaking**: Mark `GroupMetadataV3` and `ArrayMetadataV3` as non-exhaustive
- **Breaking**: Bump MSRV to 1.77 (21 March, 2024)
- **Breaking**: Mark `GroupMetadataV3` and `ArrayMetadataV3` as non-exhaustive
- **Breaking**: Bump MSRV to 1.77 (21 March, 2024)
- Refactor `GroupMetadata{V3,V2}` constructors
- **Breaking**: `GroupMetadataV3::new()` is now parameter free
- Add `GroupMetadataV2::new()`
- Add `GroupMetadata{V3,V2}::with_{attributes,additional_fields}()`

## [0.1.0] - 2024-09-02

### Added
- Initial release
- Split from the `metadata` module of `zarrs` 0.17.0-dev
- Initial release
- Split from the `metadata` module of `zarrs` 0.17.0-dev

[unreleased]: https://github.com/LDeakin/zarrs/compare/zarrs_metadata-v0.1.0...HEAD
[0.1.0]: https://github.com/LDeakin/zarrs/releases/tag/zarrs_metadata-v0.1.0
35 changes: 35 additions & 0 deletions zarrs_metadata/src/v2/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,38 @@ pub struct GroupMetadataV2 {
#[serde(default, flatten)]
pub additional_fields: AdditionalFields,
}

impl Default for GroupMetadataV2 {
fn default() -> Self {
Self::new()
}
}

impl GroupMetadataV2 {
/// Create Zarr V2 group metadata.
#[must_use]
pub fn new() -> Self {
Self {
zarr_format: monostate::MustBe!(2u64),
attributes: serde_json::Map::new(),
additional_fields: AdditionalFields::default(),
}
}

/// Set the user attributes.
#[must_use]
pub fn with_attributes(
mut self,
attributes: serde_json::Map<String, serde_json::Value>,
) -> Self {
self.attributes = attributes;
self
}

/// Set the additional fields.
#[must_use]
pub fn with_additional_fields(mut self, additional_fields: AdditionalFields) -> Self {
self.additional_fields = additional_fields;
self
}
}
7 changes: 3 additions & 4 deletions zarrs_metadata/src/v2_to_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ use super::v3::array::data_type::DataTypeMetadataV3;
#[allow(clippy::too_many_lines)]
#[must_use]
pub fn group_metadata_v2_to_v3(group_metadata_v2: &GroupMetadataV2) -> GroupMetadataV3 {
GroupMetadataV3::new(
group_metadata_v2.attributes.clone(),
group_metadata_v2.additional_fields.clone(),
)
GroupMetadataV3::new()
.with_attributes(group_metadata_v2.attributes.clone())
.with_additional_fields(group_metadata_v2.additional_fields.clone())
}

/// An error conerting Zarr V3 array metadata to V3.
Expand Down
30 changes: 22 additions & 8 deletions zarrs_metadata/src/v3/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,36 @@ pub struct GroupMetadataV3 {

impl Default for GroupMetadataV3 {
fn default() -> Self {
Self::new(serde_json::Map::new(), AdditionalFields::default())
Self::new()
}
}

impl GroupMetadataV3 {
/// Create group metadata.
/// Create Zarr V3 group metadata.
#[must_use]
pub fn new(
attributes: serde_json::Map<String, serde_json::Value>,
additional_fields: AdditionalFields,
) -> Self {
pub fn new() -> Self {
Self {
zarr_format: monostate::MustBe!(3u64),
node_type: monostate::MustBe!("group"),
attributes,
additional_fields,
attributes: serde_json::Map::new(),
additional_fields: AdditionalFields::default(),
}
}

/// Set the user attributes.
#[must_use]
pub fn with_attributes(
mut self,
attributes: serde_json::Map<String, serde_json::Value>,
) -> Self {
self.attributes = attributes;
self
}

/// Set the additional fields.
#[must_use]
pub fn with_additional_fields(mut self, additional_fields: AdditionalFields) -> Self {
self.additional_fields = additional_fields;
self
}
}

0 comments on commit 5020dfc

Please sign in to comment.