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
use crate::border::BorderStyle;
use crate::color::Color;
use crate::decoration::Decoration;
use crate::style::Style;
use std::collections::HashMap;
use std::error::Error;
use std::fmt;
use std::str::FromStr;
#[derive(Debug)]
pub struct Termio {
styles: HashMap<String, Style>,
}
/// Custom error type for TCSS parsing errors
#[derive(Debug)]
pub enum ParseError {
InvalidSyntax(String),
DuplicateElement(String),
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ParseError::InvalidSyntax(msg) => write!(f, "Invalid syntax: {}", msg),
ParseError::DuplicateElement(name) => write!(f, "Duplicate element name: {}", name),
}
}
}
impl Error for ParseError {}
impl From<String> for ParseError {
fn from(s: String) -> Self {
ParseError::InvalidSyntax(s)
}
}
impl Termio {
/// Creates a new Termio with an empty style map.
pub fn new() -> Self {
Termio {
styles: HashMap::new(),
}
}
pub fn from_file(path: &str) -> Result<Self, ParseError> {
let mut tcss = Self::new();
let content = std::fs::read_to_string(path).map_err(|e| ParseError::InvalidSyntax(e.to_string()))?;
tcss.parse(&content)?;
Ok(tcss)
}
/// Retrieves a style by name, returning None if not found.
pub fn get_style(&self, name: &str) -> Option<Style> {
self.styles.get(name).cloned()
}
/// Parses TCSS content and populates the style map.
pub fn parse(&mut self, content: &str) -> Result<(), ParseError> {
let mut lines = content.lines().peekable();
let mut current_style = None;
let mut current_name = None;
while let Some(line) = lines.next() {
let line = line.trim();
// Skip empty lines and comments
if line.is_empty() || line.starts_with("//") {
continue;
}
if line.starts_with("@element") {
if let Some(name) = current_name {
if self.styles.contains_key(&name) {
return Err(ParseError::DuplicateElement(name));
}
self.styles.insert(name, current_style.unwrap_or_default());
}
let name = line
.split('"')
.nth(1)
.ok_or_else(|| ParseError::InvalidSyntax("Missing element name".to_string()))?;
current_name = Some(name.to_string());
current_style = Some(Style::new());
} else if let Some(style) = &mut current_style {
if line == "}" {
if let Some(name) = current_name.take() {
if self.styles.contains_key(&name) {
return Err(ParseError::DuplicateElement(name));
}
self.styles
.insert(name, current_style.take().unwrap_or_default());
}
} else {
let parts: Vec<&str> = line.split(':').collect();
if parts.len() != 2 {
return Err(ParseError::InvalidSyntax(format!(
"Invalid property: {}",
line
)));
}
let property = parts[0].trim();
let value = parts[1].trim().trim_end_matches(';');
match property {
"color" => {
style.fg = Some(
Color::from_str(value)
.map_err(|e| ParseError::InvalidSyntax(e.to_string()))?,
)
}
"background" => {
style.bg = Some(
Color::from_str(value)
.map_err(|e| ParseError::InvalidSyntax(e.to_string()))?,
)
}
"decoration" => style.decoration = Some(self.parse_decoration(value)?),
"padding" => {
let values: Vec<&str> = value.split_whitespace().collect();
match values.len() {
1 => {
let pad = values[0].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid padding value: {}",
value
))
})?;
style.padding = Some(pad);
style.padding_top = Some(pad);
style.padding_bottom = Some(pad);
style.padding_left = Some(pad);
style.padding_right = Some(pad);
}
2 => {
let v = values[0].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid padding value: {}",
value
))
})?;
let h = values[1].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid padding value: {}",
value
))
})?;
style.padding_top = Some(v);
style.padding_bottom = Some(v);
style.padding_left = Some(h);
style.padding_right = Some(h);
}
4 => {
let top = values[0].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid padding value: {}",
value
))
})?;
let right = values[1].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid padding value: {}",
value
))
})?;
let bottom = values[2].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid padding value: {}",
value
))
})?;
let left = values[3].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid padding value: {}",
value
))
})?;
style.padding_top = Some(top);
style.padding_right = Some(right);
style.padding_bottom = Some(bottom);
style.padding_left = Some(left);
}
_ => {
return Err(ParseError::InvalidSyntax(
"Invalid padding format. Use 1, 2, or 4 values".to_string(),
))
}
}
}
"padding-top" => {
style.padding_top = Some(value.parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid padding-top value: {}",
value
))
})?)
}
"padding-bottom" => {
style.padding_bottom = Some(value.parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid padding-bottom value: {}",
value
))
})?)
}
"padding-left" => {
style.padding_left = Some(value.parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid padding-left value: {}",
value
))
})?)
}
"padding-right" => {
style.padding_right = Some(value.parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid padding-right value: {}",
value
))
})?)
}
"margin" => {
let values: Vec<&str> = value.split_whitespace().collect();
match values.len() {
1 => {
let margin = values[0].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid margin value: {}",
value
))
})?;
style.margin = Some(margin);
style.margin_top = Some(margin);
style.margin_bottom = Some(margin);
style.margin_left = Some(margin);
style.margin_right = Some(margin);
}
2 => {
let v = values[0].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid margin value: {}",
value
))
})?;
let h = values[1].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid margin value: {}",
value
))
})?;
style.margin_top = Some(v);
style.margin_bottom = Some(v);
style.margin_left = Some(h);
style.margin_right = Some(h);
}
4 => {
let top = values[0].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid margin value: {}",
value
))
})?;
let right = values[1].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid margin value: {}",
value
))
})?;
let bottom = values[2].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid margin value: {}",
value
))
})?;
let left = values[3].parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid margin value: {}",
value
))
})?;
style.margin_top = Some(top);
style.margin_right = Some(right);
style.margin_bottom = Some(bottom);
style.margin_left = Some(left);
}
_ => {
return Err(ParseError::InvalidSyntax(
"Invalid margin format. Use 1, 2, or 4 values".to_string(),
))
}
}
}
"margin-top" => {
style.margin_top = Some(value.parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid margin-top value: {}",
value
))
})?)
}
"margin-bottom" => {
style.margin_bottom = Some(value.parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid margin-bottom value: {}",
value
))
})?)
}
"margin-left" => {
style.margin_left = Some(value.parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid margin-left value: {}",
value
))
})?)
}
"margin-right" => {
style.margin_right = Some(value.parse().map_err(|_| {
ParseError::InvalidSyntax(format!(
"Invalid margin-right value: {}",
value
))
})?)
}
"border-color" => {
style.border_color = Some(
Color::from_str(value)
.map_err(|e| ParseError::InvalidSyntax(e.to_string()))?,
)
}
"border-style" => {
style.border_style = Some(self.parse_border_style(value)?)
}
"border" => {
let (s, c) = value.split_once(" ").unwrap();
style.border_style = Some(self.parse_border_style(s)?);
style.border_color = Some(
Color::from_str(c)
.map_err(|e| ParseError::InvalidSyntax(e.to_string()))?,
);
}
_ => {
return Err(ParseError::InvalidSyntax(format!(
"Unknown property: {}",
property
)))
}
}
}
}
}
// Handle the last style if exists
if let Some(name) = current_name {
if self.styles.contains_key(&name) {
return Err(ParseError::DuplicateElement(name));
}
self.styles.insert(name, current_style.unwrap_or_default());
}
Ok(())
}
/// Parses a decoration string into a vector of decorations
fn parse_decoration(&self, value: &str) -> Result<Vec<Decoration>, ParseError> {
value
.split_whitespace()
.map(|d| Decoration::from_str(d).map_err(|e| ParseError::InvalidSyntax(e.to_string())))
.collect()
}
/// Parses a border style string into a BorderStyle
fn parse_border_style(&self, value: &str) -> Result<BorderStyle, ParseError> {
BorderStyle::from_str(value).map_err(|e| ParseError::InvalidSyntax(e.to_string()))
}
pub fn add_style(&mut self, name: &str, style: Style) {
self.styles.insert(name.to_string(), style);
}
}