From 9fb93613bb61936eeebd3899b5663302063679e4 Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Tue, 4 Mar 2014 08:43:57 -0800 Subject: [PATCH 1/4] Rust upgrade --- Makefile.in | 2 +- ast.rs | 11 ++++++----- color.rs | 9 +++++---- from_bytes.rs | 3 ++- lib.rs | 4 ++-- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Makefile.in b/Makefile.in index 1009d63c..9311fad9 100644 --- a/Makefile.in +++ b/Makefile.in @@ -13,7 +13,7 @@ RUST_SRC=$(shell find $(VPATH)/. -type f -name '*.rs') all: libcssparser.dummy libcssparser.dummy: lib.rs $(RUST_SRC) - $(RUSTC) $(RUSTFLAGS) $< --lib --out-dir . + $(RUSTC) $(RUSTFLAGS) $< --crate-type=lib --out-dir . touch $@ cssparser-test: lib.rs $(RUST_SRC) diff --git a/ast.rs b/ast.rs index 836b503b..1422c932 100644 --- a/ast.rs +++ b/ast.rs @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use std::fmt; use std::vec; @@ -119,9 +120,9 @@ pub enum ErrorReason { // This is meant to be extended } -impl ToStr for SyntaxError { - fn to_str(&self) -> ~str { - format!("{:u}:{:u} {:?}", self.location.line, self.location.column, self.reason) +impl fmt::Show for SyntaxError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f.buf, "{:u}:{:u} {:?}", self.location.line, self.location.column, self.reason) } } @@ -138,7 +139,7 @@ impl<'a> SkipWhitespaceIterable<'a> for &'a [ComponentValue] { #[deriving(Clone)] pub struct SkipWhitespaceIterator<'a> { - iter_with_whitespace: vec::VecIterator<'a, ComponentValue>, + iter_with_whitespace: vec::Items<'a, ComponentValue>, } impl<'a> Iterator<&'a ComponentValue> for SkipWhitespaceIterator<'a> { @@ -162,7 +163,7 @@ impl MoveSkipWhitespaceIterable for ~[ComponentValue] { } pub struct MoveSkipWhitespaceIterator { - iter_with_whitespace: vec::MoveIterator, + iter_with_whitespace: vec::MoveItems, } impl Iterator for MoveSkipWhitespaceIterator { diff --git a/color.rs b/color.rs index f14efdf6..b79160a7 100644 --- a/color.rs +++ b/color.rs @@ -3,6 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use std::ascii::StrAsciiExt; +use std::cmp; use ast::*; @@ -250,7 +251,7 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue]) let mut iter = arguments.skip_whitespace(); macro_rules! expect_comma( - () => ( if iter.next() != Some(&Comma) { return None } ); + () => ( match iter.next() { Some(&Comma) => {}, _ => { return None } } ); ) macro_rules! expect_percentage( () => ( match iter.next() { @@ -297,9 +298,9 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue]) let hue = expect_number!() / 360.; let hue = hue - hue.floor(); expect_comma!(); - let saturation = (expect_percentage!() / 100.).max(&0.).min(&1.); + let saturation = cmp::min(cmp::max((expect_percentage!() / 100.), 0.), 1.); expect_comma!(); - let lightness = (expect_percentage!() / 100.).max(&0.).min(&1.); + let lightness = cmp::min(cmp::max((expect_percentage!() / 100.), 0.), 1.); // http://www.w3.org/TR/css3-color/#hsl-color fn hue_to_rgb(m1: f64, m2: f64, mut h: f64) -> f64 { @@ -321,7 +322,7 @@ fn parse_color_function(name: &str, arguments: &[ComponentValue]) let alpha = if has_alpha { expect_comma!(); - (expect_number!()).max(&0.).min(&1.) as f32 + cmp::min(cmp::max((expect_number!()), 0.), 1.) as f32 } else { 1. }; diff --git a/from_bytes.rs b/from_bytes.rs index c4a64ccd..2355e4a5 100644 --- a/from_bytes.rs +++ b/from_bytes.rs @@ -2,6 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use std::cmp; use std::str; use encoding::label::encoding_from_whatwg_label; @@ -44,7 +45,7 @@ pub fn decode_stylesheet_bytes(css: &[u8], protocol_encoding_label: Option<&str> if css.starts_with("@charset \"".as_bytes()) { // 10 is "@charset \"".len() // 100 is arbitrary so that no encoding label is more than 100-10 bytes. - match css.slice(10, css.len().min(&100)).position_elem(&('"' as u8)) { + match css.slice(10, cmp::min(css.len(), 100)).position_elem(&('"' as u8)) { None => (), Some(label_length) => if css.slice_from(10 + label_length).starts_with("\";".as_bytes()) { diff --git a/lib.rs b/lib.rs index b5837726..c09fd85c 100644 --- a/lib.rs +++ b/lib.rs @@ -5,8 +5,8 @@ #[crate_id = "github.com/mozilla-servo/rust-cssparser#cssparser:0.1"]; #[feature(globs, macro_rules)]; -extern mod extra; -extern mod encoding; // https://github.com/lifthrasiir/rust-encoding +extern crate extra; +extern crate encoding; // https://github.com/lifthrasiir/rust-encoding pub use tokenizer::tokenize; pub use parser::{parse_stylesheet_rules, parse_rule_list, parse_declaration_list, From 69d7c6e109224218e41957397ef2079524fdc9e8 Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Thu, 13 Mar 2014 19:33:00 -0500 Subject: [PATCH 2/4] Add make check support --- lib.rs | 3 +++ tests.rs | 15 +++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib.rs b/lib.rs index c09fd85c..0bcb783f 100644 --- a/lib.rs +++ b/lib.rs @@ -8,6 +8,9 @@ extern crate extra; extern crate encoding; // https://github.com/lifthrasiir/rust-encoding +#[cfg(test)] +extern crate test; + pub use tokenizer::tokenize; pub use parser::{parse_stylesheet_rules, parse_rule_list, parse_declaration_list, parse_one_rule, parse_one_declaration, parse_one_component_value}; diff --git a/tests.rs b/tests.rs index 595b45c8..be1956a6 100644 --- a/tests.rs +++ b/tests.rs @@ -2,12 +2,11 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use std::{str, run, task}; -use std::io; -use std::io::{File, Writer}; +use std::{io, str, task}; +use std::io::{File, Process, Writer}; use extra::{tempfile, json}; use extra::json::ToJson; -use extra::test; +use test; use encoding::label::encoding_from_whatwg_label; @@ -17,8 +16,8 @@ use ast::*; fn write_whole_file(path: &Path, data: &str) { match File::open_mode(path, io::Open, io::Write) { - Some(mut writer) => writer.write(data.as_bytes()), - None => fail!("could not open file"), + Ok(mut writer) => { writer.write(data.as_bytes()); }, + _ => fail!("could not open file"), } } @@ -49,8 +48,8 @@ fn assert_json_eq(results: json::Json, expected: json::Json, message: ~str) { expected_path.push("expected.json"); write_whole_file(&result_path, results); write_whole_file(&expected_path, expected); - run::process_status("colordiff", [~"-u1000", result_path.display().to_str(), - expected_path.display().to_str()]); + Process::status("colordiff", [~"-u1000", result_path.display().to_str(), + expected_path.display().to_str()]); }); fail!(message) From b03ed6b97ddc489690c58b057e508c41f2ed59bc Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Fri, 14 Mar 2014 15:08:25 -0400 Subject: [PATCH 3/4] Warning police. --- ast.rs | 2 +- nth.rs | 8 ++++---- parser.rs | 2 +- tokenizer.rs | 5 ++--- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/ast.rs b/ast.rs index 1422c932..8f82a5f4 100644 --- a/ast.rs +++ b/ast.rs @@ -95,7 +95,7 @@ pub struct AtRule { pub enum DeclarationListItem { Declaration(Declaration), // A better idea for a name that means "at-rule" but is not "AtRule"? - Decl_AtRule(AtRule), + DeclAtRule(AtRule), } #[deriving(Eq)] diff --git a/nth.rs b/nth.rs index 4108d466..7422e381 100644 --- a/nth.rs +++ b/nth.rs @@ -23,7 +23,7 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> { match unit { "n" => parse_b(iter, a as i32), "n-" => parse_signless_b(iter, a as i32, -1), - _ => match(parse_n_dash_digits(unit)) { + _ => match parse_n_dash_digits(unit) { Some(b) => parse_end(iter, a as i32, b), _ => None }, @@ -40,11 +40,11 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> { "-n" => parse_b(iter, -1), "n-" => parse_signless_b(iter, 1, -1), "-n-" => parse_signless_b(iter, -1, -1), - _ if ident.starts_with("-") => match(parse_n_dash_digits(ident.slice_from(1))) { + _ if ident.starts_with("-") => match parse_n_dash_digits(ident.slice_from(1)) { Some(b) => parse_end(iter, -1, b), _ => None }, - _ => match(parse_n_dash_digits(ident)) { + _ => match parse_n_dash_digits(ident) { Some(b) => parse_end(iter, 1, b), _ => None }, @@ -56,7 +56,7 @@ pub fn parse_nth(input: &[ComponentValue]) -> Option<(i32, i32)> { match ident { "n" => parse_b(iter, 1), "n-" => parse_signless_b(iter, 1, -1), - _ => match(parse_n_dash_digits(ident)) { + _ => match parse_n_dash_digits(ident) { Some(b) => parse_end(iter, 1, b), _ => None }, diff --git a/parser.rs b/parser.rs index 7c2dcf3a..c8cede68 100644 --- a/parser.rs +++ b/parser.rs @@ -160,7 +160,7 @@ for DeclarationListParser { match component_value { WhiteSpace | Semicolon => (), AtKeyword(name) - => return Some(Ok(Decl_AtRule(parse_at_rule(iter, name, location)))), + => return Some(Ok(DeclAtRule(parse_at_rule(iter, name, location)))), _ => return Some(match parse_declaration(iter, component_value, location) { Ok(declaration) => Ok(Declaration(declaration)), Err(e) => { diff --git a/tokenizer.rs b/tokenizer.rs index 24154252..76092e8d 100644 --- a/tokenizer.rs +++ b/tokenizer.rs @@ -185,9 +185,8 @@ fn next_component_value(tokenizer: &mut Tokenizer) -> Option { } }, '.' => { - if ( - tokenizer.position + 1 < tokenizer.length - && is_match!(tokenizer.char_at(1), '0'..'9') + if tokenizer.position + 1 < tokenizer.length + && is_match!(tokenizer.char_at(1), '0'..'9' ) { consume_numeric(tokenizer) } else { From f04b9f5a56eb70cbd4265e7d6051f20e7e7a2165 Mon Sep 17 00:00:00 2001 From: Lars Bergstrom Date: Mon, 17 Mar 2014 12:41:15 -0500 Subject: [PATCH 4/4] Rust make check upgrade --- tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests.rs b/tests.rs index be1956a6..93c0499a 100644 --- a/tests.rs +++ b/tests.rs @@ -332,7 +332,7 @@ impl ToJson for DeclarationListItem { fn to_json(&self) -> json::Json { match *self { Declaration(ref declaration) => declaration.to_json(), - Decl_AtRule(ref at_rule) => at_rule.to_json(), + DeclAtRule(ref at_rule) => at_rule.to_json(), } } }