charming_fork_zephyr/series/
tree.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{
4    datatype::CompositeValue,
5    element::{Blur, Emphasis, ItemStyle, Label, Select, Symbol},
6};
7
8#[derive(Serialize)]
9#[serde(rename_all = "snake_case")]
10pub enum TreeLayout {
11    Orthogonal,
12    Radial,
13}
14
15#[derive(Serialize)]
16pub enum TreeOrient {
17    #[serde(rename = "LR")]
18    LeftRight,
19    #[serde(rename = "RL")]
20    RightLeft,
21    #[serde(rename = "TB")]
22    TopBottom,
23    #[serde(rename = "BT")]
24    BottomTop,
25}
26
27#[derive(Serialize)]
28#[serde(rename_all = "snake_case")]
29pub enum TreeEdgeShape {
30    Curve,
31    Polyline,
32}
33
34#[derive(Serialize)]
35#[serde(rename_all = "camelCase")]
36pub struct TreeLeaves {
37    #[serde(skip_serializing_if = "Option::is_none")]
38    label: Option<Label>,
39}
40
41impl TreeLeaves {
42    pub fn new() -> Self {
43        Self { label: None }
44    }
45
46    pub fn label(mut self, label: Label) -> Self {
47        self.label = Some(label);
48        self
49    }
50}
51
52#[derive(Serialize, Deserialize)]
53#[serde(rename_all = "camelCase")]
54pub struct TreeNode {
55    pub name: String,
56
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub value: Option<i64>,
59
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub collapsed: Option<bool>,
62
63    #[serde(skip_serializing_if = "Option::is_none")]
64    pub children: Option<Vec<TreeNode>>,
65}
66
67/// The tree diagram is mainly used to display the tree data structure.
68#[derive(Serialize)]
69#[serde(rename_all = "camelCase")]
70pub struct Tree {
71    #[serde(rename = "type")]
72    type_: String,
73
74    /// Component ID.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    id: Option<String>,
77
78    /// Component name.
79    #[serde(skip_serializing_if = "Option::is_none")]
80    name: Option<String>,
81
82    /// zlevel value of all graphical elements in the tree.
83    #[serde(skip_serializing_if = "Option::is_none")]
84    z_level: Option<u64>,
85
86    /// z value of all graphical elements in the tree.
87    #[serde(skip_serializing_if = "Option::is_none")]
88    z: Option<u64>,
89
90    #[serde(skip_serializing_if = "Option::is_none")]
91    left: Option<CompositeValue>,
92
93    #[serde(skip_serializing_if = "Option::is_none")]
94    top: Option<CompositeValue>,
95
96    #[serde(skip_serializing_if = "Option::is_none")]
97    right: Option<CompositeValue>,
98
99    #[serde(skip_serializing_if = "Option::is_none")]
100    bottom: Option<CompositeValue>,
101
102    #[serde(skip_serializing_if = "Option::is_none")]
103    width: Option<CompositeValue>,
104
105    #[serde(skip_serializing_if = "Option::is_none")]
106    height: Option<CompositeValue>,
107
108    #[serde(skip_serializing_if = "Option::is_none")]
109    center: Option<CompositeValue>,
110
111    #[serde(skip_serializing_if = "Option::is_none")]
112    zoom: Option<i64>,
113
114    #[serde(skip_serializing_if = "Option::is_none")]
115    layout: Option<TreeLayout>,
116
117    #[serde(skip_serializing_if = "Option::is_none")]
118    orient: Option<TreeOrient>,
119
120    #[serde(skip_serializing_if = "Option::is_none")]
121    symbol: Option<Symbol>,
122
123    #[serde(skip_serializing_if = "Option::is_none")]
124    symbol_size: Option<i64>,
125
126    #[serde(skip_serializing_if = "Option::is_none")]
127    symbol_rotate: Option<i64>,
128
129    #[serde(skip_serializing_if = "Option::is_none")]
130    symbol_keep_aspect: Option<bool>,
131
132    #[serde(skip_serializing_if = "Option::is_none")]
133    symbol_offset: Option<CompositeValue>,
134
135    #[serde(skip_serializing_if = "Option::is_none")]
136    edge_shape: Option<TreeEdgeShape>,
137
138    #[serde(skip_serializing_if = "Option::is_none")]
139    edge_fork_position: Option<String>,
140
141    #[serde(skip_serializing_if = "Option::is_none")]
142    roam: Option<bool>,
143
144    #[serde(skip_serializing_if = "Option::is_none")]
145    initial_tree_depth: Option<i64>,
146
147    #[serde(skip_serializing_if = "Option::is_none")]
148    item_style: Option<ItemStyle>,
149
150    #[serde(skip_serializing_if = "Option::is_none")]
151    label: Option<Label>,
152
153    #[serde(skip_serializing_if = "Option::is_none")]
154    emphasis: Option<Emphasis>,
155
156    #[serde(skip_serializing_if = "Option::is_none")]
157    blur: Option<Blur>,
158
159    #[serde(skip_serializing_if = "Option::is_none")]
160    select: Option<Select>,
161
162    #[serde(skip_serializing_if = "Option::is_none")]
163    selected_mode: Option<bool>,
164
165    #[serde(skip_serializing_if = "Option::is_none")]
166    expand_and_collapse: Option<bool>,
167
168    #[serde(skip_serializing_if = "Option::is_none")]
169    animation_duration: Option<i64>,
170
171    #[serde(skip_serializing_if = "Option::is_none")]
172    animation_duration_update: Option<i64>,
173
174    #[serde(skip_serializing_if = "Option::is_none")]
175    leaves: Option<TreeLeaves>,
176
177    #[serde(skip_serializing_if = "Vec::is_empty")]
178    data: Vec<TreeNode>,
179}
180
181impl Tree {
182    pub fn new() -> Self {
183        Self {
184            type_: "tree".into(),
185            id: None,
186            name: None,
187            z_level: None,
188            z: None,
189            left: None,
190            top: None,
191            right: None,
192            bottom: None,
193            width: None,
194            height: None,
195            center: None,
196            zoom: None,
197            layout: None,
198            orient: None,
199            symbol: None,
200            symbol_size: None,
201            symbol_rotate: None,
202            symbol_keep_aspect: None,
203            symbol_offset: None,
204            edge_shape: None,
205            edge_fork_position: None,
206            roam: None,
207            initial_tree_depth: None,
208            item_style: None,
209            label: None,
210            emphasis: None,
211            blur: None,
212            select: None,
213            selected_mode: None,
214            expand_and_collapse: None,
215            animation_duration: None,
216            animation_duration_update: None,
217            leaves: None,
218            data: vec![],
219        }
220    }
221
222    pub fn id<S: Into<String>>(mut self, id: S) -> Self {
223        self.id = Some(id.into());
224        self
225    }
226
227    pub fn name<S: Into<String>>(mut self, name: S) -> Self {
228        self.name = Some(name.into());
229        self
230    }
231
232    pub fn z_level(mut self, z_level: u64) -> Self {
233        self.z_level = Some(z_level);
234        self
235    }
236
237    pub fn z(mut self, z: u64) -> Self {
238        self.z = Some(z);
239        self
240    }
241
242    pub fn left<C: Into<CompositeValue>>(mut self, left: C) -> Self {
243        self.left = Some(left.into());
244        self
245    }
246
247    pub fn top<C: Into<CompositeValue>>(mut self, top: C) -> Self {
248        self.top = Some(top.into());
249        self
250    }
251
252    pub fn right<C: Into<CompositeValue>>(mut self, right: C) -> Self {
253        self.right = Some(right.into());
254        self
255    }
256
257    pub fn bottom<C: Into<CompositeValue>>(mut self, bottom: C) -> Self {
258        self.bottom = Some(bottom.into());
259        self
260    }
261
262    pub fn width<C: Into<CompositeValue>>(mut self, width: C) -> Self {
263        self.width = Some(width.into());
264        self
265    }
266
267    pub fn height<C: Into<CompositeValue>>(mut self, height: C) -> Self {
268        self.height = Some(height.into());
269        self
270    }
271
272    pub fn center<C: Into<CompositeValue>>(mut self, center: C) -> Self {
273        self.center = Some(center.into());
274        self
275    }
276
277    pub fn zoom<F: Into<i64>>(mut self, zoom: F) -> Self {
278        self.zoom = Some(zoom.into());
279        self
280    }
281
282    pub fn layout<T: Into<TreeLayout>>(mut self, layout: T) -> Self {
283        self.layout = Some(layout.into());
284        self
285    }
286
287    pub fn orient<T: Into<TreeOrient>>(mut self, orient: T) -> Self {
288        self.orient = Some(orient.into());
289        self
290    }
291
292    pub fn symbol<S: Into<Symbol>>(mut self, symbol: S) -> Self {
293        self.symbol = Some(symbol.into());
294        self
295    }
296
297    pub fn symbol_size<F: Into<i64>>(mut self, symbol_size: F) -> Self {
298        self.symbol_size = Some(symbol_size.into());
299        self
300    }
301
302    pub fn symbol_rotate<F: Into<i64>>(mut self, symbol_rotate: F) -> Self {
303        self.symbol_rotate = Some(symbol_rotate.into());
304        self
305    }
306
307    pub fn symbol_keep_aspect(mut self, symbol_keep_aspect: bool) -> Self {
308        self.symbol_keep_aspect = Some(symbol_keep_aspect);
309        self
310    }
311
312    pub fn symbol_offset<C: Into<CompositeValue>>(mut self, symbol_offset: C) -> Self {
313        self.symbol_offset = Some(symbol_offset.into());
314        self
315    }
316
317    pub fn edge_shape<T: Into<TreeEdgeShape>>(mut self, edge_shape: T) -> Self {
318        self.edge_shape = Some(edge_shape.into());
319        self
320    }
321
322    pub fn edge_fork_position<S: Into<String>>(mut self, edge_fork_position: S) -> Self {
323        self.edge_fork_position = Some(edge_fork_position.into());
324        self
325    }
326
327    pub fn roam(mut self, roam: bool) -> Self {
328        self.roam = Some(roam);
329        self
330    }
331
332    pub fn initial_tree_depth<F: Into<i64>>(mut self, initial_tree_depth: F) -> Self {
333        self.initial_tree_depth = Some(initial_tree_depth.into());
334        self
335    }
336
337    pub fn item_style<I: Into<ItemStyle>>(mut self, item_style: I) -> Self {
338        self.item_style = Some(item_style.into());
339        self
340    }
341
342    pub fn label<L: Into<Label>>(mut self, label: L) -> Self {
343        self.label = Some(label.into());
344        self
345    }
346
347    pub fn emphasis<E: Into<Emphasis>>(mut self, emphasis: E) -> Self {
348        self.emphasis = Some(emphasis.into());
349        self
350    }
351
352    pub fn blur<B: Into<Blur>>(mut self, blur: B) -> Self {
353        self.blur = Some(blur.into());
354        self
355    }
356
357    pub fn select<S: Into<Select>>(mut self, select: S) -> Self {
358        self.select = Some(select.into());
359        self
360    }
361
362    pub fn selected_mode(mut self, selected_mode: bool) -> Self {
363        self.selected_mode = Some(selected_mode);
364        self
365    }
366
367    pub fn expand_and_collapse(mut self, expand_and_collapse: bool) -> Self {
368        self.expand_and_collapse = Some(expand_and_collapse);
369        self
370    }
371
372    pub fn animation_duration<F: Into<i64>>(mut self, animation_duration: F) -> Self {
373        self.animation_duration = Some(animation_duration.into());
374        self
375    }
376
377    pub fn animation_duration_update<F: Into<i64>>(mut self, animation_duration_update: F) -> Self {
378        self.animation_duration_update = Some(animation_duration_update.into());
379        self
380    }
381
382    pub fn leaves<T: Into<TreeLeaves>>(mut self, leaves: T) -> Self {
383        self.leaves = Some(leaves.into());
384        self
385    }
386
387    pub fn data<T: Into<TreeNode>>(mut self, data: Vec<T>) -> Self {
388        self.data = data.into_iter().map(|t| t.into()).collect();
389        self
390    }
391}