Skip to content

Commit cfca745

Browse files
committed
Nicer macro syntax for enum_property
1 parent eaff765 commit cfca745

24 files changed

+790
-615
lines changed

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7264,8 +7264,7 @@ mod tests {
72647264
minify_test(".foo { color: hwb(194 0% 0% / 50%) }", ".foo{color:#00c4ff80}");
72657265
minify_test(".foo { color: hwb(194 0% 50%) }", ".foo{color:#006280}");
72667266
minify_test(".foo { color: hwb(194 50% 0%) }", ".foo{color:#80e1ff}");
7267-
minify_test(".foo { color: hwb(194, 50%, 0%) }", ".foo{color:#80e1ff}");
7268-
minify_test(".foo { color: hwb(194, 50%, 50%) }", ".foo{color:gray}");
7267+
minify_test(".foo { color: hwb(194 50% 50%) }", ".foo{color:gray}");
72697268
// minify_test(".foo { color: ActiveText }", ".foo{color:ActiveTet}");
72707269

72717270
prefix_test(

src/macros.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
macro_rules! enum_property {
2-
($name: ident, $( $x: ident ),+) => {
2+
(
3+
$(#[$outer:meta])*
4+
$vis:vis enum $name:ident {
5+
$( $x: ident, )+
6+
}
7+
) => {
8+
$(#[$outer])*
39
#[derive(Debug, Clone, Copy, PartialEq)]
4-
pub enum $name {
10+
$vis enum $name {
511
$(
612
$x,
713
)+
@@ -42,9 +48,15 @@ macro_rules! enum_property {
4248
}
4349
}
4450
};
45-
($name: ident, $( ($str: expr, $id: ident) ),+) => {
51+
(
52+
$(#[$outer:meta])*
53+
$vis:vis enum $name:ident {
54+
$( $str: literal: $id: ident, )+
55+
}
56+
) => {
57+
$(#[$outer])*
4658
#[derive(Debug, Clone, Copy, PartialEq)]
47-
pub enum $name {
59+
$vis enum $name {
4860
$(
4961
$id,
5062
)+

src/media_query.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ impl ToCss for MediaList {
6161
}
6262
}
6363

64-
// <https://drafts.csswg.org/mediaqueries/#mq-prefix>
65-
enum_property!(Qualifier,
66-
Only,
67-
Not
68-
);
64+
enum_property! {
65+
/// <https://drafts.csswg.org/mediaqueries/#mq-prefix>
66+
pub enum Qualifier {
67+
Only,
68+
Not,
69+
}
70+
}
6971

7072
/// <http://dev.w3.org/csswg/mediaqueries-3/#media0>
7173
#[derive(Clone, Debug, PartialEq)]
@@ -170,11 +172,13 @@ impl ToCss for MediaQuery {
170172
}
171173
}
172174

173-
// A binary `and` or `or` operator.
174-
enum_property!(Operator,
175-
And,
176-
Or
177-
);
175+
enum_property! {
176+
/// A binary `and` or `or` operator.
177+
pub enum Operator {
178+
And,
179+
Or,
180+
}
181+
}
178182

179183
/// Represents a media condition.
180184
#[derive(Clone, Debug, PartialEq)]

src/properties/align.rs

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,34 @@ impl ToCss for BaselinePosition {
5050
}
5151
}
5252

53-
// https://www.w3.org/TR/2020/WD-css-align-3-20200421/#typedef-content-distribution
54-
enum_property!(ContentDistribution,
55-
("space-between", SpaceBetween),
56-
("space-around", SpaceAround),
57-
("space-evenly", SpaceEvenly),
58-
("stretch", Stretch)
59-
);
60-
61-
// https://www.w3.org/TR/2020/WD-css-align-3-20200421/#typedef-overflow-position
62-
enum_property!(OverflowPosition,
63-
Safe,
64-
Unsafe
65-
);
66-
67-
// https://www.w3.org/TR/2020/WD-css-align-3-20200421/#typedef-content-position
68-
enum_property!(ContentPosition,
69-
("center", Center),
70-
("start", Start),
71-
("end", End),
72-
("flex-start", FlexStart),
73-
("flex-end", FlexEnd)
74-
);
53+
enum_property! {
54+
/// https://www.w3.org/TR/2020/WD-css-align-3-20200421/#typedef-content-distribution
55+
pub enum ContentDistribution {
56+
"space-between": SpaceBetween,
57+
"space-around": SpaceAround,
58+
"space-evenly": SpaceEvenly,
59+
"stretch": Stretch,
60+
}
61+
}
62+
63+
enum_property! {
64+
/// https://www.w3.org/TR/2020/WD-css-align-3-20200421/#typedef-overflow-position
65+
pub enum OverflowPosition {
66+
Safe,
67+
Unsafe,
68+
}
69+
}
70+
71+
enum_property! {
72+
/// https://www.w3.org/TR/2020/WD-css-align-3-20200421/#typedef-content-position
73+
pub enum ContentPosition {
74+
"center": Center,
75+
"start": Start,
76+
"end": End,
77+
"flex-start": FlexStart,
78+
"flex-end": FlexEnd,
79+
}
80+
}
7581

7682
/// https://www.w3.org/TR/2020/WD-css-align-3-20200421/#propdef-align-content
7783
#[derive(Debug, Clone, PartialEq)]
@@ -238,16 +244,18 @@ impl ToCss for PlaceContent {
238244
}
239245
}
240246

241-
// https://www.w3.org/TR/2020/WD-css-align-3-20200421/#typedef-self-position
242-
enum_property!(SelfPosition,
243-
("center", Center),
244-
("start", Start),
245-
("end", End),
246-
("self-start", SelfStart),
247-
("self-end", SelfEnd),
248-
("flex-start", FlexStart),
249-
("flex-end", FlexEnd)
250-
);
247+
enum_property! {
248+
/// https://www.w3.org/TR/2020/WD-css-align-3-20200421/#typedef-self-position
249+
pub enum SelfPosition {
250+
"center": Center,
251+
"start": Start,
252+
"end": End,
253+
"self-start": SelfStart,
254+
"self-end": SelfEnd,
255+
"flex-start": FlexStart,
256+
"flex-end": FlexEnd,
257+
}
258+
}
251259

252260
/// https://www.w3.org/TR/2020/WD-css-align-3-20200421/#propdef-align-self
253261
#[derive(Debug, Clone, PartialEq)]

src/properties/animation.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,33 @@ impl ToCss for AnimationIterationCount {
7676
}
7777
}
7878

79-
// https://drafts.csswg.org/css-animations/#animation-direction
80-
enum_property!(AnimationDirection,
81-
("normal", Normal),
82-
("reverse", Reverse),
83-
("alternate", Alternate),
84-
("alternate-reverse", AlternateReverse)
85-
);
86-
87-
// https://drafts.csswg.org/css-animations/#animation-play-state
88-
enum_property!(AnimationPlayState,
89-
Running,
90-
Paused
91-
);
92-
93-
// https://drafts.csswg.org/css-animations/#animation-fill-mode
94-
enum_property!(AnimationFillMode,
95-
None,
96-
Forwards,
97-
Backwards,
98-
Both
99-
);
79+
enum_property! {
80+
/// https://drafts.csswg.org/css-animations/#animation-direction
81+
pub enum AnimationDirection {
82+
"normal": Normal,
83+
"reverse": Reverse,
84+
"alternate": Alternate,
85+
"alternate-reverse": AlternateReverse,
86+
}
87+
}
88+
89+
enum_property! {
90+
/// https://drafts.csswg.org/css-animations/#animation-play-state
91+
pub enum AnimationPlayState {
92+
Running,
93+
Paused,
94+
}
95+
}
96+
97+
enum_property! {
98+
/// https://drafts.csswg.org/css-animations/#animation-fill-mode
99+
pub enum AnimationFillMode {
100+
None,
101+
Forwards,
102+
Backwards,
103+
Both,
104+
}
105+
}
100106

101107
/// https://drafts.csswg.org/css-animations/#animation
102108
#[derive(Debug, Clone, PartialEq)]

src/properties/background.rs

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ impl ToCss for BackgroundSize {
7575
}
7676
}
7777

78-
// https://www.w3.org/TR/css-backgrounds-3/#typedef-repeat-style
79-
enum_property!(BackgroundRepeatKeyword,
80-
("repeat", Repeat),
81-
("space", Space),
82-
("round", Round),
83-
("no-repeat", NoRepeat)
84-
);
78+
enum_property! {
79+
/// https://www.w3.org/TR/css-backgrounds-3/#typedef-repeat-style
80+
pub enum BackgroundRepeatKeyword {
81+
"repeat": Repeat,
82+
"space": Space,
83+
"round": Round,
84+
"no-repeat": NoRepeat,
85+
}
86+
}
8587

8688
/// https://www.w3.org/TR/css-backgrounds-3/#background-repeat
8789
#[derive(Debug, Clone, PartialEq)]
@@ -137,34 +139,40 @@ impl ToCss for BackgroundRepeat {
137139
}
138140
}
139141

140-
// https://www.w3.org/TR/css-backgrounds-3/#background-attachment
141-
enum_property!(BackgroundAttachment,
142-
Scroll,
143-
Fixed,
144-
Local
145-
);
142+
enum_property! {
143+
/// https://www.w3.org/TR/css-backgrounds-3/#background-attachment
144+
pub enum BackgroundAttachment {
145+
Scroll,
146+
Fixed,
147+
Local,
148+
}
149+
}
146150

147151
impl Default for BackgroundAttachment {
148152
fn default() -> BackgroundAttachment {
149153
BackgroundAttachment::Scroll
150154
}
151155
}
152156

153-
// https://www.w3.org/TR/css-backgrounds-3/#typedef-box
154-
enum_property!(BackgroundBox,
155-
("border-box", BorderBox),
156-
("padding-box", PaddingBox),
157-
("content-box", ContentBox)
158-
);
159-
160-
// https://drafts.csswg.org/css-backgrounds-4/#background-clip
161-
enum_property!(BackgroundClip,
162-
("border-box", BorderBox),
163-
("padding-box", PaddingBox),
164-
("content-box", ContentBox),
165-
("border", Border),
166-
("text", Text)
167-
);
157+
enum_property! {
158+
/// https://www.w3.org/TR/css-backgrounds-3/#typedef-box
159+
pub enum BackgroundBox {
160+
"border-box": BorderBox,
161+
"padding-box": PaddingBox,
162+
"content-box": ContentBox,
163+
}
164+
}
165+
166+
enum_property! {
167+
/// https://drafts.csswg.org/css-backgrounds-4/#background-clip
168+
pub enum BackgroundClip {
169+
"border-box": BorderBox,
170+
"padding-box": PaddingBox,
171+
"content-box": ContentBox,
172+
"border": Border,
173+
"text": Text,
174+
}
175+
}
168176

169177
impl PartialEq<BackgroundBox> for BackgroundClip {
170178
fn eq(&self, other: &BackgroundBox) -> bool {

src/properties/border.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,20 @@ impl ToCss for BorderSideWidth {
6161
}
6262
}
6363

64-
enum_property!(BorderStyle,
65-
Hidden,
66-
None,
67-
Inset,
68-
Groove,
69-
Outset,
70-
Ridge,
71-
Dotted,
72-
Dashed,
73-
Solid,
74-
Double
75-
);
64+
enum_property! {
65+
pub enum BorderStyle {
66+
Hidden,
67+
None,
68+
Inset,
69+
Groove,
70+
Outset,
71+
Ridge,
72+
Dotted,
73+
Dashed,
74+
Solid,
75+
Double,
76+
}
77+
}
7678

7779
impl Default for BorderStyle {
7880
fn default() -> BorderStyle {

src/properties/border_image.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ use crate::printer::Printer;
1212
use crate::error::{ParserError, PrinterError};
1313
use crate::logical::LogicalProperties;
1414

15-
// https://www.w3.org/TR/css-backgrounds-3/#border-image-repeat
16-
enum_property!(BorderImageRepeatKeyword,
17-
Stretch,
18-
Repeat,
19-
Round,
20-
Space
21-
);
15+
enum_property! {
16+
/// https://www.w3.org/TR/css-backgrounds-3/#border-image-repeat
17+
pub enum BorderImageRepeatKeyword {
18+
Stretch,
19+
Repeat,
20+
Round,
21+
Space,
22+
}
23+
}
2224

2325
#[derive(Debug, Clone, PartialEq)]
2426
pub struct BorderImageRepeat(pub BorderImageRepeatKeyword, pub BorderImageRepeatKeyword);

0 commit comments

Comments
 (0)