2025-12-21 12:04:53 -07:00
|
|
|
use serde_json::{Value, json};
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
|
ModuleError, Result,
|
2025-12-23 23:09:05 -07:00
|
|
|
config::{ConfigStructField, InterfaceData, InterfaceStruct, TreeMessage},
|
2025-12-21 12:04:53 -07:00
|
|
|
warn,
|
|
|
|
|
};
|
2025-12-17 16:40:34 -07:00
|
|
|
|
2025-12-21 00:35:28 -07:00
|
|
|
pub type ConfigStructKeys = Vec<ConfigStructField>;
|
|
|
|
|
pub type ConfigStructValues = Vec<Value>;
|
2025-12-17 16:40:34 -07:00
|
|
|
|
2025-12-21 00:35:28 -07:00
|
|
|
pub struct Config {
|
|
|
|
|
keys: ConfigStructKeys,
|
|
|
|
|
values: ConfigStructValues,
|
|
|
|
|
}
|
2025-12-17 16:40:34 -07:00
|
|
|
|
2025-12-21 12:04:53 -07:00
|
|
|
impl Config {
|
|
|
|
|
pub fn new(keys: ConfigStructKeys) -> Self {
|
|
|
|
|
let values = keys
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|key| match key {
|
|
|
|
|
ConfigStructField::Header(_) => Value::Null,
|
|
|
|
|
ConfigStructField::Text(_) => Value::Null,
|
|
|
|
|
ConfigStructField::String { default, .. } => json!(default),
|
|
|
|
|
ConfigStructField::Integer { default, .. } => json!(default),
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
Self { keys, values }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn get(&mut self, message: TreeMessage) -> Result<TreeMessage> {
|
|
|
|
|
match message {
|
|
|
|
|
TreeMessage::State(InterfaceData::ConfigStruct(values)) => {
|
|
|
|
|
self.values = values;
|
|
|
|
|
Ok(TreeMessage::Success)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TreeMessage::RequestStruct => Ok(TreeMessage::Interface(
|
|
|
|
|
InterfaceStruct::ConfigStruct(self.keys.clone()),
|
|
|
|
|
)),
|
|
|
|
|
TreeMessage::RequestState => Ok(TreeMessage::State(InterfaceData::ConfigStruct(
|
|
|
|
|
self.values.clone(),
|
|
|
|
|
))),
|
|
|
|
|
TreeMessage::RequestStructAndValue => Ok(TreeMessage::InterfaceAndValue(
|
|
|
|
|
InterfaceStruct::ConfigStruct(self.keys.clone()),
|
|
|
|
|
InterfaceData::ConfigStruct(self.values.clone()),
|
|
|
|
|
)),
|
|
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
|
warn!("Tree got invalid message");
|
|
|
|
|
Err(ModuleError::Error("Invalid Request".into()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|