use aylis_core::{ComponentId, PropValue, Ui};
use serde_json::{Map, Value};
use crate::value::{from_prop, to_prop};
pub fn prop_from_json(ui: &Ui, id: ComponentId, name: &str, value: &Value) -> Result<PropValue, String> {
if name == "name" {
return value.as_str().map(PropValue::from).ok_or_else(|| "\"name\" deve ser texto".to_string());
}
if name.contains('.') {
return value.as_f64().map(PropValue::Float).ok_or_else(|| format!("'{name}' deve ser número"));
}
let info = ui.prop_info(id, name).ok_or_else(|| {
let class = ui.class_name(id).unwrap_or("?");
format!("a classe {class} não tem a propriedade '{name}'")
})?;
to_prop(value, info.kind, ui.theme())
}
pub fn prop_to_json(ui: &Ui, value: &PropValue) -> Value {
match value {
PropValue::Component(Some(target)) => match ui.name(*target).filter(|n| !n.is_empty()) {
Some(_) => Value::String(ui.path_of(*target)),
None => Value::from(target.to_u64()),
},
other => from_prop(other),
}
}
pub fn set_props(ui: &mut Ui, id: ComponentId, props: &Map<String, Value>) -> Result<(), String> {
for (name, value) in props {
let prop = prop_from_json(ui, id, name, value)?;
ui.set_prop(id, name, prop).map_err(|e| e.to_string())?;
}
Ok(())
}