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
use utils::{apperance_keys_contains, layout_keys_contains};
use types::{Case, PropertyError};
use inflector::Inflector;
#[derive(Clone, Debug, PartialEq)]
pub enum PropertyParseType {
Expression,
Default,
Custom,
}
#[derive(Clone, Debug, PartialEq)]
pub enum FieldParseType {
Variable,
Default,
Custom,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PropertyKeyInfo {
pub key_type: PropertyParseType,
pub source: String,
pub name: String,
pub case: Case,
}
impl PropertyKeyInfo {
pub fn new(key: &str) -> Result<PropertyKeyInfo, PropertyError> {
let prefix = match &key[..1] {
"~" => Ok(PropertyParseType::Expression),
"@" => Ok(PropertyParseType::Custom),
_ => {
let snake_key = key.to_snake_case();
if apperance_keys_contains(snake_key.as_str()) || layout_keys_contains(snake_key.as_str()) {
Ok(PropertyParseType::Default)
} else {
Err(PropertyError::InvalidKey {
key: key.to_string(),
})
}
}
}?;
let name = if prefix != PropertyParseType::Default {
key[1..].to_string()
} else {
key.to_string()
};
Ok(PropertyKeyInfo {
case: Case::new(name.as_str()),
source: key.to_string(),
key_type: prefix,
name,
})
}
}
pub struct StylesheetFieldInfo {
pub key_type: FieldParseType,
pub source: String,
pub name: String,
pub case: Case,
}
impl StylesheetFieldInfo {
pub fn new(key: &str) -> StylesheetFieldInfo {
let prefix = match &key[..1] {
"$" => FieldParseType::Variable,
"@" => FieldParseType::Custom,
_ => FieldParseType::Default,
};
let name = if prefix != FieldParseType::Default {
key[1..].to_string()
} else {
key.to_string()
};
StylesheetFieldInfo {
case: Case::new(name.as_str()),
source: key.to_string(),
key_type: prefix,
name,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_key_info() {
let default = extract!(Ok(_), PropertyKeyInfo::new("background"));
let custom = extract!(Ok(_), PropertyKeyInfo::new("@custom"));
let expr = extract!(Ok(_), PropertyKeyInfo::new("~expr_value"));
let exp_default = Some(PropertyKeyInfo {
key_type: PropertyParseType::Default,
source: "background".to_string(),
name: "background".to_string(),
case: Case::Snake,
});
let exp_custom = Some(PropertyKeyInfo {
key_type: PropertyParseType::Custom,
source: "@custom".to_string(),
name: "custom".to_string(),
case: Case::Snake,
});
let exp_expr = Some(PropertyKeyInfo {
key_type: PropertyParseType::Expression,
source: "~expr_value".to_string(),
name: "expr_value".to_string(),
case: Case::Snake,
});
assert_eq!(default, exp_default);
assert_eq!(custom, exp_custom);
assert_eq!(expr, exp_expr);
}
}