use derive_more::From;
use serde::{Deserialize, Serialize};
use zarrs_metadata::ConfigurationSerialize;
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug, From)]
#[non_exhaustive]
#[serde(untagged)]
pub enum ZfpCodecConfiguration {
V1(ZfpCodecConfigurationV1),
}
impl ConfigurationSerialize for ZfpCodecConfiguration {}
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct ZfpCodecConfigurationV1 {
#[serde(flatten)]
pub mode: ZfpMode,
}
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq, Debug)]
#[serde(tag = "mode", rename_all = "snake_case")]
pub enum ZfpMode {
Expert {
minbits: u32,
maxbits: u32,
maxprec: u32,
minexp: i32,
},
FixedRate {
rate: f64,
},
FixedPrecision {
precision: u32,
},
FixedAccuracy {
tolerance: f64,
},
Reversible,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn codec_zfp_configuration_expert() {
const JSON: &'static str = r#"{
"mode": "expert",
"minbits": 1,
"maxbits": 12,
"maxprec": 10,
"minexp": -2
}"#;
serde_json::from_str::<ZfpCodecConfiguration>(JSON).unwrap();
}
#[test]
fn codec_zfp_configuration_fixed_rate() {
const JSON: &'static str = r#"{
"mode": "fixed_rate",
"rate": 12
}"#;
serde_json::from_str::<ZfpCodecConfiguration>(JSON).unwrap();
}
#[test]
fn codec_zfp_configuration_fixed_precision() {
const JSON: &'static str = r#"{
"mode": "fixed_precision",
"precision": 12
}"#;
serde_json::from_str::<ZfpCodecConfiguration>(JSON).unwrap();
}
#[test]
fn codec_zfp_configuration_fixed_accuracy() {
const JSON: &'static str = r#"{
"mode": "fixed_accuracy",
"tolerance": 0.001
}"#;
serde_json::from_str::<ZfpCodecConfiguration>(JSON).unwrap();
}
#[test]
fn codec_zfp_configuration_reversible() {
const JSON: &'static str = r#"{
"mode": "reversible"
}"#;
serde_json::from_str::<ZfpCodecConfiguration>(JSON).unwrap();
}
#[test]
fn codec_zfp_configuration_invalid2() {
const JSON_INVALID2: &'static str = r#"{
"mode": "unknown"
}"#;
assert!(serde_json::from_str::<ZfpCodecConfiguration>(JSON_INVALID2).is_err());
}
}