Skip to content

Commit 0a35da4

Browse files
committed
Add rust docs for CSS values
1 parent fdb9bed commit 0a35da4

29 files changed

+996
-264
lines changed

src/macros.rs

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,97 +2,111 @@ macro_rules! enum_property {
22
(
33
$(#[$outer:meta])*
44
$vis:vis enum $name:ident {
5-
$( $x: ident, )+
5+
$(
6+
$(#[$meta: meta])*
7+
$x: ident,
8+
)+
69
}
710
) => {
811
$(#[$outer])*
912
#[derive(Debug, Clone, Copy, PartialEq)]
1013
$vis enum $name {
1114
$(
15+
$(#[$meta])*
1216
$x,
1317
)+
1418
}
1519

1620
impl<'i> Parse<'i> for $name {
1721
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
22+
let location = input.current_source_location();
1823
let ident = input.expect_ident()?;
1924
match &ident[..] {
2025
$(
2126
s if s.eq_ignore_ascii_case(stringify!($x)) => Ok($name::$x),
2227
)+
23-
_ => return Err(input.new_error(BasicParseErrorKind::QualifiedRuleInvalid))
28+
_ => Err(location.new_unexpected_token_error(
29+
cssparser::Token::Ident(ident.clone())
30+
))
2431
}
2532
}
26-
}
2733

28-
impl ToCss for $name {
29-
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write {
30-
use $name::*;
31-
match self {
34+
fn parse_string(input: &'i str) -> Result<Self, ParseError<'i, ParserError<'i>>> {
35+
match input {
3236
$(
33-
$x => dest.write_str(&stringify!($x).to_lowercase()),
37+
s if s.eq_ignore_ascii_case(stringify!($x)) => Ok($name::$x),
3438
)+
39+
_ => return Err(ParseError {
40+
kind: ParseErrorKind::Basic(BasicParseErrorKind::UnexpectedToken(cssparser::Token::Ident(input.into()))),
41+
location: cssparser::SourceLocation { line: 0, column: 1 }
42+
})
3543
}
3644
}
3745
}
3846

39-
impl $name {
40-
#[allow(dead_code)]
41-
pub fn from_str(s: &str) -> Option<Self> {
42-
match s {
47+
impl ToCss for $name {
48+
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write {
49+
use $name::*;
50+
match self {
4351
$(
44-
s if s.eq_ignore_ascii_case(stringify!($x)) => Some($name::$x),
52+
$x => dest.write_str(&stringify!($x).to_lowercase()),
4553
)+
46-
_ => None
4754
}
4855
}
4956
}
5057
};
5158
(
5259
$(#[$outer:meta])*
5360
$vis:vis enum $name:ident {
54-
$( $str: literal: $id: ident, )+
61+
$(
62+
$(#[$meta: meta])*
63+
$str: literal: $id: ident,
64+
)+
5565
}
5666
) => {
5767
$(#[$outer])*
5868
#[derive(Debug, Clone, Copy, PartialEq)]
5969
$vis enum $name {
6070
$(
71+
$(#[$meta])*
6172
$id,
6273
)+
6374
}
6475

6576
impl<'i> Parse<'i> for $name {
6677
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
78+
let location = input.current_source_location();
6779
let ident = input.expect_ident()?;
6880
match &ident[..] {
6981
$(
7082
s if s.eq_ignore_ascii_case($str) => Ok($name::$id),
7183
)+
72-
_ => return Err(input.new_error(BasicParseErrorKind::QualifiedRuleInvalid))
84+
_ => Err(location.new_unexpected_token_error(
85+
cssparser::Token::Ident(ident.clone())
86+
))
7387
}
7488
}
75-
}
7689

77-
impl ToCss for $name {
78-
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write {
79-
use $name::*;
80-
match self {
90+
fn parse_string(input: &'i str) -> Result<Self, ParseError<'i, ParserError<'i>>> {
91+
match input {
8192
$(
82-
$id => dest.write_str($str),
93+
s if s.eq_ignore_ascii_case($str) => Ok($name::$id),
8394
)+
95+
_ => return Err(ParseError {
96+
kind: ParseErrorKind::Basic(BasicParseErrorKind::UnexpectedToken(cssparser::Token::Ident(input.into()))),
97+
location: cssparser::SourceLocation { line: 0, column: 1 }
98+
})
8499
}
85100
}
86101
}
87102

88-
impl $name {
89-
#[allow(dead_code)]
90-
pub fn from_str(s: &str) -> Option<Self> {
91-
match s {
103+
impl ToCss for $name {
104+
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write {
105+
use $name::*;
106+
match self {
92107
$(
93-
s if s.eq_ignore_ascii_case($str) => Some($name::$id),
108+
$id => dest.write_str($str),
94109
)+
95-
_ => None
96110
}
97111
}
98112
}

src/properties/animation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,17 @@ impl<'i> ToCss for Animation<'i> {
204204
self.iteration_count.to_css(dest)?;
205205
}
206206

207-
if self.direction != AnimationDirection::Normal || AnimationDirection::from_str(&name.0).is_some() {
207+
if self.direction != AnimationDirection::Normal || AnimationDirection::parse_string(&name.0).is_ok() {
208208
dest.write_char(' ')?;
209209
self.direction.to_css(dest)?;
210210
}
211211

212-
if self.fill_mode != AnimationFillMode::None || AnimationFillMode::from_str(&name.0).is_some() {
212+
if self.fill_mode != AnimationFillMode::None || AnimationFillMode::parse_string(&name.0).is_ok() {
213213
dest.write_char(' ')?;
214214
self.fill_mode.to_css(dest)?;
215215
}
216216

217-
if self.play_state != AnimationPlayState::Running || AnimationPlayState::from_str(&name.0).is_some() {
217+
if self.play_state != AnimationPlayState::Running || AnimationPlayState::parse_string(&name.0).is_ok() {
218218
dest.write_char(' ')?;
219219
self.play_state.to_css(dest)?;
220220
}

src/properties/border_image.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use crate::{
1313
traits::FallbackValues,
1414
values::{
1515
length::*,
16-
number::serialize_number,
1716
percentage::{NumberOrPercentage, Percentage},
1817
},
1918
};
@@ -101,7 +100,7 @@ impl ToCss for BorderImageSideWidth {
101100
match self {
102101
Auto => dest.write_str("auto"),
103102
LengthPercentage(l) => l.to_css(dest),
104-
Number(n) => serialize_number(*n, dest),
103+
Number(n) => n.to_css(dest),
105104
}
106105
}
107106
}

src/properties/font.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ impl<'i> ToCss for FontFamily<'i> {
330330
// Generic family names such as sans-serif must be quoted if parsed as a string.
331331
// CSS wide keywords, as well as "default", must also be quoted.
332332
// https://www.w3.org/TR/css-fonts-4/#family-name-syntax
333-
if !val.is_empty() && GenericFontFamily::from_str(val).is_none() {
333+
if !val.is_empty() && !GenericFontFamily::parse_string(val).is_ok() {
334334
let mut id = String::new();
335335
let mut first = true;
336336
for slice in val.split(' ') {

src/properties/grid.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::properties::{Property, PropertyId};
88
use crate::traits::{Parse, PropertyHandler, ToCss};
99
use crate::values::ident::CustomIdent;
1010
use crate::values::length::serialize_dimension;
11-
use crate::values::number::serialize_integer;
11+
use crate::values::number::CSSInteger;
1212
use crate::values::{ident::CustomIdentList, length::LengthPercentage};
1313
use bitflags::bitflags;
1414
use cssparser::*;
@@ -1021,8 +1021,8 @@ impl ToCss for Grid<'_> {
10211021
pub enum GridLine<'i> {
10221022
Auto,
10231023
Ident(CustomIdent<'i>),
1024-
Line(i32, Option<CustomIdent<'i>>),
1025-
Span(i32, Option<CustomIdent<'i>>),
1024+
Line(CSSInteger, Option<CustomIdent<'i>>),
1025+
Span(CSSInteger, Option<CustomIdent<'i>>),
10261026
}
10271027

10281028
impl<'i> Parse<'i> for GridLine<'i> {
@@ -1033,11 +1033,11 @@ impl<'i> Parse<'i> for GridLine<'i> {
10331033

10341034
if input.try_parse(|input| input.expect_ident_matching("span")).is_ok() {
10351035
// TODO: is calc() supported here??
1036-
let (line_number, ident) = if let Ok(line_number) = input.try_parse(|input| input.expect_integer()) {
1036+
let (line_number, ident) = if let Ok(line_number) = input.try_parse(CSSInteger::parse) {
10371037
let ident = input.try_parse(CustomIdent::parse).ok();
10381038
(line_number, ident)
10391039
} else if let Ok(ident) = input.try_parse(CustomIdent::parse) {
1040-
let line_number = input.try_parse(|input| input.expect_integer()).unwrap_or(1);
1040+
let line_number = input.try_parse(CSSInteger::parse).unwrap_or(1);
10411041
(line_number, Some(ident))
10421042
} else {
10431043
return Err(input.new_custom_error(ParserError::InvalidDeclaration));
@@ -1050,7 +1050,7 @@ impl<'i> Parse<'i> for GridLine<'i> {
10501050
return Ok(GridLine::Span(line_number, ident));
10511051
}
10521052

1053-
if let Ok(line_number) = input.try_parse(|input| input.expect_integer()) {
1053+
if let Ok(line_number) = input.try_parse(CSSInteger::parse) {
10541054
if line_number == 0 {
10551055
return Err(input.new_custom_error(ParserError::InvalidDeclaration));
10561056
}
@@ -1059,7 +1059,7 @@ impl<'i> Parse<'i> for GridLine<'i> {
10591059
}
10601060

10611061
let ident = CustomIdent::parse(input)?;
1062-
if let Ok(line_number) = input.try_parse(|input| input.expect_integer()) {
1062+
if let Ok(line_number) = input.try_parse(CSSInteger::parse) {
10631063
if line_number == 0 {
10641064
return Err(input.new_custom_error(ParserError::InvalidDeclaration));
10651065
}
@@ -1079,7 +1079,7 @@ impl ToCss for GridLine<'_> {
10791079
GridLine::Auto => dest.write_str("auto"),
10801080
GridLine::Ident(id) => id.to_css(dest),
10811081
GridLine::Line(line_number, id) => {
1082-
serialize_integer(*line_number, dest)?;
1082+
line_number.to_css(dest)?;
10831083
if let Some(id) = id {
10841084
dest.write_char(' ')?;
10851085
id.to_css(dest)?;
@@ -1089,7 +1089,7 @@ impl ToCss for GridLine<'_> {
10891089
GridLine::Span(line_number, id) => {
10901090
dest.write_str("span ")?;
10911091
if *line_number != 1 || id.is_none() {
1092-
serialize_integer(*line_number, dest)?;
1092+
line_number.to_css(dest)?;
10931093
if id.is_some() {
10941094
dest.write_char(' ')?;
10951095
}

src/rules/font_palette_values.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::targets::Browsers;
88
use crate::traits::{Parse, ToCss};
99
use crate::values::color::{ColorFallbackKind, CssColor};
1010
use crate::values::ident::DashedIdent;
11-
use crate::values::number::serialize_integer;
11+
use crate::values::number::CSSInteger;
1212
use cssparser::*;
1313

1414
/// https://drafts.csswg.org/css-fonts-4/#font-palette-values
@@ -111,7 +111,7 @@ impl<'i> FontPaletteValuesRule<'i> {
111111

112112
impl<'i> Parse<'i> for BasePalette {
113113
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
114-
if let Ok(i) = input.try_parse(|input| input.expect_integer()) {
114+
if let Ok(i) = input.try_parse(CSSInteger::parse) {
115115
if i.is_negative() {
116116
return Err(input.new_custom_error(ParserError::InvalidValue));
117117
}
@@ -136,14 +136,14 @@ impl ToCss for BasePalette {
136136
match self {
137137
BasePalette::Light => dest.write_str("light"),
138138
BasePalette::Dark => dest.write_str("dark"),
139-
BasePalette::Integer(i) => serialize_integer(*i as i32, dest),
139+
BasePalette::Integer(i) => (*i as CSSInteger).to_css(dest),
140140
}
141141
}
142142
}
143143

144144
impl<'i> Parse<'i> for OverrideColors {
145145
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
146-
let index = input.expect_integer()?;
146+
let index = CSSInteger::parse(input)?;
147147
if index.is_negative() {
148148
return Err(input.new_custom_error(ParserError::InvalidValue));
149149
}
@@ -165,7 +165,7 @@ impl ToCss for OverrideColors {
165165
where
166166
W: std::fmt::Write,
167167
{
168-
serialize_integer(self.index as i32, dest)?;
168+
(self.index as CSSInteger).to_css(dest)?;
169169
dest.write_char(' ')?;
170170
self.color.to_css(dest)
171171
}

src/traits.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ pub trait Parse<'i>: Sized {
1818
fn parse_string(input: &'i str) -> Result<Self, ParseError<'i, ParserError<'i>>> {
1919
let mut input = ParserInput::new(input);
2020
let mut parser = Parser::new(&mut input);
21-
Self::parse(&mut parser)
21+
let result = Self::parse(&mut parser)?;
22+
parser.expect_exhausted()?;
23+
Ok(result)
2224
}
2325
}
2426

src/values/alpha.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
//! CSS alpha values, used to represent opacity.
2+
13
use super::percentage::NumberOrPercentage;
24
use crate::error::{ParserError, PrinterError};
35
use crate::printer::Printer;
46
use crate::traits::{Parse, ToCss};
57
use cssparser::*;
68

7-
/// https://www.w3.org/TR/2021/WD-css-color-4-20210601/#typedef-alpha-value
9+
/// A CSS [`<alpha-value>`](https://www.w3.org/TR/css-color-4/#typedef-alpha-value),
10+
/// used to represent opacity.
11+
///
12+
/// Parses either a `<number>` or `<percentage>`, but is always stored and serialized as a number.
813
#[derive(Debug, Clone, PartialEq)]
914
pub struct AlphaValue(pub f32);
1015

0 commit comments

Comments
 (0)