Skip to main content

ephemeris_forge_github/plan/
custom_properties.rs

1use std::collections::BTreeMap;
2
3use ephemeris_forge::{FieldDiff, Limitation, Report};
4
5use crate::PropertyValue;
6use crate::api::model::PropertyEntry;
7use crate::operation::custom_properties::Operation;
8
9pub(super) fn plan(
10    desired: Option<&BTreeMap<String, PropertyValue>>,
11    live: Option<&[PropertyEntry]>,
12    report: &mut Report,
13) -> Vec<Operation> {
14    let Some(desired) = desired else {
15        return Vec::new();
16    };
17    let Some(values) = live else {
18        if !desired.is_empty() {
19            report
20                .limitations
21                .push(Limitation::CustomPropertiesUnsupported);
22        }
23        return Vec::new();
24    };
25    let present: BTreeMap<&str, &PropertyValue> = values
26        .iter()
27        .map(|entry| (entry.property_name.as_str(), &entry.value))
28        .collect();
29    let mut changed = Vec::new();
30    for (name, value) in desired {
31        let current = present
32            .get(name.as_str())
33            .copied()
34            .unwrap_or(&PropertyValue::Unset);
35        let mut diff = FieldDiff::default();
36        if diff.check("value", current.same_as(value), current, value) {
37            changed.push(PropertyEntry {
38                property_name: name.clone(),
39                value: value.clone(),
40            });
41        }
42        diff.record(&format!("custom property {name}"), report);
43    }
44    if changed.is_empty() {
45        Vec::new()
46    } else {
47        vec![Operation::Set(changed)]
48    }
49}