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
use cssparser::Token;
/// A property value token which was parsed from a CSS rule.
#[derive(Clone, Debug)]
#[derive(PartialEq, PartialOrd)]
pub enum PropertyToken
{
/// A value which was parsed percent value, like `100%` or `73.23%`.
Percentage(f32),
/// A value which was parsed dimension value, like `10px` or `35em.
///
/// Currently there is no distinction between [`length-values`](https://developer.mozilla.org/en-US/docs/Web/CSS/length).
Dimension(f32),
/// A numeric float value, like `31.1` or `43`.
Number(f32),
/// A plain identifier, like `none` or `center`.
Identifier(String),
/// A identifier prefixed by a hash, like `#001122`.
Hash(String),
/// A quoted string, like `"some value"`.
String(String),
}
impl<'i> TryFrom<Token<'i>>
for PropertyToken
{
type Error = ();
fn try_from(
token: Token<'i>
) -> Result<Self, Self::Error> {
match token
{
Token::Ident(val) => Ok(Self::Identifier(val.to_string())),
Token::Hash(val) => Ok(Self::Hash(val.to_string())),
Token::IDHash(val) => Ok(Self::Hash(val.to_string())),
Token::QuotedString(val) => Ok(Self::String(val.to_string())),
Token::Number { value, .. } => Ok(Self::Number(value)),
Token::Percentage { unit_value, .. } => Ok(Self::Percentage(unit_value * 100.0)),
Token::Dimension { value, .. } => Ok(Self::Dimension(value)),
_ => Err(()),
}
}
}