1use crate::prop_value::PropValue;
2use serde::{Deserialize, Serialize};
3use std::collections::BTreeMap;
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
10pub struct Surface {
11 pub root: SurfaceNode,
13}
14
15#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
28pub struct SurfaceNode {
29 #[serde(rename = "type")]
31 pub component_type: String,
32
33 pub props: BTreeMap<String, PropValue>,
35
36 pub children: Vec<SurfaceNode>,
38}
39
40impl Surface {
43 pub fn new(root: SurfaceNode) -> Self {
45 Self { root }
46 }
47
48 pub fn to_json(&self) -> String {
50 serde_json::to_string(self).expect("Surface serialization should never fail")
51 }
52
53 pub fn to_json_pretty(&self) -> String {
55 serde_json::to_string_pretty(self).expect("Surface serialization should never fail")
56 }
57}
58
59impl SurfaceNode {
60 pub fn new(component_type: impl Into<String>) -> Self {
62 Self {
63 component_type: component_type.into(),
64 props: BTreeMap::new(),
65 children: Vec::new(),
66 }
67 }
68
69 pub fn with_prop(mut self, key: impl Into<String>, value: PropValue) -> Self {
71 self.props.insert(key.into(), value);
72 self
73 }
74
75 pub fn with_child(mut self, child: SurfaceNode) -> Self {
77 self.children.push(child);
78 self
79 }
80
81 pub fn with_children(mut self, children: Vec<SurfaceNode>) -> Self {
83 self.children = children;
84 self
85 }
86
87 pub fn set_prop(&mut self, key: impl Into<String>, value: PropValue) {
89 self.props.insert(key.into(), value);
90 }
91
92 pub fn add_child(&mut self, child: SurfaceNode) {
94 self.children.push(child);
95 }
96}