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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use utils::{generic_erase, StylePropertyField};
use erased_serde::{Deserializer, deserialize};
use traits::TParseStyleMiddleware;
use std::collections::HashMap;
use serde_json::Value;
use std::boxed::Box;
use types::{
ParseStyleMiddleware,
PropertyParseType,
PropertyKeyInfo,
PropertyError,
SourceFormat,
ParseError,
Style,
Case,
};
pub struct StyleBuilder {
custom_middlewares: HashMap<String, Box<TParseStyleMiddleware>>,
default_middleware: Box<TParseStyleMiddleware>,
source_type: SourceFormat,
case: Case,
}
impl Default for StyleBuilder {
fn default() -> StyleBuilder {
StyleBuilder {
default_middleware: Box::new(ParseStyleMiddleware {}),
custom_middlewares: HashMap::default(),
source_type: SourceFormat::Json,
case: Case::Ignore,
}
}
}
fn get_property_key(key: String) -> Result<PropertyKeyInfo, ParseError> {
PropertyKeyInfo::new(key.as_str())
.map_err(|error| ParseError::PropertyError {
error,
})
}
fn valid_case(info: &PropertyKeyInfo, case: Case) -> Result<(), ParseError> {
if case == Case::Ignore || case == info.case {
Ok(())
} else {
Err(ParseError::PropertyError {
error: PropertyError::InvalidKeyCase {
key: info.name.clone(),
case,
}
})
}
}
impl StyleBuilder {
pub fn middleware(mut self, middleware: Box<TParseStyleMiddleware>) -> Self {
self.custom_middlewares.insert(middleware.name(), middleware);
self
}
pub fn case(mut self, case: Case) -> Self {
self.case = case;
self
}
pub fn source_type(mut self, source_type: SourceFormat) -> Self {
self.source_type = source_type;
self
}
#[rustfmt::skip]
pub fn parse_from_str(&mut self, source: &str) -> Result<Style, ParseError> {
let erased = generic_erase(source, self.source_type).and_then(|properties| {
let mut result = vec![];
for (key, value) in properties {
let key_info = get_property_key(key)?;
valid_case(&key_info, self.case)?;
result.push((key_info, value));
}
Ok(result)
})?;
self.parse_from_iter(erased.into_iter())
}
pub fn parse_from_de(&mut self, de: Box<Deserializer>) -> Result<Style, ParseError> {
let map: HashMap<String, Value> = deserialize(Box::leak(de)).map_err(|error| ParseError::InvalidSource {
source_type: self.source_type,
error: error.into(),
})?;
let mut erased: Vec<StylePropertyField> = vec![];
for (key, value) in map {
let key_info = get_property_key(key)?;
valid_case(&key_info, self.case)?;
let value = Box::new(Deserializer::erase(value));
erased.push((key_info, value));
}
self.parse_from_iter(erased.into_iter())
}
#[rustfmt::skip]
pub fn parse_from_iter<'a, I>(&mut self, iter: I) -> Result<Style, ParseError>
where
I: Iterator<Item = StylePropertyField<'a>>,
{
let mut style = Style::default();
make_initial_style_states!(style, [default, active, hover]);
for (key, value) in iter {
match key.key_type {
PropertyParseType::Default => {
self.default_middleware.process_value(key, value, &mut style)?;
}
PropertyParseType::Custom => {
self.custom_middlewares.get_mut(&key.name)
.ok_or(ParseError::MissingMiddleware { name: key.name.clone() })
.and_then(|middleware| middleware.process_value(key, value, &mut style))?;
}
_ => {}
}
}
Ok(style)
}
}