mod table;
use alloc::string::String;
use alloc::vec::Vec;
pub use table::{BETA_TYPES, TYPES};
use crate::types::WireProfile;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ArrayBounds {
pub min: Option<u64>,
pub max: Option<u64>,
}
impl ArrayBounds {
#[must_use]
pub const fn accepts(self, len: u64) -> bool {
let above = match self.min {
Some(m) => len >= m,
None => true,
};
let below = match self.max {
Some(m) => len <= m,
None => true,
};
above && below
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Scalar,
Id,
Object(&'static str),
ScalarArray(ArrayBounds),
IdArray(ArrayBounds),
ObjectArray(&'static str, ArrayBounds),
}
impl Kind {
#[must_use]
pub const fn bounds(self) -> Option<ArrayBounds> {
match self {
Kind::ScalarArray(b) | Kind::IdArray(b) | Kind::ObjectArray(_, b) => Some(b),
Kind::Scalar | Kind::Id | Kind::Object(_) => None,
}
}
#[must_use]
pub const fn is_id(self) -> bool {
matches!(self, Kind::Id | Kind::IdArray(_))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Property {
pub name: &'static str,
pub required: bool,
pub kind: Kind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TypeSpec {
pub name: &'static str,
pub message_type: Option<&'static str>,
pub properties: &'static [Property],
}
impl TypeSpec {
#[must_use]
pub fn property(&self, name: &str) -> Option<&'static Property> {
self.properties.iter().find(|p| p.name == name)
}
}
#[must_use]
pub fn type_spec(name: &str) -> Option<&'static TypeSpec> {
TYPES
.binary_search_by(|t| t.name.cmp(name))
.ok()
.and_then(|i| TYPES.get(i))
}
#[must_use]
pub fn type_spec_in(profile: WireProfile, name: &str) -> Option<&'static TypeSpec> {
match profile {
WireProfile::V0_0_2Beta => BETA_TYPES
.iter()
.find(|t| t.name == name)
.or_else(|| type_spec(name)),
WireProfile::V1_0_0 => type_spec(name),
}
}
#[must_use]
pub fn message_spec(message_type: &str) -> Option<&'static TypeSpec> {
TYPES.iter().find(|t| t.message_type == Some(message_type))
}
pub fn message_types() -> impl Iterator<Item = &'static str> {
TYPES.iter().filter_map(|t| t.message_type)
}
pub const MAX_PRUNE_DEPTH: usize = 16;
pub fn prune_unknown(
value: &mut serde_json::Value,
type_name: &str,
profile: WireProfile,
removed: &mut Vec<String>,
) -> usize {
prune_at(value, type_name, profile, "", removed, MAX_PRUNE_DEPTH)
}
fn prune_at(
value: &mut serde_json::Value,
type_name: &str,
profile: WireProfile,
path: &str,
removed: &mut Vec<String>,
depth: usize,
) -> usize {
let Some(depth) = depth.checked_sub(1) else {
return 0;
};
let Some(spec) = type_spec_in(profile, type_name) else {
return 0;
};
let Some(object) = value.as_object_mut() else {
return 0;
};
let mut count = 0;
let unknown: Vec<String> = object
.keys()
.filter(|k| k.as_str() != "message_type" && spec.property(k).is_none())
.cloned()
.collect();
for key in unknown {
removed.push(alloc::format!("{path}/{key}"));
object.remove(&key);
count += 1;
}
for property in spec.properties {
let Some(child) = object.get_mut(property.name) else {
continue;
};
let child_path = alloc::format!("{path}/{}", property.name);
match property.kind {
Kind::Object(inner) => {
count += prune_at(child, inner, profile, &child_path, removed, depth);
}
Kind::ObjectArray(inner, _) => {
if let Some(items) = child.as_array_mut() {
for (i, item) in items.iter_mut().enumerate() {
let item_path = alloc::format!("{child_path}/{i}");
count += prune_at(item, inner, profile, &item_path, removed, depth);
}
}
}
Kind::Scalar | Kind::Id | Kind::ScalarArray(_) | Kind::IdArray(_) => {}
}
}
count
}
#[cfg(test)]
mod tests {
use super::*;
use crate::message::MessageKind;
use alloc::string::ToString;
#[test]
fn the_table_covers_every_message_the_crate_claims() {
let mut from_schema: Vec<&str> = message_types().collect();
from_schema.sort_unstable();
let mut from_rust: Vec<&str> = MessageKind::ALL.iter().map(|k| k.as_str()).collect();
from_rust.sort_unstable();
assert_eq!(from_schema, from_rust);
}
#[test]
fn the_beta_overlay_is_the_one_place_the_two_tags_differ() {
for spec in BETA_TYPES {
let base = type_spec(spec.name).expect("an overlay type the main table lacks");
assert_ne!(
base.properties, spec.properties,
"{} is in the overlay but identical to v1.0.0",
spec.name
);
}
for spec in BETA_TYPES {
let base = type_spec(spec.name).expect("an overlay type");
for property in spec.properties {
if let Some(same) = base.property(property.name) {
assert_eq!(
same.kind.bounds(),
property.kind.bounds(),
"{}/{} has different array bounds per profile",
spec.name,
property.name
);
}
}
}
}
#[test]
fn a_property_a_profile_requires_is_not_pruned_out_of_it() {
let text = r#"{"message_type":"DDBC.SystemDescription","message_id":"m1",
"valid_from":"2019-08-24T14:15:22Z","actuators":[],
"present_demand_rate":{"start_of_range":0.0,"end_of_range":1.0},
"provides_average_demand_rate_forecast":false,"vendor":1}"#;
let mut value: serde_json::Value = serde_json::from_str(text).unwrap();
let mut removed = Vec::new();
let n = prune_unknown(
&mut value,
"DDBC.SystemDescription",
WireProfile::V0_0_2Beta,
&mut removed,
);
assert_eq!(n, 1, "only the vendor extension should go");
assert_eq!(removed, alloc::vec!["/vendor".to_string()]);
assert!(value.get("present_demand_rate").is_some());
let mut value: serde_json::Value = serde_json::from_str(text).unwrap();
let mut removed = Vec::new();
prune_unknown(
&mut value,
"DDBC.SystemDescription",
WireProfile::V1_0_0,
&mut removed,
);
assert!(removed.contains(&"/present_demand_rate".to_string()));
}
#[test]
fn types_are_sorted_so_lookup_can_binary_search() {
for pair in TYPES.windows(2) {
if let [a, b] = pair {
assert!(a.name < b.name, "{} should sort before {}", a.name, b.name);
}
}
assert!(type_spec("FRBC.Instruction").is_some());
assert!(type_spec("NumberRange").is_some());
assert!(type_spec("Nope").is_none());
}
#[test]
fn array_bounds_came_from_the_schema() {
let forecast = type_spec("PowerForecast").unwrap();
let elements = forecast.property("elements").unwrap();
assert_eq!(
elements.kind,
Kind::ObjectArray(
"PowerForecastElement",
ArrayBounds {
min: Some(1),
max: Some(288)
}
)
);
let details = type_spec("ResourceManagerDetails").unwrap();
assert_eq!(
details.property("roles").unwrap().kind.bounds(),
Some(ArrayBounds {
min: Some(1),
max: Some(3)
})
);
let constraints = type_spec("PEBC.PowerConstraints").unwrap();
assert_eq!(
constraints
.property("allowed_limit_ranges")
.unwrap()
.kind
.bounds()
.unwrap()
.min,
Some(2)
);
}
#[test]
fn required_flags_came_from_the_schema() {
let details = type_spec("ResourceManagerDetails").unwrap();
assert!(details.property("resource_id").unwrap().required);
assert!(!details.property("name").unwrap().required);
assert!(!details.property("currency").unwrap().required);
}
#[test]
fn pruning_removes_only_what_the_schema_does_not_define() {
let mut value: serde_json::Value = serde_json::from_str(
r#"{
"message_type": "FRBC.ActuatorStatus",
"message_id": "m1",
"actuator_id": "a1",
"active_operation_mode_id": "om1",
"operation_mode_factor": 0.5,
"vendor_extension": {"anything": 1}
}"#,
)
.unwrap();
let mut removed = Vec::new();
let n = prune_unknown(
&mut value,
"FRBC.ActuatorStatus",
WireProfile::V1_0_0,
&mut removed,
);
assert_eq!(n, 1);
assert_eq!(removed, alloc::vec!["/vendor_extension".to_string()]);
assert!(value.get("vendor_extension").is_none());
assert!(value.get("operation_mode_factor").is_some());
assert!(value.get("message_type").is_some(), "the tag must survive");
}
#[test]
fn pruning_stops_at_a_depth_a_peer_did_not_choose() {
const { assert!(MAX_PRUNE_DEPTH > 8) };
let mut value: serde_json::Value = serde_json::from_str(
r#"{
"message_type": "PPBC.PowerProfileDefinition",
"message_id": "m1", "id": "p1",
"start_time": "2024-01-01T00:00:00Z",
"end_time": "2024-01-02T00:00:00Z",
"power_sequences_containers": [
{"id": "c1", "power_sequences": [
{"id": "s1", "elements": [
{"duration": 900000, "power_values": [
{"value_expected": 1.0,
"commodity_quantity": "ELECTRIC.POWER.L1",
"surprise": 1}]}],
"is_interruptible": false, "abnormal_condition_only": false}]}]
}"#,
)
.unwrap();
let mut removed = Vec::new();
assert_eq!(
prune_unknown(
&mut value,
"PPBC.PowerProfileDefinition",
WireProfile::V1_0_0,
&mut removed
),
1
);
assert_eq!(
removed,
alloc::vec![
"/power_sequences_containers/0/power_sequences/0/elements/0/power_values/0/surprise"
.to_string()
]
);
}
#[test]
fn pruning_reaches_into_nested_objects_and_arrays() {
let mut value: serde_json::Value = serde_json::from_str(
r#"{
"message_type": "FRBC.SystemDescription",
"message_id": "m1",
"valid_from": "2019-08-24T14:15:22Z",
"actuators": [
{"id": "a1", "supported_commodities": ["ELECTRICITY"],
"operation_modes": [
{"id": "om1", "elements": [], "abnormal_condition_only": false,
"surprise": 1}
],
"transitions": [], "timers": [], "mystery": true}
],
"storage": {"provides_leakage_behaviour": false,
"provides_fill_level_target_profile": false,
"provides_usage_forecast": false,
"fill_level_range": {"start_of_range": 0, "end_of_range": 100,
"hidden": 2}}
}"#,
)
.unwrap();
let mut removed = Vec::new();
let n = prune_unknown(
&mut value,
"FRBC.SystemDescription",
WireProfile::V1_0_0,
&mut removed,
);
assert_eq!(n, 3, "{removed:?}");
removed.sort();
assert_eq!(
removed,
alloc::vec![
"/actuators/0/mystery".to_string(),
"/actuators/0/operation_modes/0/surprise".to_string(),
"/storage/fill_level_range/hidden".to_string(),
]
);
}
}