zarrs_metadata 0.7.5

Zarr metadata support for the zarrs crate
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use std::fmt::Debug;

use serde::de::DeserializeOwned;
use serde::ser::SerializeMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{Configuration, ConfigurationError};

/// Zarr V3 generic metadata with a `name`, optional `configuration`, and optional `must_understand`.
///
/// Represents most fields in Zarr V3 array metadata (see [`ArrayMetadataV3`](crate::v3::ArrayMetadataV3)) which is either:
/// - a string name, or
/// - a JSON object with a required `name` field and optional `configuration` and `must_understand` fields.
///
/// `must_understand` is implicitly set to [`true`] if omitted.
/// See [ZEP0009](https://zarr.dev/zeps/draft/ZEP0009.html) for more information on this field and Zarr V3 extensions.
///
/// Note that metadata with an empty `configuration` will be serialised as `{"name":"...","configuration":{}}`, even though it *could* be simplified to a string representation.
/// This is to support compatibility with Zarr <3.1, which specified that array `codec` metadata must be a list of JSON objects.
/// Codec metadata can include strings since Zarr 3.1, but this may limit compatibility with older Zarr implementations.
///
/// ### Example Metadata
/// ```json
/// "bytes"
/// ```
///
/// ```json
/// {
///     "name": "bytes",
/// }
/// ```
///
/// ```json
/// {
///     "name": "bytes",
///     "configuration": {
///       "endian": "little"
///     }
/// }
/// ```
///
/// ```json
/// {
///     "name": "bytes",
///     "configuration": {
///       "endian": "little"
///     },
///     "must_understand": False
/// }
/// ```
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct MetadataV3 {
    name: String,
    configuration: Option<Configuration>,
    must_understand: bool,
}

impl From<MetadataV3> for Option<Configuration> {
    fn from(metadata: MetadataV3) -> Self {
        metadata.configuration
    }
}

impl TryFrom<&str> for MetadataV3 {
    type Error = serde_json::Error;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        serde_json::from_str(s)
    }
}

impl core::fmt::Display for MetadataV3 {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        if let Some(configuration) = &self.configuration {
            write!(
                f,
                "{} {}",
                self.name,
                serde_json::to_string(configuration).unwrap_or_default()
            )
        } else {
            write!(f, "{}", self.name)
        }
    }
}

impl serde::Serialize for MetadataV3 {
    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        if let Some(configuration) = &self.configuration {
            if configuration.is_empty() {
                let mut s = s.serialize_map(Some(1))?;
                s.serialize_entry("name", &self.name)?;
                s.end()
            } else {
                let mut s = s.serialize_map(Some(if self.must_understand { 2 } else { 3 }))?;
                s.serialize_entry("name", &self.name)?;
                s.serialize_entry("configuration", configuration)?;
                if !self.must_understand {
                    s.serialize_entry("must_understand", &false)?;
                }
                s.end()
            }
        } else {
            s.serialize_str(self.name.as_str())
        }
    }
}

fn default_must_understand() -> bool {
    true
}

impl<'de> serde::Deserialize<'de> for MetadataV3 {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(deny_unknown_fields)]
        struct MetadataNameConfiguration {
            name: String,
            #[serde(default)]
            configuration: Option<Configuration>,
            #[serde(default = "default_must_understand")]
            must_understand: bool,
        }

        #[derive(Deserialize)]
        #[serde(untagged)]
        enum MetadataIntermediate {
            Name(String),
            NameConfiguration(MetadataNameConfiguration),
        }

        let metadata = MetadataIntermediate::deserialize(d).map_err(|_| {
            serde::de::Error::custom(r#"Expected metadata "<name>" or {"name":"<name>"} or {"name":"<name>","configuration":{}}"#)
        })?;
        match metadata {
            MetadataIntermediate::Name(name) => Ok(Self {
                name,
                configuration: None,
                must_understand: true,
            }),
            MetadataIntermediate::NameConfiguration(metadata) => Ok(Self {
                name: metadata.name,
                configuration: metadata.configuration,
                must_understand: metadata.must_understand,
            }),
        }
    }
}

impl MetadataV3 {
    /// Create metadata from `name`.
    #[must_use]
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            configuration: None,
            must_understand: true,
        }
    }

    /// Create metadata from `name` and `configuration`.
    #[must_use]
    pub fn new_with_configuration(
        name: impl Into<String>,
        configuration: impl Into<Configuration>,
    ) -> Self {
        Self {
            name: name.into(),
            configuration: Some(configuration.into()),
            must_understand: true,
        }
    }

    /// Set the value of the `must_understand` field.
    #[must_use]
    pub fn with_must_understand(mut self, must_understand: bool) -> Self {
        self.must_understand = must_understand;
        self
    }

    /// Convert a serializable configuration to [`MetadataV3`].
    ///
    /// # Errors
    /// Returns [`serde_json::Error`] if `configuration` cannot be converted to [`MetadataV3`].
    pub fn new_with_serializable_configuration<TConfiguration: serde::Serialize>(
        name: String,
        configuration: &TConfiguration,
    ) -> Result<Self, serde_json::Error> {
        let configuration = serde_json::to_value(configuration)?;
        if let Value::Object(configuration) = configuration {
            Ok(Self::new_with_configuration(name, configuration))
        } else {
            Err(serde::ser::Error::custom(
                "the configuration cannot be serialized to a JSON struct",
            ))
        }
    }

    /// Try and convert [`Configuration`] to a specific serializable configuration.
    ///
    /// # Errors
    /// Returns a [`serde_json`] error if the metadata cannot be converted.
    pub fn to_typed_configuration<TConfiguration: DeserializeOwned>(
        &self,
    ) -> Result<TConfiguration, std::sync::Arc<serde_json::Error>> {
        if let Some(configuration) = &self.configuration {
            configuration.to_typed()
        } else {
            Configuration::default().to_typed()
        }
    }

    /// Try and convert [`MetadataV3`] to a serializable configuration.
    ///
    /// # Errors
    /// Returns a [`serde_json`] error if the metadata cannot be converted.
    // TODO: #[deprecated(since = "0.8.0", note = "Use .to_typed() instead")]
    pub fn to_configuration<TConfiguration: DeserializeOwned>(
        &self,
    ) -> Result<TConfiguration, ConfigurationError> {
        let err = |_| ConfigurationError::new(self.name.clone(), self.configuration.clone());
        let configuration = self.configuration.clone().unwrap_or_default();
        let value = serde_json::to_value(configuration).map_err(err)?;
        serde_json::from_value(value).map_err(err)
    }

    /// Returns the metadata `name`.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Mutate the metadata `name`.
    pub fn set_name(&mut self, name: String) -> &mut Self {
        self.name = name;
        self
    }

    /// Returns the metadata configuration.
    #[must_use]
    pub const fn configuration(&self) -> Option<&Configuration> {
        self.configuration.as_ref()
    }

    /// Return whether the metadata must be understood as indicated by the `must_understand` field.
    ///
    /// The `must_understand` field is implicitly `true` if omitted.
    #[must_use]
    pub fn must_understand(&self) -> bool {
        self.must_understand
    }

    /// Returns true if the configuration is none or an empty map.
    #[must_use]
    pub fn configuration_is_none_or_empty(&self) -> bool {
        self.configuration
            .as_ref()
            .map_or(true, |configuration| configuration.is_empty())
    }
}

/// A Zarr V3 additional field in array or group metadata.
///
/// A field that is not recognised / supported by `zarrs` will be considered an additional field.
/// Additional fields can be any JSON type.
/// An array / group cannot be created with an additional field, unless the additional field is an object with a `"must_understand": false` field.
///
/// ### Example additional field JSON
/// ```json
///  "unknown_field": {
///      "key": "value",
///      "must_understand": false
///  }
/// ```
/// ```json
///  "unsupported_field_1": {
///      "key": "value",
///      "must_understand": true
///  }
/// ```
/// ```json
///  "unsupported_field_2": {
///      "key": "value"
///  }
/// ```
/// ```json
///  "unsupported_field_3": []
/// ```
/// ```json
///  "unsupported_field_4": "test"
/// ```
#[derive(Clone, Eq, PartialEq, Debug, Default)]
pub struct AdditionalFieldV3 {
    field: Value,
    must_understand: bool,
}

impl AdditionalFieldV3 {
    /// Create a new additional field.
    #[must_use]
    pub fn new(field: impl Into<Value>, must_understand: bool) -> Self {
        Self {
            field: field.into(),
            must_understand,
        }
    }

    /// Return the underlying value.
    #[must_use]
    pub const fn as_value(&self) -> &Value {
        &self.field
    }

    /// Return the `must_understand` component of the additional field.
    #[must_use]
    pub const fn must_understand(&self) -> bool {
        self.must_understand
    }
}

impl Serialize for AdditionalFieldV3 {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match &self.field {
            Value::Object(object) => {
                let mut map = serializer.serialize_map(Some(object.len() + 1))?;
                map.serialize_entry("must_understand", &Value::Bool(self.must_understand))?;
                for (k, v) in object {
                    map.serialize_entry(k, v)?;
                }
                map.end()
            }
            _ => self.field.serialize(serializer),
        }
    }
}

impl<'de> serde::Deserialize<'de> for AdditionalFieldV3 {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let value = Value::deserialize(d)?;
        Ok(value.into())
    }
}

impl<T> From<T> for AdditionalFieldV3
where
    T: Into<Value>,
{
    fn from(field: T) -> Self {
        let mut value: Value = field.into();
        let must_understand = if let Some(object) = value.as_object_mut() {
            if let Some(Value::Bool(must_understand)) = object.remove("must_understand") {
                must_understand
            } else {
                true
            }
        } else {
            true
        };
        Self {
            must_understand,
            field: value,
        }
    }
}

/// Zarr V3 additional fields in array or group metadata.
// NOTE: It would be nice if this was just a serde_json::Map, but it only has implementations for `<String, Value>`.
pub type AdditionalFieldsV3 = std::collections::BTreeMap<String, AdditionalFieldV3>;

#[cfg(test)]
mod tests {
    use super::MetadataV3;

    #[test]
    fn metadata_must_understand_implicit_string() {
        let metadata = r#""test""#;
        let metadata: MetadataV3 = serde_json::from_str(metadata).unwrap();
        assert!(metadata.name() == "test");
        assert!(metadata.must_understand());
    }

    #[test]
    fn metadata_must_understand_implicit() {
        let metadata = r#"{
    "name": "test"
}"#;
        let metadata: MetadataV3 = serde_json::from_str(metadata).unwrap();
        assert!(metadata.name() == "test");
        assert!(metadata.must_understand());
    }

    #[test]
    fn metadata_must_understand_true() {
        let metadata = r#"{
    "name": "test",
    "must_understand": true
}"#;
        let metadata: MetadataV3 = serde_json::from_str(metadata).unwrap();
        assert!(metadata.name() == "test");
        assert!(metadata.must_understand());
    }

    #[test]
    fn metadata_must_understand_false() {
        let metadata = r#"{
    "name": "test",
    "must_understand": false
}"#;
        let metadata: MetadataV3 = serde_json::from_str(metadata).unwrap();
        assert!(metadata.name() == "test");
        assert!(!metadata.must_understand());
        assert_ne!(metadata, MetadataV3::new("test".to_string()));
        assert_eq!(
            metadata,
            MetadataV3::new("test".to_string()).with_must_understand(false)
        );
    }
}