1use std::collections::HashMap;
4use std::sync::atomic::{AtomicU32, Ordering};
5use std::sync::RwLock;
6
7#[repr(transparent)]
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct AtomId(pub u32);
13
14impl AtomId {
15 pub const NULL: Self = Self(0);
16
17 pub const DIV: Self = Self(1);
19 pub const SPAN: Self = Self(2);
20 pub const TEXT: Self = Self(3);
21 pub const P: Self = Self(4);
22 pub const A: Self = Self(5);
23 pub const BUTTON: Self = Self(6);
24 pub const INPUT: Self = Self(7);
25 pub const IMG: Self = Self(8);
26 pub const H1: Self = Self(9);
27 pub const H2: Self = Self(10);
28 pub const H3: Self = Self(11);
29 pub const H4: Self = Self(12);
30 pub const H5: Self = Self(13);
31 pub const H6: Self = Self(14);
32 pub const UL: Self = Self(15);
33 pub const OL: Self = Self(16);
34 pub const LI: Self = Self(17);
35 pub const TABLE: Self = Self(18);
36 pub const TR: Self = Self(19);
37 pub const TD: Self = Self(20);
38 pub const TH: Self = Self(21);
39 pub const FORM: Self = Self(22);
40 pub const HEADER: Self = Self(23);
41 pub const FOOTER: Self = Self(24);
42 pub const NAV: Self = Self(25);
43 pub const SECTION: Self = Self(26);
44 pub const ARTICLE: Self = Self(27);
45 pub const ASIDE: Self = Self(28);
46 pub const MAIN: Self = Self(29);
47 pub const CANVAS: Self = Self(30);
48 pub const CANVAS_3D: Self = Self(31);
49 pub const SVG: Self = Self(32);
50 pub const PATH: Self = Self(33);
51
52 pub const DISPLAY: Self = Self(101);
54 pub const POSITION: Self = Self(102);
55 pub const WIDTH: Self = Self(103);
56 pub const HEIGHT: Self = Self(104);
57 pub const MIN_WIDTH: Self = Self(105);
58 pub const MAX_WIDTH: Self = Self(106);
59 pub const MIN_HEIGHT: Self = Self(107);
60 pub const MAX_HEIGHT: Self = Self(108);
61 pub const MARGIN: Self = Self(109);
62 pub const PADDING: Self = Self(110);
63 pub const BORDER: Self = Self(111);
64 pub const BORDER_RADIUS: Self = Self(112);
65 pub const COLOR: Self = Self(113);
66 pub const BACKGROUND_COLOR: Self = Self(114);
67 pub const OPACITY: Self = Self(115);
68 pub const Z_INDEX: Self = Self(116);
69 pub const TRANSFORM: Self = Self(117);
70 pub const OVERFLOW: Self = Self(118);
71 pub const FLEX_DIRECTION: Self = Self(119);
72 pub const JUSTIFY_CONTENT: Self = Self(120);
73 pub const ALIGN_ITEMS: Self = Self(121);
74 pub const GAP: Self = Self(122);
75 pub const GRID_TEMPLATE_COLUMNS: Self = Self(123);
76 pub const GRID_TEMPLATE_ROWS: Self = Self(124);
77
78 pub const FONT_SANS: Self = Self(201);
80 pub const FONT_SERIF: Self = Self(202);
81 pub const FONT_MONO: Self = Self(203);
82 pub const HREF: Self = Self(204);
83
84 #[inline(always)]
85 pub const fn is_static(self) -> bool {
86 self.0 > 0 && self.0 <= 1023
87 }
88}
89
90const STATIC_ATOMS: &[(AtomId, &str)] = &[
92 (AtomId::DIV, "div"),
93 (AtomId::SPAN, "span"),
94 (AtomId::TEXT, "text"),
95 (AtomId::P, "p"),
96 (AtomId::A, "a"),
97 (AtomId::BUTTON, "button"),
98 (AtomId::INPUT, "input"),
99 (AtomId::IMG, "img"),
100 (AtomId::H1, "h1"),
101 (AtomId::H2, "h2"),
102 (AtomId::H3, "h3"),
103 (AtomId::H4, "h4"),
104 (AtomId::H5, "h5"),
105 (AtomId::H6, "h6"),
106 (AtomId::UL, "ul"),
107 (AtomId::OL, "ol"),
108 (AtomId::LI, "li"),
109 (AtomId::TABLE, "table"),
110 (AtomId::TR, "tr"),
111 (AtomId::TD, "td"),
112 (AtomId::TH, "th"),
113 (AtomId::FORM, "form"),
114 (AtomId::HEADER, "header"),
115 (AtomId::FOOTER, "footer"),
116 (AtomId::NAV, "nav"),
117 (AtomId::SECTION, "section"),
118 (AtomId::ARTICLE, "article"),
119 (AtomId::ASIDE, "aside"),
120 (AtomId::MAIN, "main"),
121 (AtomId::CANVAS, "canvas"),
122 (AtomId::CANVAS_3D, "canvas3d"),
123 (AtomId::SVG, "svg"),
124 (AtomId::PATH, "path"),
125 (AtomId::DISPLAY, "display"),
127 (AtomId::POSITION, "position"),
128 (AtomId::WIDTH, "width"),
129 (AtomId::HEIGHT, "height"),
130 (AtomId::MIN_WIDTH, "min-width"),
131 (AtomId::MAX_WIDTH, "max-width"),
132 (AtomId::MIN_HEIGHT, "min-height"),
133 (AtomId::MAX_HEIGHT, "max-height"),
134 (AtomId::MARGIN, "margin"),
135 (AtomId::PADDING, "padding"),
136 (AtomId::BORDER, "border"),
137 (AtomId::BORDER_RADIUS, "border-radius"),
138 (AtomId::COLOR, "color"),
139 (AtomId::BACKGROUND_COLOR, "background-color"),
140 (AtomId::OPACITY, "opacity"),
141 (AtomId::Z_INDEX, "z-index"),
142 (AtomId::TRANSFORM, "transform"),
143 (AtomId::OVERFLOW, "overflow"),
144 (AtomId::FLEX_DIRECTION, "flex-direction"),
145 (AtomId::JUSTIFY_CONTENT, "justify-content"),
146 (AtomId::ALIGN_ITEMS, "align-items"),
147 (AtomId::GAP, "gap"),
148 (AtomId::GRID_TEMPLATE_COLUMNS, "grid-template-columns"),
149 (AtomId::GRID_TEMPLATE_ROWS, "grid-template-rows"),
150 (AtomId::FONT_SANS, "system-ui, sans-serif"),
151 (AtomId::FONT_SERIF, "ui-serif, Georgia, Cambria, serif"),
152 (AtomId::FONT_MONO, "ui-monospace, monospace"),
153 (AtomId::HREF, "href"),
154];
155
156pub struct AtomTable {
158 string_to_id: RwLock<HashMap<String, AtomId>>,
159 id_to_string: RwLock<Vec<String>>,
160 next_id: AtomicU32,
161}
162
163impl Default for AtomTable {
164 fn default() -> Self {
165 Self::new()
166 }
167}
168
169impl AtomTable {
170 pub fn new() -> Self {
171 let mut string_to_id = HashMap::with_capacity(STATIC_ATOMS.len() + 64);
172 for &(id, name) in STATIC_ATOMS {
173 string_to_id.insert(name.to_string(), id);
174 }
175
176 Self {
177 string_to_id: RwLock::new(string_to_id),
178 id_to_string: RwLock::new(Vec::new()),
179 next_id: AtomicU32::new(1024), }
181 }
182
183 pub fn intern(&self, text: &str) -> AtomId {
185 {
187 let map = self.string_to_id.read().unwrap();
188 if let Some(&id) = map.get(text) {
189 return id;
190 }
191 }
192
193 let mut map = self.string_to_id.write().unwrap();
195 if let Some(&id) = map.get(text) {
196 return id;
197 }
198
199 let id = AtomId(self.next_id.fetch_add(1, Ordering::Relaxed));
200 map.insert(text.to_string(), id);
201
202 let mut list = self.id_to_string.write().unwrap();
203 list.push(text.to_string());
204
205 id
206 }
207
208 pub fn resolve(&self, id: AtomId) -> Option<String> {
210 if id.is_static() {
211 for &(static_id, name) in STATIC_ATOMS {
212 if static_id == id {
213 return Some(name.to_string());
214 }
215 }
216 }
217
218 let idx = (id.0.checked_sub(1024)?) as usize;
219 let list = self.id_to_string.read().unwrap();
220 list.get(idx).cloned()
221 }
222}
223
224static GLOBAL_ATOM_TABLE: std::sync::OnceLock<AtomTable> = std::sync::OnceLock::new();
225
226pub fn global_atom_table() -> &'static AtomTable {
228 GLOBAL_ATOM_TABLE.get_or_init(AtomTable::new)
229}
230
231pub fn intern(text: &str) -> AtomId {
233 global_atom_table().intern(text)
234}
235
236pub fn resolve(id: AtomId) -> Option<String> {
238 global_atom_table().resolve(id)
239}