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
140
141
142
143
144
145
use chumsky::span::SimpleSpan;
use super::Engine;
use crate::{
color::parse::parse_css_color,
{
Error, ParseErrorKind, Value,
engine::{FORMATS, format_color},
},
};
impl Engine {
pub fn resolve_generic_color<'a>(
&self,
color: &Value,
format: &'a str,
format_value: bool,
span: SimpleSpan,
name: &str,
) -> Result<Value, Error> {
let color = match parse_css_color(&color.to_string()) {
Ok(v) => v,
Err(_) => {
return Err(Error::ResolveError {
span,
name: name.to_string(),
});
}
};
if format_value {
let res = match format_color(color, format) {
Some(v) => v,
None => {
return Err(Error::ParseError {
kind: ParseErrorKind::Keyword(crate::KeywordError::InvalidFormat {
formats: FORMATS,
}),
span,
name: name.to_string(),
});
}
};
Ok(Value::Ident(res.to_string()))
} else {
Ok(Value::Color(color))
}
}
pub fn resolve_path<'a, I>(
&self,
path: I,
format_value: bool,
span: SimpleSpan,
name: &str,
) -> Result<Value, Error>
where
I: IntoIterator<Item = &'a str> + Clone,
{
let mut iter = path.clone().into_iter().peekable();
let first = iter.next().ok_or(Error::ResolveError {
span,
name: name.to_string(),
})?;
let mut current = self
.runtime
.borrow()
.resolve_path(std::iter::once(first))
.or_else(|| self.context.data().get(first).cloned())
.ok_or(Error::ResolveError {
span,
name: name.to_string(),
})?;
while let Some(next_key) = iter.next() {
let next_key = if next_key.starts_with("_") {
next_key.strip_prefix("_").unwrap()
} else {
next_key
};
match current {
Value::Map(ref map) => {
if map.contains_key("color") {
let color = map.get("color").unwrap();
current =
self.resolve_generic_color(color, next_key, format_value, span, name)?;
} else {
current = map
.get(next_key)
.ok_or(Error::ResolveError {
span,
name: name.to_string(),
})?
.clone();
}
}
Value::LazyColor { color, .. } => {
current = if format_value {
Value::Ident(
format_color(color, next_key)
.ok_or(Error::ResolveError {
span,
name: name.to_string(),
})?
.to_string(),
)
} else {
Value::Color(color)
}
}
Value::Color(color) => {
current = if format_value {
Value::Ident(
format_color(color, next_key)
.ok_or(Error::ResolveError {
span,
name: name.to_string(),
})?
.to_string(),
)
} else {
Value::Color(color)
}
}
_ => {
return Err(Error::ResolveError {
span,
name: name.to_string(),
});
}
}
}
Ok(current)
}
pub fn get_format<'a>(&self, keywords: &[&'a str]) -> &'a str {
keywords
.last()
.expect("Could not get format from {keywords}")
}
}