1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
use serde::{Deserialize, Serialize};
use super::rendered_element::{
button, column, container, image, row, svg, text, Action, RenderedElement,
};
use crate::Error;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ElementName {
Text(String),
Button(String),
Svg(String),
Image(String),
Container,
Row,
Column,
}
impl ElementName {
pub const ALL: &'static [Self; 7] = &[
Self::Text(String::new()),
Self::Button(String::new()),
Self::Svg(String::new()),
Self::Image(String::new()),
Self::Container,
Self::Row,
Self::Column,
];
pub fn handle_action(
&self,
element_tree: Option<&mut RenderedElement>,
action: Action,
) -> Result<Option<RenderedElement>, Error> {
let element = match self {
Self::Text(_) => text(""),
Self::Button(_) => button(""),
Self::Svg(_) => svg(""),
Self::Image(_) => image(""),
Self::Container => container(None),
Self::Row => row(None),
Self::Column => column(None),
};
match action {
Action::Stop | Action::Drop => Ok(None),
Action::AddNew => Ok(Some(element)),
Action::PushFront(id) => {
element_tree
.ok_or("the action was of kind `PushFront`, but no element tree was provided.")?
.find_by_id(id)
.ok_or(Error::NonExistentElement)?
.push_front(&element);
Ok(None)
}
Action::InsertAfter(parent_id, child_id) => {
element_tree
.ok_or(
"the action was of kind `InsertAfter`, but no element tree was provided.",
)?
.find_by_id(parent_id)
.ok_or(Error::NonExistentElement)?
.insert_after(child_id, &element);
Ok(None)
}
}
}
}
impl std::fmt::Display for ElementName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::Text(_) => "Text",
Self::Button(_) => "Button",
Self::Svg(_) => "SVG",
Self::Image(_) => "Image",
Self::Container => "Container",
Self::Row => "Row",
Self::Column => "Column",
}
)
}
}
|