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
use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashMap;
use std::sync::Mutex;
lazy_static! {
static ref INVALID_STYLE_MATCHER: Regex = Regex::new(r"(?i)[\s\{\}]").unwrap();
static ref CUSTOM: Mutex<HashMap<String, Vec<String>>> = Mutex::new(HashMap::new());
}
pub enum CustomStyleError {
InvalidSyntax,
}
pub fn add_style(name: &str, styles: &[&str]) -> Result<(), CustomStyleError> {
if INVALID_STYLE_MATCHER.is_match(&name) {
return Err(CustomStyleError::InvalidSyntax);
}
CUSTOM
.lock()
.unwrap()
.insert(name.to_string(), styles.iter().map(|c| c.to_string()).collect());
Ok(())
}
pub fn delete_styles(names: &[&str]) {
for name in names.iter() {
CUSTOM.lock().unwrap().remove(&name.to_string());
}
}
pub fn resolve_styles(names: &[&str]) -> Vec<String> {
let mut resolved: Vec<String> = vec![];
for name in names.iter() {
match CUSTOM.lock().unwrap().get(&name.to_string()) {
Some(styles) => {
resolved.extend_from_slice(styles);
}
None => resolved.push(name.to_string()),
}
}
resolved
}