Skip to content

Commit 8900872

Browse files
committed
Handle premultiplied data types.
1 parent 5d5ac9f commit 8900872

File tree

1 file changed

+87
-19
lines changed

1 file changed

+87
-19
lines changed

src/lib.rs

Lines changed: 87 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::borrow::Cow;
2+
13
mod ascii;
24

35
/// https://drafts.css-houdini.org/css-properties-values-api-1/#parsing-syntax
@@ -22,19 +24,45 @@ pub enum ParseError {
2224
}
2325

2426
/// https://drafts.css-houdini.org/css-properties-values-api-1/#multipliers
25-
#[derive(Debug, PartialEq)]
27+
#[derive(Clone, Copy, Debug, PartialEq)]
2628
pub enum Multiplier {
2729
Space,
2830
Comma,
2931
}
3032

31-
#[derive(Debug, PartialEq)]
33+
#[derive(Clone, Debug, PartialEq)]
3234
pub struct Component {
33-
pub name: ComponentName,
34-
pub multiplier: Option<Multiplier>,
35+
name: ComponentName,
36+
multiplier: Option<Multiplier>,
3537
}
3638

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)]
3866
pub struct CustomIdent(Box<[u8]>);
3967

4068
impl CustomIdent {
@@ -51,35 +79,75 @@ impl CustomIdent {
5179
}
5280

5381

54-
#[derive(Debug, PartialEq)]
82+
#[derive(Clone, Debug, PartialEq)]
5583
pub enum ComponentName {
5684
DataType(DataType),
5785
Ident(CustomIdent),
5886
}
5987

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+
}
6394
}
64-
}
6595

66-
impl ComponentName {
6796
/// https://drafts.css-houdini.org/css-properties-values-api-1/#pre-multiplied-data-type-name
6897
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> {
69122
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,
72128
}
73129
}
74130
}
75131

76-
#[derive(Debug, PartialEq)]
77-
pub enum DataType {}
78-
79132
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+
})
83151
}
84152
}
85153

0 commit comments

Comments
 (0)