1use std::sync::Arc;
16
17use rustc_hash::FxHashMap;
18
19use super::style::AdaptiveColor;
20
21pub const FONT_SANS: &str = "font-sans";
23
24pub const FONT_MONO: &str = "font-mono";
26
27#[derive(Debug, Clone, PartialEq, Default)]
30pub struct Tokens {
31 colors: FxHashMap<Box<str>, AdaptiveColor>,
32 numbers: FxHashMap<Box<str>, f32>,
33 fonts: FxHashMap<Box<str>, Arc<str>>,
34}
35
36#[derive(Debug, Clone, PartialEq)]
38pub enum Token {
39 Color(AdaptiveColor),
41 Number(f32),
43 Font(Arc<str>),
45}
46
47pub type TokenSource = fn(Option<&str>) -> Result<Tokens, String>;
50
51impl Tokens {
52 pub fn color(&self, name: &str) -> Option<AdaptiveColor> {
54 self.colors.get(name).copied()
55 }
56
57 pub fn set_color(&mut self, name: &str, color: AdaptiveColor) {
59 self.colors.insert(name.into(), color);
60 }
61
62 pub fn number(&self, name: &str) -> Option<f32> {
64 self.numbers.get(name).copied()
65 }
66
67 pub fn set_number(&mut self, name: &str, value: f32) {
69 self.numbers.insert(name.into(), value);
70 }
71
72 pub fn font(&self, name: &str) -> Option<&Arc<str>> {
74 self.fonts.get(name)
75 }
76
77 pub fn set_font(&mut self, name: &str, family: impl Into<Arc<str>>) {
79 self.fonts.insert(name.into(), family.into());
80 }
81
82 pub fn get(&self, name: &str) -> Option<Token> {
85 if let Some(c) = self.colors.get(name) {
86 return Some(Token::Color(*c));
87 }
88 if let Some(n) = self.numbers.get(name) {
89 return Some(Token::Number(*n));
90 }
91 self.fonts.get(name).cloned().map(Token::Font)
92 }
93
94 pub fn is_empty(&self) -> bool {
96 self.colors.is_empty() && self.numbers.is_empty() && self.fonts.is_empty()
97 }
98
99 pub fn colors(&self) -> impl Iterator<Item = (&str, AdaptiveColor)> {
101 self.colors.iter().map(|(k, c)| (&**k, *c))
102 }
103
104 pub fn numbers(&self) -> impl Iterator<Item = (&str, f32)> {
106 self.numbers.iter().map(|(k, n)| (&**k, *n))
107 }
108
109 pub fn fonts(&self) -> impl Iterator<Item = (&str, &Arc<str>)> {
111 self.fonts.iter().map(|(k, f)| (&**k, f))
112 }
113
114 pub fn over(&self, base: &Tokens) -> Tokens {
116 let mut out = base.clone();
117 out.colors
118 .extend(self.colors.iter().map(|(k, c)| (k.clone(), *c)));
119 out.numbers
120 .extend(self.numbers.iter().map(|(k, n)| (k.clone(), *n)));
121 out.fonts
122 .extend(self.fonts.iter().map(|(k, f)| (k.clone(), f.clone())));
123 out
124 }
125}
126
127#[cfg(test)]
130pub(crate) fn sample() -> Tokens {
131 let mut tokens = Tokens::default();
132 for (key, light, dark) in [
133 ("background", 0xffffff, 0x0a0a0a),
134 ("foreground", 0x0a0a0a, 0xfafafa),
135 ("border", 0xe5e5e5, 0x262626),
136 ("ring", 0xa3a3a3, 0x525252),
137 ("muted", 0xf5f5f5, 0x262626),
138 ("muted-foreground", 0x737373, 0xa3a3a3),
139 ("accent", 0xf5f5f5, 0x262626),
140 ("primary", 0x171717, 0xfafafa),
141 ("primary-active", 0x0a0a0a, 0xe5e5e5),
142 ("primary-foreground", 0xfafafa, 0x171717),
143 ("popover", 0xffffff, 0x0a0a0a),
144 ("popover-foreground", 0x0a0a0a, 0xfafafa),
145 ] {
146 tokens.set_color(key, AdaptiveColor::rgb(light, dark));
147 }
148 for (key, n) in [("radius", 12.0), ("radius-lg", 16.0), ("font-size", 16.0)] {
149 tokens.set_number(key, n);
150 }
151 tokens
152}
153
154#[cfg(test)]
155mod tests {
156 use super::*;
157 use crate::color::rgb;
158
159 #[test]
160 fn tokens_over_others_win_and_keep_the_rest() {
161 let mut base = Tokens::default();
162 base.set_color("primary", rgb(0x111111).into());
163 base.set_color("ring", rgb(0x222222).into());
164 base.set_number("radius", 12.0);
165 base.set_font(FONT_SANS, "Inter");
166 let mut top = Tokens::default();
167 top.set_color("primary", rgb(0x333333).into());
168 top.set_number("radius-card", 10.0);
169 top.set_font(FONT_MONO, "Iosevka");
170 let tokens = top.over(&base);
171 assert_eq!(tokens.color("primary"), Some(rgb(0x333333).into()));
172 assert_eq!(tokens.color("ring"), Some(rgb(0x222222).into()));
173 assert_eq!(tokens.number("radius"), Some(12.0));
174 assert_eq!(tokens.number("radius-card"), Some(10.0));
175 assert_eq!(tokens.font(FONT_SANS).map(|f| &**f), Some("Inter"));
176 assert_eq!(tokens.font(FONT_MONO).map(|f| &**f), Some("Iosevka"));
177 assert!(Tokens::default().is_empty());
178 }
179}