diff --git a/zarrs_metadata/CHANGELOG.md b/zarrs_metadata/CHANGELOG.md index aa696cf9..4fcf69f3 100644 --- a/zarrs_metadata/CHANGELOG.md +++ b/zarrs_metadata/CHANGELOG.md @@ -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 diff --git a/zarrs_metadata/src/v2/group.rs b/zarrs_metadata/src/v2/group.rs index c6f141f2..026f5cee 100644 --- a/zarrs_metadata/src/v2/group.rs +++ b/zarrs_metadata/src/v2/group.rs @@ -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, + ) -> 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 + } +} diff --git a/zarrs_metadata/src/v2_to_v3.rs b/zarrs_metadata/src/v2_to_v3.rs index 312eaded..12448017 100644 --- a/zarrs_metadata/src/v2_to_v3.rs +++ b/zarrs_metadata/src/v2_to_v3.rs @@ -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. diff --git a/zarrs_metadata/src/v3/group.rs b/zarrs_metadata/src/v3/group.rs index 2375aafd..cb5dc70f 100644 --- a/zarrs_metadata/src/v3/group.rs +++ b/zarrs_metadata/src/v3/group.rs @@ -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, - 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, + ) -> 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 + } }