applin/widget/
widget_enum.rs

1use crate::widget::{
2    BackButton, Button, Checkbox, CheckboxButton, Column, Empty, ErrorText, Form, FormButton,
3    FormSection, GroupedRowTable, Image, LastErrorText, NavButton, Scroll, Selector, Text,
4    Textfield,
5};
6use serde::{Deserialize, Serialize};
7use std::fmt::{Debug, Formatter};
8
9#[derive(Clone, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
10#[serde(tag = "typ")]
11pub enum Widget {
12    #[serde(rename = "back_button")]
13    BackButton(BackButton),
14    #[serde(rename = "button")]
15    Button(Button),
16    #[serde(rename = "checkbox")]
17    Checkbox(Checkbox),
18    #[serde(rename = "checkbox_button")]
19    CheckboxButton(CheckboxButton),
20    #[serde(rename = "column")]
21    Column(Column),
22    #[serde(rename = "empty")]
23    Empty(Empty),
24    #[serde(rename = "error_text")]
25    ErrorText(ErrorText),
26    #[serde(rename = "form")]
27    Form(Form),
28    #[serde(rename = "form_button")]
29    FormButton(FormButton),
30    #[serde(rename = "form_section")]
31    FormSection(FormSection),
32    #[serde(rename = "grouped_row_table")]
33    GroupedRowTable(GroupedRowTable),
34    #[serde(rename = "image")]
35    Image(Image),
36    #[serde(rename = "last_error_text")]
37    LastErrorText(LastErrorText),
38    #[serde(rename = "nav_button")]
39    NavButton(NavButton),
40    #[serde(rename = "scroll")]
41    Scroll(Scroll),
42    #[serde(rename = "selector")]
43    Selector(Selector),
44    #[serde(rename = "text")]
45    Text(Text),
46    #[serde(rename = "textfield")]
47    Textfield(Textfield),
48}
49impl Debug for Widget {
50    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
51        write!(
52            f,
53            "{}",
54            serde_json::to_string(self)
55                .unwrap_or_else(|_| "Widget<failed serializing>".to_string())
56        )
57    }
58}
59
60impl<A: Into<Widget>> From<Option<A>> for Widget {
61    fn from(value: Option<A>) -> Self {
62        match value {
63            Some(widget) => widget.into(),
64            None => Widget::Empty(Empty { id: String::new() }),
65        }
66    }
67}
68
69impl From<BackButton> for Widget {
70    fn from(src: BackButton) -> Self {
71        Widget::BackButton(src)
72    }
73}
74
75impl From<Button> for Widget {
76    fn from(src: Button) -> Self {
77        Widget::Button(src)
78    }
79}
80
81impl From<Checkbox> for Widget {
82    fn from(src: Checkbox) -> Self {
83        Widget::Checkbox(src)
84    }
85}
86
87impl From<CheckboxButton> for Widget {
88    fn from(src: CheckboxButton) -> Self {
89        Widget::CheckboxButton(src)
90    }
91}
92
93impl From<Column> for Widget {
94    fn from(src: Column) -> Self {
95        Widget::Column(src)
96    }
97}
98
99impl From<Empty> for Widget {
100    fn from(src: Empty) -> Self {
101        Widget::Empty(src)
102    }
103}
104
105impl From<ErrorText> for Widget {
106    fn from(src: ErrorText) -> Self {
107        Widget::ErrorText(src)
108    }
109}
110
111impl From<Form> for Widget {
112    fn from(src: Form) -> Self {
113        Widget::Form(src)
114    }
115}
116
117impl From<FormButton> for Widget {
118    fn from(src: FormButton) -> Self {
119        Widget::FormButton(src)
120    }
121}
122
123impl From<FormSection> for Widget {
124    fn from(src: FormSection) -> Self {
125        Widget::FormSection(src)
126    }
127}
128
129impl From<GroupedRowTable> for Widget {
130    fn from(src: GroupedRowTable) -> Self {
131        Widget::GroupedRowTable(src)
132    }
133}
134
135impl From<Image> for Widget {
136    fn from(src: Image) -> Self {
137        Widget::Image(src)
138    }
139}
140
141impl From<LastErrorText> for Widget {
142    fn from(src: LastErrorText) -> Self {
143        Widget::LastErrorText(src)
144    }
145}
146
147impl From<NavButton> for Widget {
148    fn from(src: NavButton) -> Self {
149        Widget::NavButton(src)
150    }
151}
152
153impl From<Scroll> for Widget {
154    fn from(src: Scroll) -> Self {
155        Widget::Scroll(src)
156    }
157}
158
159impl From<Selector> for Widget {
160    fn from(src: Selector) -> Self {
161        Widget::Selector(src)
162    }
163}
164
165impl From<Text> for Widget {
166    fn from(src: Text) -> Self {
167        Widget::Text(src)
168    }
169}
170
171impl From<Textfield> for Widget {
172    fn from(src: Textfield) -> Self {
173        Widget::Textfield(src)
174    }
175}
176
177impl From<BackButton> for Option<Widget> {
178    fn from(src: BackButton) -> Self {
179        Some(Widget::BackButton(src))
180    }
181}
182
183impl From<Button> for Option<Widget> {
184    fn from(src: Button) -> Self {
185        Some(Widget::Button(src))
186    }
187}
188
189impl From<Checkbox> for Option<Widget> {
190    fn from(src: Checkbox) -> Self {
191        Some(Widget::Checkbox(src))
192    }
193}
194
195impl From<CheckboxButton> for Option<Widget> {
196    fn from(src: CheckboxButton) -> Self {
197        Some(Widget::CheckboxButton(src))
198    }
199}
200
201impl From<Column> for Option<Widget> {
202    fn from(src: Column) -> Self {
203        Some(Widget::Column(src))
204    }
205}
206
207impl From<Empty> for Option<Widget> {
208    fn from(src: Empty) -> Self {
209        Some(Widget::Empty(src))
210    }
211}
212
213impl From<ErrorText> for Option<Widget> {
214    fn from(src: ErrorText) -> Self {
215        Some(Widget::ErrorText(src))
216    }
217}
218
219impl From<Form> for Option<Widget> {
220    fn from(src: Form) -> Self {
221        Some(Widget::Form(src))
222    }
223}
224
225impl From<FormButton> for Option<Widget> {
226    fn from(src: FormButton) -> Self {
227        Some(Widget::FormButton(src))
228    }
229}
230
231impl From<FormSection> for Option<Widget> {
232    fn from(src: FormSection) -> Self {
233        Some(Widget::FormSection(src))
234    }
235}
236
237impl From<GroupedRowTable> for Option<Widget> {
238    fn from(src: GroupedRowTable) -> Self {
239        Some(Widget::GroupedRowTable(src))
240    }
241}
242
243impl From<Image> for Option<Widget> {
244    fn from(src: Image) -> Self {
245        Some(Widget::Image(src))
246    }
247}
248
249impl From<LastErrorText> for Option<Widget> {
250    fn from(src: LastErrorText) -> Self {
251        Some(Widget::LastErrorText(src))
252    }
253}
254
255impl From<NavButton> for Option<Widget> {
256    fn from(src: NavButton) -> Self {
257        Some(Widget::NavButton(src))
258    }
259}
260
261impl From<Scroll> for Option<Widget> {
262    fn from(src: Scroll) -> Self {
263        Some(Widget::Scroll(src))
264    }
265}
266
267impl From<Text> for Option<Widget> {
268    fn from(src: Text) -> Self {
269        Some(Widget::Text(src))
270    }
271}
272
273impl From<Textfield> for Option<Widget> {
274    fn from(src: Textfield) -> Self {
275        Some(Widget::Textfield(src))
276    }
277}