use crate::prop_value::PropValue;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Surface {
pub root: SurfaceNode,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SurfaceNode {
#[serde(rename = "type")]
pub component_type: String,
pub props: BTreeMap<String, PropValue>,
pub children: Vec<SurfaceNode>,
}
impl Surface {
pub fn new(root: SurfaceNode) -> Self {
Self { root }
}
pub fn to_json(&self) -> String {
serde_json::to_string(self).expect("Surface serialization should never fail")
}
pub fn to_json_pretty(&self) -> String {
serde_json::to_string_pretty(self).expect("Surface serialization should never fail")
}
}
impl SurfaceNode {
pub fn new(component_type: impl Into<String>) -> Self {
Self {
component_type: component_type.into(),
props: BTreeMap::new(),
children: Vec::new(),
}
}
pub fn with_prop(mut self, key: impl Into<String>, value: PropValue) -> Self {
self.props.insert(key.into(), value);
self
}
pub fn with_child(mut self, child: SurfaceNode) -> Self {
self.children.push(child);
self
}
pub fn with_children(mut self, children: Vec<SurfaceNode>) -> Self {
self.children = children;
self
}
pub fn set_prop(&mut self, key: impl Into<String>, value: PropValue) {
self.props.insert(key.into(), value);
}
pub fn add_child(&mut self, child: SurfaceNode) {
self.children.push(child);
}
}