muskitty-css-values 0.1.1

CSS Values Level 4 typed value parsing (length/angle/time/calc/var)
Documentation
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! 数值类型 — CSS Values Level 4 §4-§6。

use muskitty_css::parser::ComponentValue;
use muskitty_css::tokenizer::Token;

/// 值解析错误。
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
    pub message: String,
}

impl ParseError {
    pub fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
        }
    }
}

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "CSS value parse error: {}", self.message)
    }
}

impl std::error::Error for ParseError {}

/// 从 ComponentValue 列表中提取唯一的非 whitespace token。
pub fn single_non_ws_cv(cvs: &[ComponentValue]) -> Result<&ComponentValue, ParseError> {
    let filtered: Vec<&ComponentValue> = cvs
        .iter()
        .filter(|cv| !matches!(cv, ComponentValue::PreservedToken(Token::Whitespace)))
        .collect();
    if filtered.len() != 1 {
        return Err(ParseError::new(format!(
            "expected exactly one value, got {}",
            filtered.len()
        )));
    }
    Ok(filtered[0])
}

// ── §5 Length ────────────────────────────────────────────────────────

/// CSS `<length>` 值 (css-values-4 §5)。
///
/// 一个数值 + 长度单位。本阶段不计算绝对长度(如 em→px),
/// 保留原始值和单位,求值留到 Cascade 阶段。
#[derive(Debug, Clone, PartialEq)]
pub struct Length {
    pub value: f64,
    pub unit: LengthUnit,
}

/// 长度单位 (css-values-4 §5.1 相对、§5.2 绝对)。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LengthUnit {
    // 相对 (§5.1)
    Em,
    Rem,
    Ex,
    Ch,
    Vw,
    Vh,
    Vmin,
    Vmax,
    // 绝对 (§5.2)
    Px,
    Cm,
    Mm,
    In,
    Pt,
    Pc,
    Q,
}

impl LengthUnit {
    /// 从字符串解析长度单位(ASCII case-insensitive)。
    pub fn parse_unit(s: &str) -> Option<Self> {
        match s.to_ascii_lowercase().as_str() {
            "em" => Some(Self::Em),
            "rem" => Some(Self::Rem),
            "ex" => Some(Self::Ex),
            "ch" => Some(Self::Ch),
            "vw" => Some(Self::Vw),
            "vh" => Some(Self::Vh),
            "vmin" => Some(Self::Vmin),
            "vmax" => Some(Self::Vmax),
            "px" => Some(Self::Px),
            "cm" => Some(Self::Cm),
            "mm" => Some(Self::Mm),
            "in" => Some(Self::In),
            "pt" => Some(Self::Pt),
            "pc" => Some(Self::Pc),
            "q" => Some(Self::Q),
            _ => None,
        }
    }

    /// 序列化为 CSS 单位字符串。
    pub fn to_str(self) -> &'static str {
        match self {
            Self::Em => "em",
            Self::Rem => "rem",
            Self::Ex => "ex",
            Self::Ch => "ch",
            Self::Vw => "vw",
            Self::Vh => "vh",
            Self::Vmin => "vmin",
            Self::Vmax => "vmax",
            Self::Px => "px",
            Self::Cm => "cm",
            Self::Mm => "mm",
            Self::In => "in",
            Self::Pt => "pt",
            Self::Pc => "pc",
            Self::Q => "Q",
        }
    }
}

impl Length {
    /// 从 CSS 字符串解析一个 `<length>` 值。
    pub fn parse(input: &str) -> Result<Self, ParseError> {
        let cvs = muskitty_css::parse_list_of_component_values(input);
        Self::from_cvs(&cvs)
    }

    /// 从 ComponentValue 列表解析。
    pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
        let cv = single_non_ws_cv(cvs)?;
        match cv {
            ComponentValue::PreservedToken(Token::Dimension(numeric, unit)) => {
                let unit = LengthUnit::parse_unit(unit)
                    .ok_or_else(|| ParseError::new(format!("unknown length unit: {unit}")))?;
                Ok(Length {
                    value: numeric.value,
                    unit,
                })
            }
            _ => Err(ParseError::new("expected a dimension token for length")),
        }
    }
}

// ── §4.6 Percentage ─────────────────────────────────────────────────

/// CSS `<percentage>` 值 (§4.6)。
#[derive(Debug, Clone, PartialEq)]
pub struct Percentage {
    pub value: f64,
}

impl Percentage {
    pub fn parse(input: &str) -> Result<Self, ParseError> {
        let cvs = muskitty_css::parse_list_of_component_values(input);
        Self::from_cvs(&cvs)
    }

    pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
        let cv = single_non_ws_cv(cvs)?;
        match cv {
            ComponentValue::PreservedToken(Token::Percentage(numeric)) => Ok(Percentage {
                value: numeric.value,
            }),
            _ => Err(ParseError::new("expected a percentage token")),
        }
    }
}

// ── §4.4 Number ─────────────────────────────────────────────────────

/// CSS `<number>` 值 (§4.4)。
#[derive(Debug, Clone, PartialEq)]
pub struct Number {
    pub value: f64,
}

impl Number {
    pub fn parse(input: &str) -> Result<Self, ParseError> {
        let cvs = muskitty_css::parse_list_of_component_values(input);
        Self::from_cvs(&cvs)
    }

    pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
        let cv = single_non_ws_cv(cvs)?;
        match cv {
            ComponentValue::PreservedToken(Token::Number(numeric)) => Ok(Number {
                value: numeric.value,
            }),
            _ => Err(ParseError::new("expected a number token")),
        }
    }
}

// ── §4.3 Integer ────────────────────────────────────────────────────

/// CSS `<integer>` 值 (§4.3)。
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Integer {
    pub value: i32,
}

impl Integer {
    pub fn parse(input: &str) -> Result<Self, ParseError> {
        let cvs = muskitty_css::parse_list_of_component_values(input);
        Self::from_cvs(&cvs)
    }

    pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
        let cv = single_non_ws_cv(cvs)?;
        match cv {
            ComponentValue::PreservedToken(Token::Number(numeric)) if numeric.is_integer => {
                Ok(Integer {
                    value: numeric.value as i32,
                })
            }
            _ => Err(ParseError::new("expected an integer token")),
        }
    }
}

// ── §6.1 Angle ──────────────────────────────────────────────────────

/// CSS `<angle>` 值 (§6.1)。
#[derive(Debug, Clone, PartialEq)]
pub struct Angle {
    pub value: f64,
    pub unit: AngleUnit,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AngleUnit {
    Deg,
    Grad,
    Rad,
    Turn,
}

impl AngleUnit {
    pub fn parse_unit(s: &str) -> Option<Self> {
        match s.to_ascii_lowercase().as_str() {
            "deg" => Some(Self::Deg),
            "grad" => Some(Self::Grad),
            "rad" => Some(Self::Rad),
            "turn" => Some(Self::Turn),
            _ => None,
        }
    }
}

impl Angle {
    pub fn parse(input: &str) -> Result<Self, ParseError> {
        let cvs = muskitty_css::parse_list_of_component_values(input);
        Self::from_cvs(&cvs)
    }

    pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
        let cv = single_non_ws_cv(cvs)?;
        match cv {
            ComponentValue::PreservedToken(Token::Dimension(numeric, unit)) => {
                let unit = AngleUnit::parse_unit(unit)
                    .ok_or_else(|| ParseError::new(format!("unknown angle unit: {unit}")))?;
                Ok(Angle {
                    value: numeric.value,
                    unit,
                })
            }
            // §6.1: bare 0 is a valid angle (represents 0deg)
            ComponentValue::PreservedToken(Token::Number(numeric)) if numeric.value == 0.0 => {
                Ok(Angle {
                    value: 0.0,
                    unit: AngleUnit::Deg,
                })
            }
            _ => Err(ParseError::new("expected a dimension or 0 for angle")),
        }
    }
}

// ── §6.2 Time ───────────────────────────────────────────────────────

/// CSS `<time>` 值 (§6.2)。
#[derive(Debug, Clone, PartialEq)]
pub struct Time {
    pub value: f64,
    pub unit: TimeUnit,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeUnit {
    S,
    Ms,
}

impl TimeUnit {
    pub fn parse_unit(s: &str) -> Option<Self> {
        match s.to_ascii_lowercase().as_str() {
            "s" => Some(Self::S),
            "ms" => Some(Self::Ms),
            _ => None,
        }
    }
}

impl Time {
    pub fn parse(input: &str) -> Result<Self, ParseError> {
        let cvs = muskitty_css::parse_list_of_component_values(input);
        Self::from_cvs(&cvs)
    }

    pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
        let cv = single_non_ws_cv(cvs)?;
        match cv {
            ComponentValue::PreservedToken(Token::Dimension(numeric, unit)) => {
                let unit = TimeUnit::parse_unit(unit)
                    .ok_or_else(|| ParseError::new(format!("unknown time unit: {unit}")))?;
                Ok(Time {
                    value: numeric.value,
                    unit,
                })
            }
            _ => Err(ParseError::new("expected a dimension token for time")),
        }
    }
}

// ── §6.3 Frequency ──────────────────────────────────────────────────

/// CSS `<frequency>` 值 (§6.3)。
#[derive(Debug, Clone, PartialEq)]
pub struct Frequency {
    pub value: f64,
    pub unit: FrequencyUnit,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrequencyUnit {
    Hz,
    KHz,
}

impl FrequencyUnit {
    pub fn parse_unit(s: &str) -> Option<Self> {
        match s.to_ascii_lowercase().as_str() {
            "hz" => Some(Self::Hz),
            "khz" => Some(Self::KHz),
            _ => None,
        }
    }
}

impl Frequency {
    pub fn parse(input: &str) -> Result<Self, ParseError> {
        let cvs = muskitty_css::parse_list_of_component_values(input);
        Self::from_cvs(&cvs)
    }

    pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
        let cv = single_non_ws_cv(cvs)?;
        match cv {
            ComponentValue::PreservedToken(Token::Dimension(numeric, unit)) => {
                let unit = FrequencyUnit::parse_unit(unit)
                    .ok_or_else(|| ParseError::new(format!("unknown frequency unit: {unit}")))?;
                Ok(Frequency {
                    value: numeric.value,
                    unit,
                })
            }
            _ => Err(ParseError::new("expected a dimension token for frequency")),
        }
    }
}

// ── §6.4 Resolution ─────────────────────────────────────────────────

/// CSS `<resolution>` 值 (§6.4)。
#[derive(Debug, Clone, PartialEq)]
pub struct Resolution {
    pub value: f64,
    pub unit: ResolutionUnit,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResolutionUnit {
    Dpi,
    Dpcm,
    Dppx,
}

impl ResolutionUnit {
    pub fn parse_unit(s: &str) -> Option<Self> {
        match s.to_ascii_lowercase().as_str() {
            "dpi" => Some(Self::Dpi),
            "dpcm" => Some(Self::Dpcm),
            "dppx" => Some(Self::Dppx),
            _ => None,
        }
    }
}

impl Resolution {
    pub fn parse(input: &str) -> Result<Self, ParseError> {
        let cvs = muskitty_css::parse_list_of_component_values(input);
        Self::from_cvs(&cvs)
    }

    pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
        let cv = single_non_ws_cv(cvs)?;
        match cv {
            ComponentValue::PreservedToken(Token::Dimension(numeric, unit)) => {
                let unit = ResolutionUnit::parse_unit(unit)
                    .ok_or_else(|| ParseError::new(format!("unknown resolution unit: {unit}")))?;
                Ok(Resolution {
                    value: numeric.value,
                    unit,
                })
            }
            _ => Err(ParseError::new("expected a dimension token for resolution")),
        }
    }
}

// ── §4.7 Ratio ──────────────────────────────────────────────────────

/// CSS `<ratio>` 值 (§4.7)。
///
/// 语法:`<number [0,∞]> [ / <number [0,∞]> ]?`
/// 单数字时 height 默认 1.0。
#[derive(Debug, Clone, PartialEq)]
pub struct Ratio {
    pub width: f64,
    pub height: f64,
}

impl Ratio {
    pub fn parse(input: &str) -> Result<Self, ParseError> {
        let cvs = muskitty_css::parse_list_of_component_values(input);
        Self::from_cvs(&cvs)
    }

    pub fn from_cvs(cvs: &[ComponentValue]) -> Result<Self, ParseError> {
        // 过滤 whitespace,收集非 ws 的 component values
        let filtered: Vec<&ComponentValue> = cvs
            .iter()
            .filter(|cv| !matches!(cv, ComponentValue::PreservedToken(Token::Whitespace)))
            .collect();

        if filtered.is_empty() {
            return Err(ParseError::new("ratio requires at least one number"));
        }

        // 第一个必须是 number
        let width = match filtered[0] {
            ComponentValue::PreservedToken(Token::Number(numeric)) => numeric.value,
            _ => return Err(ParseError::new("ratio first value must be a number")),
        };

        if width < 0.0 {
            return Err(ParseError::new("ratio values must be non-negative"));
        }

        // 只有一个 number → height = 1
        if filtered.len() == 1 {
            return Ok(Ratio { width, height: 1.0 });
        }

        // 两个元素:必须是 `/` + number
        if filtered.len() != 3 {
            return Err(ParseError::new(
                "ratio with second value must be 'number / number'",
            ));
        }

        if !matches!(
            filtered[1],
            ComponentValue::PreservedToken(Token::Delim('/'))
        ) {
            return Err(ParseError::new("expected '/' between ratio numbers"));
        }

        let height = match filtered[2] {
            ComponentValue::PreservedToken(Token::Number(numeric)) => numeric.value,
            _ => return Err(ParseError::new("ratio second value must be a number")),
        };

        if height < 0.0 {
            return Err(ParseError::new("ratio values must be non-negative"));
        }

        Ok(Ratio { width, height })
    }
}