Skip to content

Commit 3ada1ce

Browse files
committed
Upgrade to rustc e09d98603 2014-11-18 and take advantage of namespaced enumes.
[breaking-change]
1 parent 2d0c04f commit 3ada1ce

File tree

9 files changed

+66
-58
lines changed

9 files changed

+66
-58
lines changed

src/ast.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,14 @@ pub struct AtRule {
107107

108108
#[deriving(PartialEq)]
109109
pub enum DeclarationListItem {
110-
Declaration_(Declaration),
111-
// A better idea for a name that means "at-rule" but is not "AtRule"?
112-
DeclAtRule(AtRule),
110+
Declaration(Declaration),
111+
AtRule(AtRule),
113112
}
114113

115114
#[deriving(PartialEq)]
116115
pub enum Rule {
117-
QualifiedRule_(QualifiedRule),
118-
AtRule_(AtRule),
116+
QualifiedRule(QualifiedRule),
117+
AtRule(AtRule),
119118
}
120119

121120
#[deriving(PartialEq)]
@@ -126,11 +125,11 @@ pub struct SyntaxError {
126125

127126
#[deriving(PartialEq, Show)]
128127
pub enum ErrorReason {
129-
ErrEmptyInput, // Parsing a single "thing", found only whitespace.
130-
ErrExtraInput, // Found more non-whitespace after parsing a single "thing".
131-
ErrMissingQualifiedRuleBlock, // EOF in a qualified rule prelude, before '{'
132-
ErrInvalidDeclarationSyntax,
133-
ErrInvalidBangImportantSyntax,
128+
EmptyInput, // Parsing a single "thing", found only whitespace.
129+
ExtraInput, // Found more non-whitespace after parsing a single "thing".
130+
MissingQualifiedRuleBlock, // EOF in a qualified rule prelude, before '{'
131+
InvalidDeclarationSyntax,
132+
InvalidBangImportantSyntax,
134133
// This is meant to be extended
135134
}
136135

@@ -159,7 +158,7 @@ pub struct SkipWhitespaceIterator<'a> {
159158
impl<'a> Iterator<&'a ComponentValue> for SkipWhitespaceIterator<'a> {
160159
fn next(&mut self) -> Option<&'a ComponentValue> {
161160
for component_value in self.iter_with_whitespace {
162-
if component_value != &WhiteSpace { return Some(component_value) }
161+
if component_value != &ComponentValue::WhiteSpace { return Some(component_value) }
163162
}
164163
None
165164
}
@@ -183,7 +182,7 @@ pub struct MoveSkipWhitespaceIterator {
183182
impl Iterator<ComponentValue> for MoveSkipWhitespaceIterator {
184183
fn next(&mut self) -> Option<ComponentValue> {
185184
for component_value in self.iter_with_whitespace {
186-
if component_value != WhiteSpace { return Some(component_value) }
185+
if component_value != ComponentValue::WhiteSpace { return Some(component_value) }
187186
}
188187
None
189188
}

src/color.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use std::ascii::AsciiExt;
66
use std::fmt;
77
use std::num::{Float, FloatMath};
88

9-
use ast::*;
9+
use ast::{ComponentValue, SkipWhitespaceIterable};
10+
use ast::ComponentValue::{Number, Percentage, Function, Ident, Hash, IDHash, Comma};
1011

1112

1213
#[deriving(Clone, PartialEq)]
@@ -34,14 +35,14 @@ impl fmt::Show for RGBA {
3435
#[deriving(Clone, PartialEq)]
3536
pub enum Color {
3637
CurrentColor,
37-
RGBAColor(RGBA),
38+
RGBA(RGBA),
3839
}
3940

4041
impl fmt::Show for Color {
4142
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4243
match self {
43-
&CurrentColor => write!(f, "currentColor"),
44-
&RGBAColor(c) => write!(f, "{}", c),
44+
&Color::CurrentColor => write!(f, "currentColor"),
45+
&Color::RGBA(c) => write!(f, "{}", c),
4546
}
4647
}
4748
}
@@ -214,11 +215,11 @@ fn parse_color_keyword(value: &str) -> Result<Color, ()> {
214215
"whitesmoke" => (245., 245., 245.),
215216
"yellowgreen" => (154., 205., 50.),
216217

217-
"transparent" => return Ok(RGBAColor(RGBA { red: 0., green: 0., blue: 0., alpha: 0. })),
218-
"currentcolor" => return Ok(CurrentColor),
218+
"transparent" => return Ok(Color::RGBA(RGBA { red: 0., green: 0., blue: 0., alpha: 0. })),
219+
"currentcolor" => return Ok(Color::CurrentColor),
219220
_ => return Err(()),
220221
};
221-
Ok(RGBAColor(RGBA { red: r / 255., green: g / 255., blue: b / 255., alpha: 1. }))
222+
Ok(Color::RGBA(RGBA { red: r / 255., green: g / 255., blue: b / 255., alpha: 1. }))
222223
}
223224

224225

@@ -237,10 +238,10 @@ fn parse_color_hash(value: &str) -> Result<Color, ()> {
237238
)
238239
macro_rules! to_rgba(
239240
($r: expr, $g: expr, $b: expr,) => {
240-
Ok(RGBAColor(RGBA { red: $r as f32 / 255.,
241-
green: $g as f32 / 255.,
242-
blue: $b as f32 / 255.,
243-
alpha: 1. }))
241+
Ok(Color::RGBA(RGBA { red: $r as f32 / 255.,
242+
green: $g as f32 / 255.,
243+
blue: $b as f32 / 255.,
244+
alpha: 1. }))
244245
};
245246
)
246247

@@ -351,7 +352,7 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])
351352
1.
352353
};
353354
if iter.next().is_none() {
354-
Ok(RGBAColor(RGBA { red: red, green: green, blue: blue, alpha: alpha }))
355+
Ok(Color::RGBA(RGBA { red: red, green: green, blue: blue, alpha: alpha }))
355356
} else {
356357
Err(())
357358
}

src/from_bytes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::cmp;
66

77
use encoding::label::encoding_from_whatwg_label;
88
use encoding::all::UTF_8;
9-
use encoding::{EncodingRef, DecodeReplace, decode};
9+
use encoding::{EncodingRef, DecoderTrap, decode};
1010

1111
use tokenizer::{tokenize, Tokenizer};
1212
use parser::{parse_stylesheet_rules, StylesheetParser};
@@ -65,7 +65,7 @@ pub fn decode_stylesheet_bytes(css: &[u8], protocol_encoding_label: Option<&str>
6565

6666
#[inline]
6767
fn decode_replace(input: &[u8], fallback_encoding: EncodingRef)-> (String, EncodingRef) {
68-
let (result, used_encoding) = decode(input, DecodeReplace, fallback_encoding);
68+
let (result, used_encoding) = decode(input, DecoderTrap::Replace, fallback_encoding);
6969
(result.unwrap(), used_encoding)
7070
}
7171

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use parser::{parse_stylesheet_rules, StylesheetParser,
2121
parse_declaration_list, DeclarationListParser,
2222
parse_one_rule, parse_one_declaration, parse_one_component_value};
2323
pub use from_bytes::{decode_stylesheet_bytes, parse_stylesheet_rules_from_bytes};
24-
pub use color::{RGBA, Color, CurrentColor, RGBAColor};
24+
pub use color::{RGBA, Color};
2525
pub use nth::parse_nth;
2626
pub use serializer::{ToCss, serialize_identifier, serialize_string};
2727

src/nth.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
use std::ascii::AsciiExt;
66

7-
use ast::*;
7+
use ast::{ComponentValue, NumericValue, SkipWhitespaceIterator, SkipWhitespaceIterable};
8+
use ast::ComponentValue::{Number, Dimension, Ident, Delim};
89

910

1011
/// Parse the *An+B* notation, as found in the `:nth-child()` selector.

src/parser.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use std::iter::Iterator;
1818
use std::ascii::AsciiExt;
1919

2020
use ast::*;
21+
use ast::ComponentValue::{WhiteSpace, CDC, CDO, Ident, Delim, AtKeyword,
22+
Semicolon, Colon, CurlyBracketBlock};
2123

2224

2325
pub struct StylesheetParser<T>{ iter: T }
@@ -54,12 +56,12 @@ pub fn parse_declaration_list<T: Iterator<Node>>(iter: T) -> DeclarationListPars
5456
pub fn parse_one_rule<T: Iterator<Node>>(iter: T) -> Result<Rule, SyntaxError> {
5557
let mut parser = RuleListParser{ iter: iter };
5658
match parser.next() {
57-
None => error(START_LOCATION, ErrEmptyInput),
59+
None => error(START_LOCATION, ErrorReason::EmptyInput),
5860
Some(result) => {
5961
if result.is_err() { result }
6062
else { match next_non_whitespace(&mut parser.iter) {
6163
None => result,
62-
Some((_component_value, location)) => error(location, ErrExtraInput),
64+
Some((_component_value, location)) => error(location, ErrorReason::ExtraInput),
6365
}}
6466
}
6567
}
@@ -70,13 +72,13 @@ pub fn parse_one_rule<T: Iterator<Node>>(iter: T) -> Result<Rule, SyntaxError> {
7072
/// Used eg. in @supports
7173
pub fn parse_one_declaration<T: Iterator<Node>>(mut iter: T) -> Result<Declaration, SyntaxError> {
7274
match next_non_whitespace(&mut iter) {
73-
None => error(START_LOCATION, ErrEmptyInput),
75+
None => error(START_LOCATION, ErrorReason::EmptyInput),
7476
Some((component_value, location)) => {
7577
let result = parse_declaration(&mut iter, component_value, location);
7678
if result.is_err() { result }
7779
else { match next_non_whitespace(&mut iter) {
7880
None => result,
79-
Some((_component_value, location)) => error(location, ErrExtraInput),
81+
Some((_component_value, location)) => error(location, ErrorReason::ExtraInput),
8082
}}
8183
}
8284
}
@@ -88,11 +90,11 @@ pub fn parse_one_declaration<T: Iterator<Node>>(mut iter: T) -> Result<Declarati
8890
pub fn parse_one_component_value<T: Iterator<Node>>(mut iter: T)
8991
-> Result<ComponentValue, SyntaxError> {
9092
match next_non_whitespace(&mut iter) {
91-
None => error(START_LOCATION, ErrEmptyInput),
93+
None => error(START_LOCATION, ErrorReason::EmptyInput),
9294
Some((component_value, _location)) => {
9395
match next_non_whitespace(&mut iter) {
9496
None => Ok(component_value),
95-
Some((_component_value, location)) => error(location, ErrExtraInput),
97+
Some((_component_value, location)) => error(location, ErrorReason::ExtraInput),
9698
}
9799
}
98100
}
@@ -119,9 +121,10 @@ impl<T: Iterator<Node>> Iterator<Result<Rule, SyntaxError>> for StylesheetParser
119121
for_iter!(iter, (component_value, location), {
120122
match component_value {
121123
WhiteSpace | CDO | CDC => (),
122-
AtKeyword(name) => return Some(Ok(AtRule_(parse_at_rule(iter, name, location)))),
124+
AtKeyword(name) => return Some(Ok(
125+
Rule::AtRule(parse_at_rule(iter, name, location)))),
123126
_ => return Some(match parse_qualified_rule(iter, component_value, location) {
124-
Ok(rule) => Ok(QualifiedRule_(rule)),
127+
Ok(rule) => Ok(Rule::QualifiedRule(rule)),
125128
Err(e) => Err(e),
126129
}),
127130
}
@@ -137,9 +140,10 @@ impl<T: Iterator<Node>> Iterator<Result<Rule, SyntaxError>> for RuleListParser<T
137140
for_iter!(iter, (component_value, location), {
138141
match component_value {
139142
WhiteSpace => (),
140-
AtKeyword(name) => return Some(Ok(AtRule_(parse_at_rule(iter, name, location)))),
143+
AtKeyword(name) => return Some(Ok(
144+
Rule::AtRule(parse_at_rule(iter, name, location)))),
141145
_ => return Some(match parse_qualified_rule(iter, component_value, location) {
142-
Ok(rule) => Ok(QualifiedRule_(rule)),
146+
Ok(rule) => Ok(Rule::QualifiedRule(rule)),
143147
Err(e) => Err(e),
144148
}),
145149
}
@@ -156,10 +160,10 @@ for DeclarationListParser<T> {
156160
for_iter!(iter, (component_value, location), {
157161
match component_value {
158162
WhiteSpace | Semicolon => (),
159-
AtKeyword(name)
160-
=> return Some(Ok(DeclAtRule(parse_at_rule(iter, name, location)))),
163+
AtKeyword(name) => return Some(Ok(
164+
DeclarationListItem::AtRule(parse_at_rule(iter, name, location)))),
161165
_ => return Some(match parse_declaration(iter, component_value, location) {
162-
Ok(declaration) => Ok(Declaration_(declaration)),
166+
Ok(declaration) => Ok(DeclarationListItem::Declaration(declaration)),
163167
Err(e) => {
164168
// Find the end of the declaration
165169
for (v, _) in *iter { if v == Semicolon { break } }
@@ -204,7 +208,7 @@ fn parse_qualified_rule<T: Iterator<Node>>(iter: &mut T, first: ComponentValue,
204208
component_value => prelude.push(component_value),
205209
}
206210
})
207-
error(location, ErrMissingQualifiedRuleBlock)
211+
error(location, ErrorReason::MissingQualifiedRuleBlock)
208212
}
209213

210214

@@ -213,11 +217,11 @@ fn parse_declaration<T: Iterator<Node>>(iter: &mut T, first: ComponentValue,
213217
-> Result<Declaration, SyntaxError> {
214218
let name = match first {
215219
Ident(name) => name,
216-
_ => return error(location, ErrInvalidDeclarationSyntax)
220+
_ => return error(location, ErrorReason::InvalidDeclarationSyntax)
217221
};
218222
match next_non_whitespace(iter) {
219223
Some((Colon, _)) => (),
220-
_ => return error(location, ErrInvalidDeclarationSyntax),
224+
_ => return error(location, ErrorReason::InvalidDeclarationSyntax),
221225
}
222226
let mut value = Vec::new();
223227
let mut important = false;
@@ -228,7 +232,7 @@ fn parse_declaration<T: Iterator<Node>>(iter: &mut T, first: ComponentValue,
228232
important = true;
229233
break
230234
} else {
231-
return error(location, ErrInvalidBangImportantSyntax)
235+
return error(location, ErrorReason::InvalidBangImportantSyntax)
232236
},
233237
component_value => value.push(component_value),
234238
}

src/serializer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
use ast::*;
6+
use ast::ComponentValue::*;
67

78

89
pub fn to_css_push(component_value: &ComponentValue, css: &mut String) {

src/tests.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use encoding::label::encoding_from_whatwg_label;
1313

1414
use super::*;
1515
use ast::*;
16+
use ast::ComponentValue::*;
1617

1718
macro_rules! JString {
1819
($e: expr) => { json::String($e.to_string()) }
@@ -230,9 +231,9 @@ fn color3_hsl() {
230231
fn color3_keywords() {
231232
run_color_tests(include_str!("css-parsing-tests/color3_keywords.json"), |c| {
232233
match c {
233-
Some(RGBAColor(RGBA { red: r, green: g, blue: b, alpha: a }))
234+
Some(Color::RGBA(RGBA { red: r, green: g, blue: b, alpha: a }))
234235
=> vec!(r * 255., g * 255., b * 255., a).to_json(),
235-
Some(CurrentColor) => JString!("currentColor"),
236+
Some(Color::CurrentColor) => JString!("currentColor"),
236237
None => json::Null,
237238
}
238239
});
@@ -280,21 +281,21 @@ fn serializer() {
280281

281282
#[test]
282283
fn serialize_current_color() {
283-
let c = CurrentColor;
284+
let c = Color::CurrentColor;
284285
assert!(format!("{}", c).as_slice() == "currentColor");
285286
}
286287

287288

288289
#[test]
289290
fn serialize_rgb_full_alpha() {
290-
let c = RGBAColor(RGBA { red: 1.0, green: 0.9, blue: 0.8, alpha: 1.0 });
291+
let c = Color::RGBA(RGBA { red: 1.0, green: 0.9, blue: 0.8, alpha: 1.0 });
291292
assert!(format!("{}", c).as_slice() == "rgb(255, 230, 204)");
292293
}
293294

294295

295296
#[test]
296297
fn serialize_rgba() {
297-
let c = RGBAColor(RGBA { red: 0.1, green: 0.2, blue: 0.3, alpha: 0.5 });
298+
let c = Color::RGBA(RGBA { red: 0.1, green: 0.2, blue: 0.3, alpha: 0.5 });
298299
assert!(format!("{}", c).as_slice() == "rgba(26, 51, 77, 0.5)");
299300
}
300301

@@ -342,8 +343,8 @@ impl ToJson for Result<ComponentValue, SyntaxError> {
342343
impl ToJson for SyntaxError {
343344
fn to_json(&self) -> json::Json {
344345
json::List(vec!(JString!("error"), JString!(match self.reason {
345-
ErrEmptyInput => "empty",
346-
ErrExtraInput => "extra-input",
346+
ErrorReason::EmptyInput => "empty",
347+
ErrorReason::ExtraInput => "extra-input",
347348
_ => "invalid",
348349
})))
349350
}
@@ -353,8 +354,8 @@ impl ToJson for SyntaxError {
353354
impl ToJson for Color {
354355
fn to_json(&self) -> json::Json {
355356
match *self {
356-
RGBAColor(RGBA { red: r, green: g, blue: b, alpha: a }) => vec!(r, g, b, a).to_json(),
357-
CurrentColor => JString!("currentColor"),
357+
Color::RGBA(RGBA { red: r, green: g, blue: b, alpha: a }) => vec!(r, g, b, a).to_json(),
358+
Color::CurrentColor => JString!("currentColor"),
358359
}
359360
}
360361
}
@@ -363,8 +364,8 @@ impl ToJson for Color {
363364
impl ToJson for Rule {
364365
fn to_json(&self) -> json::Json {
365366
match *self {
366-
QualifiedRule_(ref rule) => rule.to_json(),
367-
AtRule_(ref rule) => rule.to_json(),
367+
Rule::QualifiedRule(ref rule) => rule.to_json(),
368+
Rule::AtRule(ref rule) => rule.to_json(),
368369
}
369370
}
370371
}
@@ -373,8 +374,8 @@ impl ToJson for Rule {
373374
impl ToJson for DeclarationListItem {
374375
fn to_json(&self) -> json::Json {
375376
match *self {
376-
Declaration_(ref declaration) => declaration.to_json(),
377-
DeclAtRule(ref at_rule) => at_rule.to_json(),
377+
DeclarationListItem::Declaration(ref declaration) => declaration.to_json(),
378+
DeclarationListItem::AtRule(ref at_rule) => at_rule.to_json(),
378379
}
379380
}
380381
}

src/tokenizer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::{char, num};
88
use std::ascii::AsciiExt;
99

1010
use ast::*;
11+
use ast::ComponentValue::*;
1112

1213

1314
/// Returns a `Iterator<(ComponentValue, SourceLocation)>`

0 commit comments

Comments
 (0)