1
+ use std:: borrow:: Cow ;
2
+
1
3
mod ascii;
2
4
3
5
/// https://drafts.css-houdini.org/css-properties-values-api-1/#parsing-syntax
@@ -22,19 +24,45 @@ pub enum ParseError {
22
24
}
23
25
24
26
/// https://drafts.css-houdini.org/css-properties-values-api-1/#multipliers
25
- #[ derive( Debug , PartialEq ) ]
27
+ #[ derive( Clone , Copy , Debug , PartialEq ) ]
26
28
pub enum Multiplier {
27
29
Space ,
28
30
Comma ,
29
31
}
30
32
31
- #[ derive( Debug , PartialEq ) ]
33
+ #[ derive( Clone , Debug , PartialEq ) ]
32
34
pub struct Component {
33
- pub name : ComponentName ,
34
- pub multiplier : Option < Multiplier > ,
35
+ name : ComponentName ,
36
+ multiplier : Option < Multiplier > ,
35
37
}
36
38
37
- #[ derive( Debug , PartialEq ) ]
39
+ impl Component {
40
+ #[ inline]
41
+ pub fn name ( & self ) -> & ComponentName {
42
+ & self . name
43
+ }
44
+
45
+ #[ inline]
46
+ pub fn multiplier ( & self ) -> Option < Multiplier > {
47
+ self . multiplier
48
+ }
49
+
50
+ #[ inline]
51
+ pub fn unpremultipied ( & self ) -> Cow < Self > {
52
+ match self . name . unpremultiply ( ) {
53
+ Some ( component) => {
54
+ debug_assert ! (
55
+ self . multiplier. is_none( ) ,
56
+ "Shouldn't have parsed a multiplier for a pre-multiplied data type name" ,
57
+ ) ;
58
+ Cow :: Owned ( component)
59
+ }
60
+ None => Cow :: Borrowed ( self ) ,
61
+ }
62
+ }
63
+ }
64
+
65
+ #[ derive( Clone , Debug , PartialEq ) ]
38
66
pub struct CustomIdent ( Box < [ u8 ] > ) ;
39
67
40
68
impl CustomIdent {
@@ -51,35 +79,75 @@ impl CustomIdent {
51
79
}
52
80
53
81
54
- #[ derive( Debug , PartialEq ) ]
82
+ #[ derive( Clone , Debug , PartialEq ) ]
55
83
pub enum ComponentName {
56
84
DataType ( DataType ) ,
57
85
Ident ( CustomIdent ) ,
58
86
}
59
87
60
- impl DataType {
61
- fn is_pre_multiplied ( & self ) -> bool {
62
- false
88
+ impl ComponentName {
89
+ fn unpremultiply ( & self ) -> Option < Component > {
90
+ match * self {
91
+ ComponentName :: DataType ( ref t) => t. unpremultiply ( ) ,
92
+ ComponentName :: Ident ( ..) => None ,
93
+ }
63
94
}
64
- }
65
95
66
- impl ComponentName {
67
96
/// https://drafts.css-houdini.org/css-properties-values-api-1/#pre-multiplied-data-type-name
68
97
fn is_pre_multiplied ( & self ) -> bool {
98
+ self . unpremultiply ( ) . is_some ( )
99
+ }
100
+ }
101
+
102
+ #[ derive( Clone , Copy , Debug , PartialEq ) ]
103
+ pub enum DataType {
104
+ Length ,
105
+ Number ,
106
+ Percentage ,
107
+ LengthPercentage ,
108
+ Color ,
109
+ Image ,
110
+ Url ,
111
+ Integer ,
112
+ Angle ,
113
+ Time ,
114
+ Resolution ,
115
+ TransformFunction ,
116
+ TransformList ,
117
+ CustomIdent ,
118
+ }
119
+
120
+ impl DataType {
121
+ fn unpremultiply ( & self ) -> Option < Component > {
69
122
match * self {
70
- ComponentName :: DataType ( ref t) => t. is_pre_multiplied ( ) ,
71
- ComponentName :: Ident ( ..) => false ,
123
+ DataType :: TransformList => Some ( Component {
124
+ name : ComponentName :: DataType ( DataType :: TransformFunction ) ,
125
+ multiplier : Some ( Multiplier :: Space ) ,
126
+ } ) ,
127
+ _ => None ,
72
128
}
73
129
}
74
130
}
75
131
76
- #[ derive( Debug , PartialEq ) ]
77
- pub enum DataType { }
78
-
79
132
impl DataType {
80
- fn from_bytes ( _: & [ u8 ] ) -> Option < Self > {
81
- // TODO
82
- None
133
+ fn from_bytes ( ty : & [ u8 ] ) -> Option < Self > {
134
+ Some ( match ty {
135
+ b"length" => DataType :: Length ,
136
+ b"number" => DataType :: Number ,
137
+ b"percentage" => DataType :: Percentage ,
138
+ b"length-percentage" => DataType :: LengthPercentage ,
139
+ b"color" => DataType :: Color ,
140
+ b"image" => DataType :: Image ,
141
+ b"url" => DataType :: Url ,
142
+ b"integer" => DataType :: Integer ,
143
+ b"angle" => DataType :: Angle ,
144
+ b"time" => DataType :: Time ,
145
+ b"resolution" => DataType :: Resolution ,
146
+ b"transform-function" => DataType :: TransformFunction ,
147
+ b"custom-ident" => DataType :: CustomIdent ,
148
+ b"transform-list" => DataType :: TransformList ,
149
+ _ => return None ,
150
+ } )
83
151
}
84
152
}
85
153
0 commit comments