use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CriterionDescriptor {
pub key: &'static str,
pub label: &'static str,
pub help: &'static str,
pub quantity: Option<&'static str>,
pub kind: CriterionKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum CriterionKind {
Value {
default: f64,
},
Band {
cuts: &'static [BandCut],
},
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BandCut {
pub key: &'static str,
pub label: &'static str,
pub default: f64,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn descriptors_serialize_in_wire_shape() {
let d = CriterionDescriptor {
key: "freeboard",
label: "Freeboard",
help: "Clearance kept below the rim.",
quantity: Some("depth"),
kind: CriterionKind::Value { default: 0.3 },
};
let json = serde_json::to_value(d).unwrap();
assert_eq!(json["quantity"], "depth");
assert_eq!(json["kind"]["type"], "value");
assert_eq!(json["kind"]["default"], 0.3);
let band = CriterionDescriptor {
key: "velocity",
label: "Velocity",
help: "Self-cleansing to erosive.",
quantity: Some("velocity"),
kind: CriterionKind::Band {
cuts: &[
BandCut {
key: "selfCleansing",
label: "Self-cleansing",
default: 0.6,
},
BandCut {
key: "erosive",
label: "Erosive",
default: 3.0,
},
],
},
};
let json = serde_json::to_value(band).unwrap();
assert_eq!(json["kind"]["type"], "band");
assert_eq!(json["kind"]["cuts"][1]["key"], "erosive");
}
}