charming_fork_zephyr/component/
toolbox.rs

1use serde::Serialize;
2
3use crate::{datatype::CompositeValue, element::Orient};
4
5#[derive(Serialize)]
6#[serde(rename_all = "snake_case")]
7pub enum SaveAsImageType {
8    Png,
9    Jpg,
10    Svg,
11}
12
13#[derive(Serialize)]
14#[serde(rename_all = "camelCase")]
15pub struct SaveAsImage {
16    #[serde(skip_serializing_if = "Option::is_none")]
17    show: Option<bool>,
18
19    #[serde(rename = "type")]
20    #[serde(skip_serializing_if = "Option::is_none")]
21    type_: Option<SaveAsImageType>,
22
23    #[serde(skip_serializing_if = "Option::is_none")]
24    name: Option<String>,
25
26    #[serde(skip_serializing_if = "Option::is_none")]
27    background_color: Option<String>,
28}
29
30impl SaveAsImage {
31    pub fn new() -> Self {
32        Self {
33            show: None,
34            type_: None,
35            name: None,
36            background_color: None,
37        }
38    }
39
40    pub fn show(mut self, show: bool) -> Self {
41        self.show = Some(show);
42        self
43    }
44
45    pub fn type_(mut self, type_: SaveAsImageType) -> Self {
46        self.type_ = Some(type_);
47        self
48    }
49
50    pub fn name<S: Into<String>>(mut self, name: S) -> Self {
51        self.name = Some(name.into());
52        self
53    }
54
55    pub fn background_color<S: Into<String>>(mut self, background_color: S) -> Self {
56        self.background_color = Some(background_color.into());
57        self
58    }
59}
60
61#[derive(Serialize)]
62#[serde(rename_all = "camelCase")]
63pub struct Restore {
64    #[serde(skip_serializing_if = "Option::is_none")]
65    show: Option<bool>,
66
67    #[serde(skip_serializing_if = "Option::is_none")]
68    title: Option<String>,
69}
70
71impl Restore {
72    pub fn new() -> Self {
73        Self {
74            show: None,
75            title: None,
76        }
77    }
78
79    pub fn show(mut self, show: bool) -> Self {
80        self.show = Some(show);
81        self
82    }
83
84    pub fn title<S: Into<String>>(mut self, title: S) -> Self {
85        self.title = Some(title.into());
86        self
87    }
88}
89
90#[derive(Serialize)]
91#[serde(rename_all = "camelCase")]
92pub struct DataView {
93    #[serde(skip_serializing_if = "Option::is_none")]
94    show: Option<bool>,
95
96    #[serde(skip_serializing_if = "Option::is_none")]
97    title: Option<String>,
98
99    #[serde(skip_serializing_if = "Option::is_none")]
100    read_only: Option<bool>,
101}
102
103impl DataView {
104    pub fn new() -> Self {
105        Self {
106            show: None,
107            title: None,
108            read_only: None,
109        }
110    }
111
112    pub fn show(mut self, show: bool) -> Self {
113        self.show = Some(show);
114        self
115    }
116
117    pub fn title<S: Into<String>>(mut self, title: S) -> Self {
118        self.title = Some(title.into());
119        self
120    }
121
122    pub fn read_only(mut self, read_only: bool) -> Self {
123        self.read_only = Some(read_only);
124        self
125    }
126}
127
128#[derive(Serialize)]
129#[serde(rename_all = "snake_case")]
130pub enum MagicTypeType {
131    /// For line charts.
132    Line,
133    /// For bar charts.
134    Bar,
135    /// For stacked charts.
136    Stack,
137}
138
139impl From<&str> for MagicTypeType {
140    fn from(s: &str) -> Self {
141        match s {
142            "line" => Self::Line,
143            "bar" => Self::Bar,
144            "stack" => Self::Stack,
145            _ => panic!("Invalid magic type type: {}", s),
146        }
147    }
148}
149
150#[derive(Serialize)]
151#[serde(rename_all = "camelCase")]
152pub struct MagicType {
153    #[serde(skip_serializing_if = "Option::is_none")]
154    type_: Option<Vec<MagicTypeType>>,
155
156    #[serde(skip_serializing_if = "Option::is_none")]
157    title: Option<String>,
158}
159
160impl MagicType {
161    pub fn new() -> Self {
162        Self {
163            type_: None,
164            title: None,
165        }
166    }
167
168    pub fn type_(mut self, type_: Vec<MagicTypeType>) -> Self {
169        self.type_ = Some(type_);
170        self
171    }
172
173    pub fn title<S: Into<String>>(mut self, title: S) -> Self {
174        self.title = Some(title.into());
175        self
176    }
177}
178
179#[derive(Serialize)]
180#[serde(rename_all = "camelCase")]
181pub enum BrushType {
182    Rect,
183    Polygon,
184    LineX,
185    LineY,
186    Keep,
187    Clear,
188}
189
190#[derive(Serialize)]
191#[serde(rename_all = "camelCase")]
192pub struct Brush {
193    #[serde(skip_serializing_if = "Vec::is_empty")]
194    type_: Vec<BrushType>,
195}
196
197impl Brush {
198    pub fn new() -> Self {
199        Self { type_: vec![] }
200    }
201
202    pub fn type_(mut self, type_: Vec<BrushType>) -> Self {
203        self.type_ = type_;
204        self
205    }
206}
207
208#[derive(Serialize)]
209#[serde(rename_all = "camelCase")]
210pub struct ToolboxDataZoom {
211    #[serde(skip_serializing_if = "Option::is_none")]
212    y_axis_index: Option<CompositeValue>,
213}
214
215impl ToolboxDataZoom {
216    pub fn new() -> Self {
217        Self { y_axis_index: None }
218    }
219
220    pub fn y_axis_index<C: Into<CompositeValue>>(mut self, y_axis_index: C) -> Self {
221        self.y_axis_index = Some(y_axis_index.into());
222        self
223    }
224}
225
226#[derive(Serialize)]
227#[serde(rename_all = "camelCase")]
228pub struct Feature {
229    #[serde(skip_serializing_if = "Option::is_none")]
230    save_as_image: Option<SaveAsImage>,
231
232    #[serde(skip_serializing_if = "Option::is_none")]
233    restore: Option<Restore>,
234
235    #[serde(skip_serializing_if = "Option::is_none")]
236    data_view: Option<DataView>,
237
238    #[serde(skip_serializing_if = "Option::is_none")]
239    magic_type: Option<MagicType>,
240
241    #[serde(skip_serializing_if = "Option::is_none")]
242    data_zoom: Option<ToolboxDataZoom>,
243
244    #[serde(skip_serializing_if = "Option::is_none")]
245    brush: Option<Brush>,
246}
247
248impl Feature {
249    pub fn new() -> Self {
250        Self {
251            save_as_image: None,
252            restore: None,
253            data_view: None,
254            magic_type: None,
255            data_zoom: None,
256            brush: None,
257        }
258    }
259
260    pub fn save_as_image(mut self, save_as_image: SaveAsImage) -> Self {
261        self.save_as_image = Some(save_as_image);
262        self
263    }
264
265    pub fn restore(mut self, restore: Restore) -> Self {
266        self.restore = Some(restore);
267        self
268    }
269
270    pub fn data_view(mut self, data_view: DataView) -> Self {
271        self.data_view = Some(data_view);
272        self
273    }
274
275    pub fn magic_type(mut self, magic_type: MagicType) -> Self {
276        self.magic_type = Some(magic_type);
277        self
278    }
279
280    pub fn data_zoom(mut self, data_zoom: ToolboxDataZoom) -> Self {
281        self.data_zoom = Some(data_zoom);
282        self
283    }
284
285    pub fn brush(mut self, brush: Brush) -> Self {
286        self.brush = Some(brush);
287        self
288    }
289}
290
291#[derive(Serialize)]
292#[serde(rename_all = "camelCase")]
293pub struct Toolbox {
294    #[serde(skip_serializing_if = "Option::is_none")]
295    show: Option<bool>,
296
297    #[serde(skip_serializing_if = "Option::is_none")]
298    feature: Option<Feature>,
299
300    #[serde(skip_serializing_if = "Option::is_none")]
301    orient: Option<Orient>,
302
303    #[serde(skip_serializing_if = "Option::is_none")]
304    left: Option<CompositeValue>,
305
306    #[serde(skip_serializing_if = "Option::is_none")]
307    top: Option<CompositeValue>,
308
309    #[serde(skip_serializing_if = "Option::is_none")]
310    right: Option<CompositeValue>,
311
312    #[serde(skip_serializing_if = "Option::is_none")]
313    bottom: Option<CompositeValue>,
314}
315
316impl Toolbox {
317    pub fn new() -> Self {
318        Self {
319            show: None,
320            feature: None,
321            orient: None,
322            left: None,
323            top: None,
324            right: None,
325            bottom: None,
326        }
327    }
328
329    pub fn show(mut self, show: bool) -> Self {
330        self.show = Some(show);
331        self
332    }
333
334    pub fn feature<T: Into<Feature>>(mut self, feature: T) -> Self {
335        self.feature = Some(feature.into());
336        self
337    }
338
339    pub fn orient<O: Into<Orient>>(mut self, orient: O) -> Self {
340        self.orient = Some(orient.into());
341        self
342    }
343
344    pub fn left<C: Into<CompositeValue>>(mut self, left: C) -> Self {
345        self.left = Some(left.into());
346        self
347    }
348
349    pub fn top<C: Into<CompositeValue>>(mut self, top: C) -> Self {
350        self.top = Some(top.into());
351        self
352    }
353
354    pub fn right<C: Into<CompositeValue>>(mut self, right: C) -> Self {
355        self.right = Some(right.into());
356        self
357    }
358
359    pub fn bottom<C: Into<CompositeValue>>(mut self, bottom: C) -> Self {
360        self.bottom = Some(bottom.into());
361        self
362    }
363
364    pub fn save_as_image_type(&self) -> Option<&SaveAsImageType> {
365        self.feature
366            .as_ref()
367            .and_then(|f| f.save_as_image.as_ref())
368            .and_then(|s| s.type_.as_ref())
369    }
370}