use crate::modules::{
traits::transform::Transform,
types::property::Property,
utils::{get_name, to_string_vec}
};
use std::any::type_name;
use alright::commonly::KeyError;
use serde_json::{Map, Value};
impl<T: Transform > Default for Property<T> {
fn default() -> Self {
Self {
name: get_name(type_name::<Self>()),
context: Vec::new(),
cause: None,
other: Map::new(),
}
}
}
impl<T: Transform + Default> Property<T> {
pub fn record(&mut self, msg_list: &mut Vec<impl Into<String>>) {
self.context.append(&mut to_string_vec(msg_list));
}
pub fn add(&mut self, msg: impl Into<String>) {
self.context.push(msg.into());
}
pub fn context(&mut self, msg: impl Into<String>) {
self.add(msg.into());
}
pub fn update(
&mut self,
key: impl Into<String>,
value: impl Into<String>,
) -> Result<(), KeyError> {
let string_key: String = key.into();
if self.other.contains_key(&string_key.clone()) {
let mut err = KeyError { property: Box::new(Property::default()) };
err.property.context(format!("Duplicate Key: {string_key}"));
return Err(
err
)
}
self.other
.insert(string_key.into(), Value::String(value.into()));
Ok(())
}
}