Skip to content

Commit e5aee9b

Browse files
committed
Merge pull request #37 from larsbergstrom/rust_20140224
Rust 20140224
2 parents 0688d6e + eb5dd25 commit e5aee9b

File tree

9 files changed

+35
-31
lines changed

9 files changed

+35
-31
lines changed

Makefile.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs')
1313
all: libcssparser.dummy
1414

1515
libcssparser.dummy: lib.rs $(RUST_SRC)
16-
$(RUSTC) $(RUSTFLAGS) $< --lib --out-dir .
16+
$(RUSTC) $(RUSTFLAGS) $< --crate-type=lib --out-dir .
1717
touch $@
1818

1919
cssparser-test: lib.rs $(RUST_SRC)

ast.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
use std::fmt;
56
use std::vec;
67

78

@@ -94,7 +95,7 @@ pub struct AtRule {
9495
pub enum DeclarationListItem {
9596
Declaration(Declaration),
9697
// A better idea for a name that means "at-rule" but is not "AtRule"?
97-
Decl_AtRule(AtRule),
98+
DeclAtRule(AtRule),
9899
}
99100

100101
#[deriving(Eq)]
@@ -119,9 +120,9 @@ pub enum ErrorReason {
119120
// This is meant to be extended
120121
}
121122

122-
impl ToStr for SyntaxError {
123-
fn to_str(&self) -> ~str {
124-
format!("{:u}:{:u} {:?}", self.location.line, self.location.column, self.reason)
123+
impl fmt::Show for SyntaxError {
124+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125+
write!(f.buf, "{:u}:{:u} {:?}", self.location.line, self.location.column, self.reason)
125126
}
126127
}
127128

@@ -138,7 +139,7 @@ impl<'a> SkipWhitespaceIterable<'a> for &'a [ComponentValue] {
138139

139140
#[deriving(Clone)]
140141
pub struct SkipWhitespaceIterator<'a> {
141-
iter_with_whitespace: vec::VecIterator<'a, ComponentValue>,
142+
iter_with_whitespace: vec::Items<'a, ComponentValue>,
142143
}
143144

144145
impl<'a> Iterator<&'a ComponentValue> for SkipWhitespaceIterator<'a> {
@@ -162,7 +163,7 @@ impl MoveSkipWhitespaceIterable for ~[ComponentValue] {
162163
}
163164

164165
pub struct MoveSkipWhitespaceIterator {
165-
iter_with_whitespace: vec::MoveIterator<ComponentValue>,
166+
iter_with_whitespace: vec::MoveItems<ComponentValue>,
166167
}
167168

168169
impl Iterator<ComponentValue> for MoveSkipWhitespaceIterator {

color.rs

+5-4
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 std::ascii::StrAsciiExt;
6+
use std::cmp;
67

78
use ast::*;
89

@@ -250,7 +251,7 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])
250251

251252
let mut iter = arguments.skip_whitespace();
252253
macro_rules! expect_comma(
253-
() => ( if iter.next() != Some(&Comma) { return None } );
254+
() => ( match iter.next() { Some(&Comma) => {}, _ => { return None } } );
254255
)
255256
macro_rules! expect_percentage(
256257
() => ( match iter.next() {
@@ -297,9 +298,9 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])
297298
let hue = expect_number!() / 360.;
298299
let hue = hue - hue.floor();
299300
expect_comma!();
300-
let saturation = (expect_percentage!() / 100.).max(&0.).min(&1.);
301+
let saturation = cmp::min(cmp::max((expect_percentage!() / 100.), 0.), 1.);
301302
expect_comma!();
302-
let lightness = (expect_percentage!() / 100.).max(&0.).min(&1.);
303+
let lightness = cmp::min(cmp::max((expect_percentage!() / 100.), 0.), 1.);
303304

304305
// http://www.w3.org/TR/css3-color/#hsl-color
305306
fn hue_to_rgb(m1: f64, m2: f64, mut h: f64) -> f64 {
@@ -321,7 +322,7 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue])
321322

322323
let alpha = if has_alpha {
323324
expect_comma!();
324-
(expect_number!()).max(&0.).min(&1.) as f32
325+
cmp::min(cmp::max((expect_number!()), 0.), 1.) as f32
325326
} else {
326327
1.
327328
};

from_bytes.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
use std::cmp;
56
use std::str;
67

78
use encoding::label::encoding_from_whatwg_label;
@@ -44,7 +45,7 @@ pub fn decode_stylesheet_bytes(css: &[u8], protocol_encoding_label: Option<&str>
4445
if css.starts_with("@charset \"".as_bytes()) {
4546
// 10 is "@charset \"".len()
4647
// 100 is arbitrary so that no encoding label is more than 100-10 bytes.
47-
match css.slice(10, css.len().min(&100)).position_elem(&('"' as u8)) {
48+
match css.slice(10, cmp::min(css.len(), 100)).position_elem(&('"' as u8)) {
4849
None => (),
4950
Some(label_length)
5051
=> if css.slice_from(10 + label_length).starts_with("\";".as_bytes()) {

lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
#[crate_id = "github.com/mozilla-servo/rust-cssparser#cssparser:0.1"];
66
#[feature(globs, macro_rules)];
77

8-
extern mod extra;
9-
extern mod encoding; // https://github.com/lifthrasiir/rust-encoding
8+
extern crate extra;
9+
extern crate encoding; // https://github.com/lifthrasiir/rust-encoding
10+
11+
#[cfg(test)]
12+
extern crate test;
1013

1114
pub use tokenizer::tokenize;
1215
pub use parser::{parse_stylesheet_rules, parse_rule_list, parse_declaration_list,

nth.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> {
2323
match unit {
2424
"n" => parse_b(iter, a as i32),
2525
"n-" => parse_signless_b(iter, a as i32, -1),
26-
_ => match(parse_n_dash_digits(unit)) {
26+
_ => match parse_n_dash_digits(unit) {
2727
Some(b) => parse_end(iter, a as i32, b),
2828
_ => None
2929
},
@@ -40,11 +40,11 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> {
4040
"-n" => parse_b(iter, -1),
4141
"n-" => parse_signless_b(iter, 1, -1),
4242
"-n-" => parse_signless_b(iter, -1, -1),
43-
_ if ident.starts_with("-") => match(parse_n_dash_digits(ident.slice_from(1))) {
43+
_ if ident.starts_with("-") => match parse_n_dash_digits(ident.slice_from(1)) {
4444
Some(b) => parse_end(iter, -1, b),
4545
_ => None
4646
},
47-
_ => match(parse_n_dash_digits(ident)) {
47+
_ => match parse_n_dash_digits(ident) {
4848
Some(b) => parse_end(iter, 1, b),
4949
_ => None
5050
},
@@ -56,7 +56,7 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> {
5656
match ident {
5757
"n" => parse_b(iter, 1),
5858
"n-" => parse_signless_b(iter, 1, -1),
59-
_ => match(parse_n_dash_digits(ident)) {
59+
_ => match parse_n_dash_digits(ident) {
6060
Some(b) => parse_end(iter, 1, b),
6161
_ => None
6262
},

parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ for DeclarationListParser<T> {
160160
match component_value {
161161
WhiteSpace | Semicolon => (),
162162
AtKeyword(name)
163-
=> return Some(Ok(Decl_AtRule(parse_at_rule(iter, name, location)))),
163+
=> return Some(Ok(DeclAtRule(parse_at_rule(iter, name, location)))),
164164
_ => return Some(match parse_declaration(iter, component_value, location) {
165165
Ok(declaration) => Ok(Declaration(declaration)),
166166
Err(e) => {

tests.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
use std::{str, run, task};
6-
use std::io;
7-
use std::io::{File, Writer};
5+
use std::{io, str, task};
6+
use std::io::{File, Process, Writer};
87
use extra::{tempfile, json};
98
use extra::json::ToJson;
10-
use extra::test;
9+
use test;
1110

1211
use encoding::label::encoding_from_whatwg_label;
1312

@@ -17,8 +16,8 @@ use ast::*;
1716

1817
fn write_whole_file(path: &Path, data: &str) {
1918
match File::open_mode(path, io::Open, io::Write) {
20-
Some(mut writer) => writer.write(data.as_bytes()),
21-
None => fail!("could not open file"),
19+
Ok(mut writer) => { writer.write(data.as_bytes()); },
20+
_ => fail!("could not open file"),
2221
}
2322
}
2423

@@ -49,8 +48,8 @@ fn assert_json_eq(results: json::Json, expected: json::Json, message: ~str) {
4948
expected_path.push("expected.json");
5049
write_whole_file(&result_path, results);
5150
write_whole_file(&expected_path, expected);
52-
run::process_status("colordiff", [~"-u1000", result_path.display().to_str(),
53-
expected_path.display().to_str()]);
51+
Process::status("colordiff", [~"-u1000", result_path.display().to_str(),
52+
expected_path.display().to_str()]);
5453
});
5554
5655
fail!(message)
@@ -333,7 +332,7 @@ impl ToJson for DeclarationListItem {
333332
fn to_json(&self) -> json::Json {
334333
match *self {
335334
Declaration(ref declaration) => declaration.to_json(),
336-
Decl_AtRule(ref at_rule) => at_rule.to_json(),
335+
DeclAtRule(ref at_rule) => at_rule.to_json(),
337336
}
338337
}
339338
}

tokenizer.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,8 @@ fn next_component_value(tokenizer: &mut Tokenizer) -> Option<Node> {
185185
}
186186
},
187187
'.' => {
188-
if (
189-
tokenizer.position + 1 < tokenizer.length
190-
&& is_match!(tokenizer.char_at(1), '0'..'9')
188+
if tokenizer.position + 1 < tokenizer.length
189+
&& is_match!(tokenizer.char_at(1), '0'..'9'
191190
) {
192191
consume_numeric(tokenizer)
193192
} else {

0 commit comments

Comments
 (0)