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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#![allow(dead_code)]
use core::fmt;
#[cfg(feature = "fast-hash")]
use fx_hash::FxHashMap as HashMap;
use serde_jsonc2::Value;
use serde_jsonc2::from_str;
#[cfg(feature = "theme_yml")]
use serde_yml::{Value as YamlValue, from_str as from_yml_str, to_string as yml_to_string};
#[cfg(not(feature = "fast-hash"))]
use std::collections::HashMap;
use std::ops::Deref;
use std::ops::DerefMut;
/// Represents a color as a string.
#[derive(Clone, Debug)]
pub struct Color(String);
impl Color {
/// Constructs a new `Color` from a string.
///
/// # Arguments
///
/// * `s` - A string representing the color.
///
/// # Returns
///
/// A new `Color` instance.
pub fn new(s: String) -> Self {
Color(s)
}
/// Returns a reference to the inner string.
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for Color {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0) // Use the inner String value
}
}
impl PartialEq<&str> for Color {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
impl PartialEq<str> for Color {
fn eq(&self, other: &str) -> bool {
self.0 == other
}
}
impl PartialEq<Color> for Color {
fn eq(&self, other: &Color) -> bool {
self.0 == other.0
}
}
impl Deref for Color {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Color {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
#[derive(Clone, Debug)]
pub enum ThemeValue {
Color(Color),
Subtheme(Theme), // A nested theme itself
}
impl fmt::Display for ThemeValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ThemeValue::Color(color) => write!(f, "{}", color), // Print just the inner Color value
ThemeValue::Subtheme(theme) => write!(f, "Subtheme({})", theme), // Print the inner theme in display format
}
}
}
impl ThemeValue {
/// Converts a `serde_jsonc2::Value` into a `ThemeValue`.
///
/// # Arguments
///
/// * `value` - A `serde_jsonc2::Value` to convert.
///
/// # Returns
///
/// A `ThemeValue` representing the input value.
pub fn from_json(value: Value) -> Self {
match value {
Value::Object(map) => {
let nested = map
.into_iter()
.map(|(key, value)| (key, ThemeValue::from_json(value)))
.collect::<HashMap<String, ThemeValue>>();
ThemeValue::Subtheme(Theme(nested))
}
Value::String(s) => ThemeValue::Color(Color::new(s)),
_ => ThemeValue::Color(Color::new(value.to_string())),
}
}
/// Converts a `serde_yml::Value` into a `ThemeValue`.
///
/// # Arguments
///
/// * `value` - A `serde_yml::Value` to convert.
///
/// # Returns
///
/// A `ThemeValue` representing the input value.
#[cfg(feature = "theme_yml")]
pub fn from_yaml(value: YamlValue) -> Self {
match value {
YamlValue::Mapping(map) => {
let nested = map
.into_iter()
.map(|(key, value): (YamlValue, YamlValue)| {
// Convert YamlValue::String into a regular string and handle nested values
let key = key.as_str().unwrap_or_default().to_string();
(key, ThemeValue::from_yaml(value))
})
.collect::<HashMap<String, ThemeValue>>();
ThemeValue::Subtheme(Theme(nested))
}
YamlValue::String(s) => ThemeValue::Color(Color::new(s)),
// Handle other types (like booleans, integers, etc.)
_ => ThemeValue::Color(Color::new(yml_to_string(&value).unwrap_or_default())),
}
}
}
/// Represents a theme, which is a collection of key-value pairs.
#[derive(Debug, Clone)]
pub struct Theme(HashMap<String, ThemeValue>);
impl fmt::Display for Theme {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{{ ")?;
let mut iter = self.0.iter();
if let Some((key, value)) = iter.next() {
write!(f, "{}: {}", key, value)?;
for (key, value) in iter {
write!(f, ", {}: {}", key, value)?;
}
}
write!(f, " }}")
}
}
impl Theme {
/// Retrieves a value by its key.
///
/// # Arguments
///
/// * `key` - A string slice representing the key.
///
/// # Returns
///
/// An `Option` containing the `ThemeValue` associated with the key.
pub fn get(&self, key: &str) -> Option<ThemeValue> {
let mut current_map = &self.0;
let parts: Vec<&str> = key.split('.').collect(); // Split the key into parts
for (index, part) in parts.iter().enumerate() {
match current_map.get(*part) {
Some(ThemeValue::Subtheme(subtheme)) => {
// If the current part is a Subtheme, update the current map to the subtheme
current_map = &subtheme.0;
// If we're at the last part, return the subtheme itself
if index == parts.len() - 1 {
return Some(ThemeValue::Subtheme(subtheme.clone())); // Return a cloned Subtheme
}
}
Some(value) => {
// If the current part matches a value and it's not a Subtheme, return it
if index == parts.len() - 1 {
return Some(value.clone()); // Clone the value before returning it
}
}
None => return None, // If any part is not found, return None
}
}
None // Return None if we couldn't resolve the entire key path
}
/// Retrieves a color by its key.
///
/// # Arguments
///
/// * `key` - A string slice representing the key.
///
/// # Returns
///
/// An `Option` containing the `Color` associated with the key.
pub fn get_color(&self, key: &str) -> Option<Color> {
self.get(key).and_then(|theme_value| {
if let ThemeValue::Color(color) = theme_value {
Some(color)
} else {
None
}
})
}
/// Parses a theme configuration from a string.
///
/// # Arguments
///
/// * `contents` - A string slice containing the theme data in either YAML or JSON format.
///
/// # Returns
///
/// A `Result` containing the parsed `Theme` or an error. The function will first attempt to parse the contents as YAML (if the `theme_yml` feature is enabled),
/// and fallback to JSON parsing if YAML parsing fails or is not supported.
pub fn parse_theme(contents: &str) -> Result<Theme, Box<dyn std::error::Error>> {
// Try to parse as YAML if the `theme_yml` feature is enabled
#[cfg(feature = "theme_yml")]
if let Ok(yaml_value) = from_yml_str(contents) {
if let YamlValue::Mapping(_) = &yaml_value {
let theme = ThemeValue::from_yaml(yaml_value);
if let ThemeValue::Subtheme(theme_map) = theme {
return Ok(theme_map);
} else {
return Err("invalid root type: expected a mapping.".into());
}
} else {
return Err("invalid root type: expected a mapping.".into());
}
}
let value: Value = from_str(contents)?;
if let Value::Object(_) = &value {
let theme = ThemeValue::from_json(value); // Convert JSON into ThemeValue
if let ThemeValue::Subtheme(theme_map) = theme {
Ok(theme_map) // Return the parsed theme
} else {
Err("invalid root type: expected an object.".into()) // Error for unexpected root type
}
} else {
Err("invalid root type: expected an object.".into()) // Error if the root is not an object
}
}
// Recursive function to collect all keys, including nested ones
pub fn keys(&self) -> Vec<String> {
let mut keys = Vec::new();
self.collect_keys_recursive(&self.0, &mut keys, String::new());
keys
}
// Helper function to recursively collect keys
fn collect_keys_recursive(
&self,
current_map: &HashMap<String, ThemeValue>,
keys: &mut Vec<String>,
prefix: String,
) {
for (key, value) in current_map {
let new_key = if prefix.is_empty() {
key.clone()
} else {
format!("{}.{}", prefix, key)
};
keys.push(new_key.clone());
if let ThemeValue::Subtheme(subtheme) = value {
subtheme.collect_keys_recursive(&subtheme.0, keys, new_key);
}
}
}
// Method to collect only color keys
pub fn colors(&self) -> Vec<String> {
let mut colors = Vec::new();
self.collect_colors_recursive(&self.0, &mut colors, String::new());
colors
}
// Helper function to recursively collect color keys
fn collect_colors_recursive(
&self,
current_map: &HashMap<String, ThemeValue>,
colors: &mut Vec<String>,
prefix: String,
) {
for (key, value) in current_map {
let new_key = if prefix.is_empty() {
key.clone()
} else {
format!("{}.{}", prefix, key)
};
match value {
// If the value is a color (i.e., string), add it to the colors list
ThemeValue::Color(_) => colors.push(new_key),
// If the value is a subtheme, recursively collect from the subtheme
ThemeValue::Subtheme(subtheme) => {
subtheme.collect_colors_recursive(&subtheme.0, colors, new_key);
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const JSON_DATA: &str = r###"
{
"blue": "#89b4fa",
"text": {
"white": "#cdd6f4",
"dark": {
"grey": "#313244"
}
},
"red": "#f38ba8"
}
"###;
#[cfg(feature = "theme_yml")]
const YAML_DATA: &str = r###"
blue: "#89b4fa"
text:
white: "#cdd6f4"
dark:
grey: "#313244"
red: "#f38ba8"
"###;
#[test]
fn test_get_color() {
let theme = Theme::parse_theme(JSON_DATA).unwrap();
// Test getting the "blue" color
if let Some(ThemeValue::Color(color)) = theme.get("blue") {
assert_eq!(color.as_str(), "#89b4fa");
} else {
panic!("Failed to get the blue color");
}
// Test getting the "red" color
if let Some(ThemeValue::Color(color)) = theme.get("red") {
assert_eq!(color.as_str(), "#f38ba8");
} else {
panic!("Failed to get the red color");
}
// Test for non-existent key
assert!(theme.get("yellow").is_none());
}
#[test]
#[cfg(feature = "theme_yml")]
fn test_get_color_yml() {
let theme = Theme::parse_theme(YAML_DATA).unwrap();
println!("{:?}", theme);
// Test getting the "blue" color
if let Some(ThemeValue::Color(color)) = theme.get("blue") {
assert_eq!(color.as_str(), "#89b4fa");
} else {
panic!("Failed to get the blue color");
}
// Test getting the "red" color
if let Some(ThemeValue::Color(color)) = theme.get("red") {
assert_eq!(color.as_str(), "#f38ba8");
} else {
panic!("Failed to get the red color");
}
// Test for non-existent key
assert!(theme.get("yellow").is_none());
}
#[test]
fn test_get_subtheme() {
let theme = Theme::parse_theme(JSON_DATA).unwrap();
// Test getting the "text" subtheme
if let Some(ThemeValue::Subtheme(subtheme)) = theme.get("text") {
assert!(subtheme.get("white").is_some()); // The "white" color should exist within "text"
} else {
panic!("Failed to get the text subtheme");
}
// Test getting the "text.dark" subtheme
if let Some(ThemeValue::Subtheme(subtheme)) = theme.get("text.dark") {
assert!(subtheme.get("grey").is_some()); // The "grey" color should exist within "text.dark"
} else {
panic!("Failed to get the text.dark subtheme");
}
// Test for non-existent key in subtheme
assert!(theme.get("text.dark.notfound").is_none());
}
#[test]
fn test_get_nested_subtheme() {
let theme = Theme::parse_theme(JSON_DATA).unwrap();
// Test getting the nested subtheme "text.dark"
if let Some(ThemeValue::Subtheme(subtheme)) = theme.get("text.dark") {
if let Some(ThemeValue::Color(color)) = subtheme.get("grey") {
assert_eq!(color.as_str(), "#313244");
} else {
panic!("Failed to get grey color from text.dark");
}
} else {
panic!("Failed to get the text.dark subtheme");
}
}
#[test]
fn test_get_non_existent_key() {
let theme = Theme::parse_theme(JSON_DATA).unwrap();
// Test for non-existent key
assert!(theme.get("green").is_none());
}
}