use std::collections::BTreeSet;
use modem_device::config::{
ConfigGroup, ConfigValue, Effective, ParamClass, ParamKind, ParamSpec, Schema,
};
use zenkey::config::{ConfigSchema, ConfigView, ParamValue, ValueSource};
#[must_use]
pub fn kind(k: &ParamKind) -> zenkey::config::ParamKind {
match k {
ParamKind::Bool => zenkey::config::ParamKind::Bool,
ParamKind::Integer { min, max, unit } => zenkey::config::ParamKind::Integer {
min: *min,
max: *max,
unit: unit.map(str::to_string),
},
ParamKind::Text => zenkey::config::ParamKind::Text,
_ => zenkey::config::ParamKind::Text,
}
}
#[must_use]
pub fn class(c: ParamClass) -> zenkey::config::ParamClass {
match c {
ParamClass::Hot => zenkey::config::ParamClass::Hot,
ParamClass::Reach => zenkey::config::ParamClass::Reach,
ParamClass::Contract => zenkey::config::ParamClass::Contract,
_ => zenkey::config::ParamClass::Contract,
}
}
#[must_use]
pub fn spec(p: &ParamSpec) -> zenkey::config::ParamSpec {
let s = zenkey::config::ParamSpec::new(p.name, kind(&p.kind), p.description);
if p.sensitive { s.sensitive() } else { s }
}
#[must_use]
pub fn group(g: &ConfigGroup) -> zenkey::config::ConfigGroup {
g.parameters.iter().fold(
zenkey::config::ConfigGroup::new(g.name, class(g.class), g.description),
|acc, p| acc.with(spec(p)),
)
}
#[must_use]
pub fn schema(s: &Schema) -> ConfigSchema {
s.groups
.iter()
.fold(ConfigSchema::new(), |acc, g| acc.with(group(g)))
}
#[must_use]
pub fn value(v: &ConfigValue) -> ParamValue {
match v {
ConfigValue::Bool(b) => ParamValue::Bool(*b),
ConfigValue::Integer(i) => ParamValue::Integer(*i),
ConfigValue::Text(t) => ParamValue::Text(t.clone()),
_ => ParamValue::Text(v.to_string()),
}
}
#[must_use]
pub fn value_of(v: &ParamValue) -> ConfigValue {
match v {
ParamValue::Bool(b) => ConfigValue::Bool(*b),
ParamValue::Integer(i) => ConfigValue::Integer(*i),
ParamValue::Text(t) => ConfigValue::Text(t.clone()),
_ => ConfigValue::Text(v.to_string()),
}
}
#[derive(Clone, Debug, Default)]
pub struct Provenance<'a> {
pub startup: Option<&'a Effective>,
pub file_keys: Option<&'a BTreeSet<String>>,
pub runtime_keys: Option<&'a BTreeSet<String>>,
pub overlay_keys: Option<&'a BTreeSet<String>>,
pub revision: u64,
pub pending: Option<zenkey::config::PendingChange>,
}
#[must_use]
pub fn view(
device: &str,
schema_: &Schema,
effective: &Effective,
facts: &Provenance<'_>,
) -> ConfigView {
let empty_keys = BTreeSet::new();
let empty = Effective::default();
let startup = facts.startup.unwrap_or(&empty);
let file_keys = facts.file_keys.unwrap_or(&empty_keys);
let runtime_keys = facts.runtime_keys.unwrap_or(&empty_keys);
let overlay_keys = facts.overlay_keys.unwrap_or(&empty_keys);
let mut view = ConfigView::of(device, &schema(schema_));
view.revision = facts.revision;
view.pending = facts.pending.clone();
for g in &mut view.groups {
for p in &mut g.parameters {
if p.spec.sensitive {
continue;
}
let now = effective.get(&g.name).and_then(|m| m.get(&p.spec.name));
let then = startup.get(&g.name).and_then(|m| m.get(&p.spec.name));
p.value = now.map(value);
p.startup = then.map(value);
let in_file = schema_
.group(&g.name)
.and_then(|dg| dg.parameter(&p.spec.name))
.is_some_and(|dp| file_keys.contains(dp.key_in_file()));
let key = format!("{}/{}", g.name, p.spec.name);
let set_remotely = runtime_keys.contains(&key);
let from_overlay = overlay_keys.contains(&key);
p.source = now.map(|_| {
if set_remotely {
ValueSource::Runtime
} else if from_overlay {
ValueSource::Overlay
} else if in_file {
ValueSource::File
} else {
ValueSource::Default
}
});
}
}
view
}