Skip to content

Commit 6d7bd9d

Browse files
committed
Inline fields of Token::Percentage to align with Number and Dimension
1 parent c3f7c75 commit 6d7bd9d

File tree

7 files changed

+28
-33
lines changed

7 files changed

+28
-33
lines changed

src/color.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,8 @@ fn parse_color_function<'i, 't>(name: &str, arguments: &mut Parser<'i, 't>) -> R
432432
Token::Number { value: v, .. } => {
433433
clamp_unit_f32(v)
434434
}
435-
Token::Percentage(ref v) => {
436-
clamp_unit_f32(v.unit_value)
435+
Token::Percentage { unit_value: v, .. } => {
436+
clamp_unit_f32(v)
437437
}
438438
t => {
439439
return Err(BasicParseError::UnexpectedToken(t))
@@ -473,10 +473,10 @@ fn parse_rgb_components_rgb<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
473473
}
474474
blue = clamp_floor_256_f32(try!(arguments.expect_number()));
475475
}
476-
Token::Percentage(ref v) => {
477-
red = clamp_unit_f32(v.unit_value);
476+
Token::Percentage { unit_value, .. } => {
477+
red = clamp_unit_f32(unit_value);
478478
green = clamp_unit_f32(match try!(arguments.next()) {
479-
Token::Percentage(ref v) => v.unit_value,
479+
Token::Percentage { unit_value, .. } => unit_value,
480480
Token::Comma => {
481481
uses_commas = true;
482482
try!(arguments.expect_percentage())
@@ -520,7 +520,7 @@ fn parse_rgb_components_hsl<'i, 't>(arguments: &mut Parser<'i, 't>) -> Result<(u
520520
// Saturation and lightness are clamped to 0% ... 100%
521521
// https://drafts.csswg.org/css-color/#the-hsl-notation
522522
let saturation = match try!(arguments.next()) {
523-
Token::Percentage(ref v) => v.unit_value,
523+
Token::Percentage { unit_value, .. } => unit_value,
524524
Token::Comma => {
525525
uses_commas = true;
526526
try!(arguments.expect_percentage())

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn parse_border_spacing(_context: &ParserContext, input: &mut Parser)
8080

8181
pub use cssparser_macros::*;
8282

83-
pub use tokenizer::{Token, PercentageValue, SourceLocation};
83+
pub use tokenizer::{Token, SourceLocation};
8484
pub use rules_and_declarations::{parse_important};
8585
pub use rules_and_declarations::{DeclarationParser, DeclarationListParser, parse_one_declaration};
8686
pub use rules_and_declarations::{RuleListParser, parse_one_rule};

src/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use compact_cow_str::CompactCowStr;
66
use std::ops::Range;
77
use std::ascii::AsciiExt;
88
use std::ops::BitOr;
9-
use tokenizer::{self, Token, PercentageValue, Tokenizer, SourceLocation};
9+
use tokenizer::{self, Token, Tokenizer, SourceLocation};
1010

1111

1212
/// A capture of the internal state of a `Parser` (including the position within the input),
@@ -529,7 +529,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
529529
#[inline]
530530
pub fn expect_percentage(&mut self) -> Result<f32, BasicParseError<'i>> {
531531
match try!(self.next()) {
532-
Token::Percentage(PercentageValue { unit_value, .. }) => Ok(unit_value),
532+
Token::Percentage { unit_value, .. } => Ok(unit_value),
533533
t => Err(BasicParseError::UnexpectedToken(t))
534534
}
535535
}

src/serializer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use std::ascii::AsciiExt;
66
use std::fmt::{self, Write};
77

8-
use super::{Token, PercentageValue};
8+
use super::Token;
99

1010

1111
/// Trait for things the can serialize themselves in CSS syntax.
@@ -91,7 +91,7 @@ impl<'a> ToCss for Token<'a> {
9191
Token::Number { value, int_value, has_sign } => {
9292
try!(write_numeric(value, int_value, has_sign, dest))
9393
}
94-
Token::Percentage(PercentageValue { unit_value, int_value, has_sign }) => {
94+
Token::Percentage { unit_value, int_value, has_sign } => {
9595
try!(write_numeric(unit_value * 100., int_value, has_sign, dest));
9696
try!(dest.write_str("%"));
9797
},
@@ -388,7 +388,7 @@ impl<'a> Token<'a> {
388388
Token::Delim('/') => DelimSlash,
389389
Token::Delim('*') => DelimAsterisk,
390390
Token::Number { .. } => Number,
391-
Token::Percentage(_) => Percentage,
391+
Token::Percentage { .. } => Percentage,
392392
Token::Dimension { .. } => Dimension,
393393
Token::WhiteSpace(_) => WhiteSpace,
394394
Token::Comment(_) => DelimSlash,

src/size_of_tests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use compact_cow_str::CompactCowStr;
66
use std::borrow::Cow;
7-
use tokenizer::{Token, PercentageValue};
7+
use tokenizer::Token;
88

99
#[macro_export]
1010
macro_rules! size_of_test {
@@ -33,6 +33,5 @@ macro_rules! size_of_test {
3333

3434
// These assume 64-bit
3535
size_of_test!(token, Token, 32);
36-
size_of_test!(percentage_value, PercentageValue, 16);
3736
size_of_test!(std_cow_str, Cow<'static, str>, 32);
3837
size_of_test!(compact_cow_str, CompactCowStr, 16);

src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_serialize::json::{self, Json, ToJson};
1111
#[cfg(feature = "bench")]
1212
use self::test::Bencher;
1313

14-
use super::{Parser, Delimiter, Token, PercentageValue, SourceLocation, ParseError,
14+
use super::{Parser, Delimiter, Token, SourceLocation, ParseError,
1515
DeclarationListParser, DeclarationParser, RuleListParser, BasicParseError,
1616
AtRuleType, AtRuleParser, QualifiedRuleParser, ParserInput,
1717
parse_one_declaration, parse_one_rule, parse_important,
@@ -802,7 +802,7 @@ fn one_component_value_to_json(token: Token, input: &mut Parser) -> Json {
802802
v.extend(numeric(value, int_value, has_sign));
803803
v
804804
}),
805-
Token::Percentage(PercentageValue { unit_value, int_value, has_sign }) => Json::Array({
805+
Token::Percentage { unit_value, int_value, has_sign } => Json::Array({
806806
let mut v = vec!["percentage".to_json()];
807807
v.extend(numeric(unit_value * 100., int_value, has_sign));
808808
v

src/tokenizer.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,17 @@ pub enum Token<'a> {
6767
},
6868

6969
/// A [`<percentage-token>`](https://drafts.csswg.org/css-syntax/#percentage-token-diagram)
70-
Percentage(PercentageValue),
70+
Percentage {
71+
/// Whether the number had a `+` or `-` sign.
72+
has_sign: bool,
73+
74+
/// The value as a float, divided by 100 so that the nominal range is 0.0 to 1.0.
75+
unit_value: f32,
76+
77+
/// If the origin source did not include a fractional part, the value as an integer.
78+
/// It is **not** divided by 100.
79+
int_value: Option<i32>,
80+
},
7181

7282
/// A [`<dimension-token>`](https://drafts.csswg.org/css-syntax/#dimension-token-diagram)
7383
Dimension {
@@ -190,20 +200,6 @@ impl<'a> Token<'a> {
190200
}
191201

192202

193-
/// The numeric value of `Percentage` tokens.
194-
#[derive(PartialEq, Debug, Copy, Clone)]
195-
pub struct PercentageValue {
196-
/// The value as a float, divided by 100 so that the nominal range is 0.0 to 1.0.
197-
pub unit_value: f32,
198-
199-
/// If the origin source did not include a fractional part, the value as an integer. It is **not** divided by 100.
200-
pub int_value: Option<i32>,
201-
202-
/// Whether the number had a `+` or `-` sign.
203-
pub has_sign: bool,
204-
}
205-
206-
207203
#[derive(Clone)]
208204
pub struct Tokenizer<'a> {
209205
input: &'a str,
@@ -864,11 +860,11 @@ fn consume_numeric<'a>(tokenizer: &mut Tokenizer<'a>) -> Token<'a> {
864860

865861
if !tokenizer.is_eof() && tokenizer.next_byte_unchecked() == b'%' {
866862
tokenizer.advance(1);
867-
return Percentage(PercentageValue {
863+
return Percentage {
868864
unit_value: (value / 100.) as f32,
869865
int_value: int_value,
870866
has_sign: has_sign,
871-
})
867+
}
872868
}
873869
let value = value as f32;
874870
if is_ident_start(tokenizer) {

0 commit comments

Comments
 (0)