use crate::v0_4::validate_unique_labels;
#[doc(inline)]
pub use crate::v0_4::{ImageLabelColor, ImageLabelProperties, ImageLabelSource, Labels};
use serde::{Deserialize, Serialize};
use validatrix::Validate;
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct ImageLabel {
pub colors: Option<Vec<ImageLabelColor>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<Vec<ImageLabelProperties>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<ImageLabelSource>,
}
impl Validate for ImageLabel {
fn validate_inner(&self, accum: &mut validatrix::Accumulator) {
if let Some(c) = self.colors.as_ref() {
accum.with_key("colors", |a| {
if c.is_empty() {
a.add_failure("empty");
}
validate_unique_labels(a, c.iter())
});
}
if let Some(p) = self.properties.as_ref() {
accum.with_key("properties", |a| {
if p.is_empty() {
a.add_failure("empty");
}
validate_unique_labels(a, p.iter());
});
}
}
}
impl From<crate::v0_4::ImageLabel> for ImageLabel {
fn from(value: crate::v0_4::ImageLabel) -> Self {
Self {
colors: value.colors,
properties: value.properties,
source: value.source,
}
}
}
#[cfg(test)]
mod tests {
use crate::v0_5::OmeZarrGroupMetadata;
use super::*;
#[test]
fn labels_color_properties() {
let json = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/ome-zarr/specifications/0.5/examples/label_strict/colors_properties.json"
));
let ome_metadata: OmeZarrGroupMetadata = serde_json::from_str(json).unwrap();
let _image_label: ImageLabel = ome_metadata.attributes.ome.image_label.unwrap();
}
}