Configurable Struct

This commit is contained in:
Michael Mikovsky
2025-12-21 12:04:53 -07:00
parent c7d66c5560
commit 78fda07ab2
11 changed files with 160 additions and 98 deletions
+48 -1
View File
@@ -1,5 +1,11 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use serde_json::{Value, json};
use crate::{
ModuleError, Result,
config::{InterfaceData, InterfaceStruct, TreeMessage},
warn,
};
pub type ConfigStructKeys = Vec<ConfigStructField>;
pub type ConfigStructValues = Vec<Value>;
@@ -9,6 +15,47 @@ pub struct Config {
values: ConfigStructValues,
}
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()))
}
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum ConfigStructField {
Header(String),
+10 -8
View File
@@ -1,5 +1,4 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{ModuleError, Result, config::config_struct};
@@ -12,13 +11,13 @@ pub trait Tree {
unimplemented!();
}
fn select_child(&self, child: &str, _message: TreeMessage) -> Result<TreeMessage>;
fn select_child(&mut self, child: &str, _message: TreeMessage) -> Result<TreeMessage>;
fn get_value(&self, _message: TreeMessage) -> TreeMessage {
unimplemented!()
}
fn get_path(&self, elements: &mut Vec<&str>, message: TreeMessage) -> Result<TreeMessage> {
fn get_path(&mut self, elements: &mut Vec<&str>, message: TreeMessage) -> Result<TreeMessage> {
if elements.is_empty() {
return if Self::is_folder() {
Ok(TreeMessage::Folder(self.get_children_string()))
@@ -38,7 +37,7 @@ pub trait Tree {
}
}
fn get(&self, path: &str, message: TreeMessage) -> Result<TreeMessage> {
fn get(&mut self, path: &str, message: TreeMessage) -> Result<TreeMessage> {
let mut path = if path.is_empty() {
Vec::new()
} else {
@@ -49,25 +48,28 @@ pub trait Tree {
}
}
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum TreeMessage {
RequestState,
RequestStruct,
RequestStructAndValue,
State(Value),
State(InterfaceData),
Interface(InterfaceStruct),
InterfaceAndValue(InterfaceStruct, InterfaceData),
Success,
Failure,
Folder(Vec<String>),
}
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum InterfaceStruct {
ConfigStruct(config_struct::ConfigStructKeys),
}
#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum InterfaceData {
ConfigStruct(config_struct::ConfigStructValues),
}
+24
View File
@@ -1,3 +1,7 @@
use std::fmt;
pub type Result<T> = std::result::Result<T, ModuleError>;
///Generic error type for module-related operations.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub enum ModuleError {
@@ -30,3 +34,23 @@ impl From<Box<dyn std::error::Error>> for ModuleError {
ModuleError::Error(value.to_string())
}
}
impl std::error::Error for ModuleError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
fn description(&self) -> &str {
"description() is deprecated; use Display"
}
fn cause(&self) -> Option<&dyn std::error::Error> {
Some(self)
}
}
impl fmt::Display for ModuleError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(format!("{:?}", self).as_str())
}
}
+1 -24
View File
@@ -5,34 +5,11 @@ mod error;
pub mod logger;
mod announcement;
use std::fmt::{self, Debug};
pub use error::ModuleError;
pub use error::{ModuleError, Result};
pub use announcement::Announcement;
pub type Result<T> = std::result::Result<T, ModuleError>;
impl std::error::Error for ModuleError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
fn description(&self) -> &str {
"description() is deprecated; use Display"
}
fn cause(&self) -> Option<&dyn std::error::Error> {
Some(self)
}
}
impl fmt::Display for ModuleError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(format!("{:?}", self).as_str())
}
}
// pub trait Component {
// fn name(&self) -> &'static str;
// // fn start_runtime(&self, manager: Arc<Mutex<Manager>>) -> Option<Box<dyn ModuleRuntime>>;