[librsvg] color.rs: Convert directly from cssparser's Result to our ColorSpec



commit 267e6f284380461da248b9898b82eeaaea956478
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri May 19 20:21:01 2017 -0500

    color.rs: Convert directly from cssparser's Result to our ColorSpec

 rust/Cargo.lock   |   22 +++++-----
 rust/Cargo.toml   |    3 +-
 rust/src/color.rs |  125 +++++++++++++++-------------------------------------
 3 files changed, 48 insertions(+), 102 deletions(-)
---
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index eab69af..80df258 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -5,7 +5,7 @@ dependencies = [
  "bitflags 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "cairo-rs 0.1.1 (git+https://github.com/federicomenaquintero/cairo.git)",
  "cairo-sys-rs 0.3.2 (git+https://github.com/federicomenaquintero/cairo.git)",
- "cssparser 0.13.5 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)",
+ "cssparser 0.14.0 (git+https://github.com/servo/rust-cssparser)",
  "downcast-rs 1.0.0 (git+https://github.com/marcianx/downcast-rs)",
  "glib 0.1.1 (git+https://github.com/gtk-rs/glib)",
  "glib-sys 0.3.2 (git+https://github.com/gtk-rs/sys)",
@@ -99,13 +99,13 @@ dependencies = [
 
 [[package]]
 name = "cssparser"
-version = "0.13.5"
-source = "git+https://github.com/servo/rust-cssparser?tag=v0.13.5#fc0bdcd1845e3aa616c3bdd15127aed259b87cb8";
+version = "0.14.0"
+source = "git+https://github.com/servo/rust-cssparser#0a70d228aca1f60b1d838bbe4b471bfa9c6bb557";
 dependencies = [
- "cssparser-macros 0.3.0 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)",
+ "cssparser-macros 0.3.0 (git+https://github.com/servo/rust-cssparser)",
  "matches 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
  "phf 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
- "procedural-masquerade 0.1.2 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)",
+ "procedural-masquerade 0.1.2 (git+https://github.com/servo/rust-cssparser)",
  "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
  "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
@@ -113,10 +113,10 @@ dependencies = [
 [[package]]
 name = "cssparser-macros"
 version = "0.3.0"
-source = "git+https://github.com/servo/rust-cssparser?tag=v0.13.5#fc0bdcd1845e3aa616c3bdd15127aed259b87cb8";
+source = "git+https://github.com/servo/rust-cssparser#0a70d228aca1f60b1d838bbe4b471bfa9c6bb557";
 dependencies = [
  "phf_codegen 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)",
- "procedural-masquerade 0.1.2 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)",
+ "procedural-masquerade 0.1.2 (git+https://github.com/servo/rust-cssparser)",
  "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
  "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
@@ -332,7 +332,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 [[package]]
 name = "procedural-masquerade"
 version = "0.1.2"
-source = "git+https://github.com/servo/rust-cssparser?tag=v0.13.5#fc0bdcd1845e3aa616c3bdd15127aed259b87cb8";
+source = "git+https://github.com/servo/rust-cssparser#0a70d228aca1f60b1d838bbe4b471bfa9c6bb557";
 
 [[package]]
 name = "quote"
@@ -518,8 +518,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum c_vec 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = 
"0059f5a658f62a4bd3937a7addc52ccfda144b75cce7a92b187e528629cdc507"
 "checksum cairo-rs 0.1.1 (git+https://github.com/federicomenaquintero/cairo.git)" = "<none>"
 "checksum cairo-sys-rs 0.3.2 (git+https://github.com/federicomenaquintero/cairo.git)" = "<none>"
-"checksum cssparser 0.13.5 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)" = "<none>"
-"checksum cssparser-macros 0.3.0 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)" = "<none>"
+"checksum cssparser 0.14.0 (git+https://github.com/servo/rust-cssparser)" = "<none>"
+"checksum cssparser-macros 0.3.0 (git+https://github.com/servo/rust-cssparser)" = "<none>"
 "checksum diff 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = 
"0a515461b6c8c08419850ced27bc29e86166dcdcde8fbe76f8b1f0589bb49472"
 "checksum docopt 0.6.86 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4a7ef30445607f6fc8720f0a0a2c7442284b629cf0d049286860fae23e71c4d9"
 "checksum downcast-rs 1.0.0 (git+https://github.com/marcianx/downcast-rs)" = "<none>"
@@ -545,7 +545,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index";
 "checksum phf_generator 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = 
"6b07ffcc532ccc85e3afc45865469bf5d9e4ef5bfcf9622e3cfe80c2d275ec03"
 "checksum phf_shared 0.7.21 (registry+https://github.com/rust-lang/crates.io-index)" = 
"07e24b0ca9643bdecd0632f2b3da6b1b89bbb0030e0b992afc1113b23a7bc2f2"
 "checksum pkg-config 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = 
"3a8b4c6b8165cd1a1cd4b9b120978131389f64bdaf456435caa41e630edba903"
-"checksum procedural-masquerade 0.1.2 (git+https://github.com/servo/rust-cssparser?tag=v0.13.5)" = "<none>"
+"checksum procedural-masquerade 0.1.2 (git+https://github.com/servo/rust-cssparser)" = "<none>"
 "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = 
"7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
 "checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = 
"022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
 "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = 
"4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 17c2976..d5ee84c 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -10,8 +10,7 @@ bitflags = ""
 
 [dependencies.cssparser]
 git = "https://github.com/servo/rust-cssparser";
-version = "0.13.5"
-tag = "v0.13.5"
+version = "0.14.0"
 
 [dependencies.cairo-sys-rs]
 #git = "https://github.com/gtk-rs/cairo.git";
diff --git a/rust/src/color.rs b/rust/src/color.rs
index bb8d888..ab358f6 100644
--- a/rust/src/color.rs
+++ b/rust/src/color.rs
@@ -1,8 +1,3 @@
-use std::str::FromStr;
-
-use parsers::ParseError;
-use error::*;
-
 use ::cssparser;
 
 // Keep this in sync with rsvg-css.h:RsvgCssColorKind
@@ -23,59 +18,29 @@ pub struct ColorSpec {
     argb: u32
 }
 
-impl From<cssparser::Color> for ColorSpec {
-    fn from (color: cssparser::Color) -> ColorSpec {
-        match color {
-            cssparser::Color::CurrentColor =>
+impl<'i> From<Result<cssparser::Color, cssparser::BasicParseError<'i>>> for ColorSpec {
+    fn from (result: Result<cssparser::Color, cssparser::BasicParseError<'i>>) -> ColorSpec {
+        match result {
+            Ok (cssparser::Color::CurrentColor) =>
                 ColorSpec {
                     kind: ColorKind::CurrentColor,
                     argb: 0
                 },
 
-            cssparser::Color::RGBA (rgba) =>
+            Ok (cssparser::Color::RGBA (rgba)) =>
                 ColorSpec {
                     kind: ColorKind::ARGB,
                     argb: ((rgba.alpha as u32) << 24 |
                            (rgba.red as u32)   << 16 |
                            (rgba.green as u32) << 8  |
                            (rgba.blue as u32))
-                }
-        }
-    }
-}
-
-#[derive(Debug, Copy, Clone, PartialEq, Eq)]
-pub struct RawColor {
-    pub argb: u32
-}
-
-impl RawColor {
-    pub fn new_rgb (r: u8, g: u8, b: u8) -> RawColor {
-        RawColor {
-            argb: (0xff000000       |
-                   (r as u32) << 16 |
-                   (g as u32) << 8  |
-                   (b as u32))
-        }
-    }
-
-    pub fn new_argb (a: u8, r: u8, g: u8, b: u8) -> RawColor {
-        RawColor {
-            argb: ((a as u32) << 24 |
-                   (r as u32) << 16 |
-                   (g as u32) << 8  |
-                   (b as u32))
-        }
-    }
-}
-
-impl FromStr for RawColor {
-    type Err = AttributeError;
+                },
 
-    fn from_str (s: &str) -> Result<RawColor, AttributeError> {
-        match cssparser::Color::parse (&mut cssparser::Parser::new (s)) {
-            Ok (cssparser::Color::RGBA (rgba)) => Ok (RawColor::new_argb (rgba.alpha, rgba.red, rgba.green, 
rgba.blue)),
-            _ => Err (AttributeError::Parse (ParseError::new ("invalid color specification")))
+            _ =>
+                ColorSpec {
+                    kind: ColorKind::ParseError,
+                    argb: 0
+                }
         }
     }
 }
@@ -85,72 +50,54 @@ mod tests {
     use super::*;
     use std::str::FromStr;
 
+    fn parse (s: &str) -> ColorSpec {
+        ColorSpec::from (cssparser::Color::parse (&mut cssparser::Parser::new (s)))
+    }
+
     #[test]
     fn parses_hash_hex_colors () {
-        assert_eq! (RawColor::from_str ("#AB10fa20").unwrap (), RawColor::new_argb (0x20, 0xab, 0x10, 0xfa));
-        assert_eq! (RawColor::from_str ("#10fa20").unwrap (),   RawColor::new_rgb  (0x10, 0xfa, 0x20));
-        assert_eq! (RawColor::from_str ("#abcd").unwrap (),     RawColor::new_argb (0xdd, 0xaa, 0xbb, 0xcc));
-        assert_eq! (RawColor::from_str ("#123").unwrap (),      RawColor::new_rgb  (0x11, 0x22, 0x33));
+        assert_eq! (parse ("#AB10fa20"), ColorSpec { kind: ColorKind::ARGB, argb: 0x20ab10fa });
+        assert_eq! (parse ("#10fa20"),   ColorSpec { kind: ColorKind::ARGB, argb: 0xff10fa20 });
+        assert_eq! (parse ("#abcd"),     ColorSpec { kind: ColorKind::ARGB, argb: 0xddaabbcc });
+        assert_eq! (parse ("#123"),      ColorSpec { kind: ColorKind::ARGB, argb: 0xff112233 });
     }
 
     #[test]
     fn parses_color_keywords () {
-        assert_eq! (RawColor::from_str ("red").unwrap (),  RawColor::new_rgb (0xff, 0x00, 0x00));
-        assert_eq! (RawColor::from_str ("lime").unwrap (), RawColor::new_rgb (0x00, 0xff, 0x00));
-        assert_eq! (RawColor::from_str ("blue").unwrap (), RawColor::new_rgb (0x00, 0x00, 0xff));
+        assert_eq! (parse ("red"),  ColorSpec { kind: ColorKind::ARGB, argb: 0xffff0000 });
+        assert_eq! (parse ("lime"), ColorSpec { kind: ColorKind::ARGB, argb: 0xff00ff00 });
+        assert_eq! (parse ("blue"), ColorSpec { kind: ColorKind::ARGB, argb: 0xff0000ff });
     }
 
     #[test]
     fn parses_color_functions () {
-        assert_eq! (RawColor::from_str ("rgb(255, 0, 0)").unwrap (), RawColor::new_rgb (0xff, 0x00, 0x00));
-        assert_eq! (RawColor::from_str ("rgb(0, 255, 0)").unwrap (), RawColor::new_rgb (0x00, 0xff, 0x00));
-        assert_eq! (RawColor::from_str ("rgb(0, 0, 255)").unwrap (), RawColor::new_rgb (0x00, 0x00, 0xff));
+        assert_eq! (parse ("rgb(255, 0, 0)"), ColorSpec { kind: ColorKind::ARGB, argb: 0xffff0000 });
+        assert_eq! (parse ("rgb(0, 255, 0)"), ColorSpec { kind: ColorKind::ARGB, argb: 0xff00ff00 });
+        assert_eq! (parse ("rgb(0, 0, 255)"), ColorSpec { kind: ColorKind::ARGB, argb: 0xff0000ff });
     }
 
     #[test]
-    fn invalid_hash_hex_colors_yield_error () {
-        assert! (RawColor::from_str ("#").is_err ());
-        assert! (RawColor::from_str ("#xyz").is_err ());
-        assert! (RawColor::from_str ("#112233gg").is_err ());
-    }
-
-    #[test]
-    fn invalid_colors_yield_error () {
-        assert! (RawColor::from_str ("").is_err ());
-        assert! (RawColor::from_str ("foo").is_err ());
+    fn parses_current_color () {
+        assert_eq! (parse ("currentColor"), ColorSpec { kind: ColorKind::CurrentColor, argb: 0 });
     }
 
-    fn make_color (a: u8, r: u8, g: u8, b: u8) -> cssparser::Color {
-        cssparser::Color::RGBA (cssparser::RGBA::new (r, g, b, a))
-    }
-
-    fn make_color_spec (a: u8, r: u8, g: u8, b: u8) -> ColorSpec {
-        ColorSpec {
-            kind: ColorKind::ARGB,
-            argb: ((a as u32) << 24 |
-                   (r as u32) << 16 |
-                   (g as u32) << 8  |
-                   (b as u32))
-        }
-    }
-
-    fn make_color_spec_current_color () -> ColorSpec {
+    fn make_error () -> ColorSpec {
         ColorSpec {
-            kind: ColorKind::CurrentColor,
+            kind: ColorKind::ParseError,
             argb: 0
         }
     }
 
     #[test]
-    fn color_spec_from_css_color () {
-        assert_eq! (ColorSpec::from (make_color (255, 0, 0, 0)), make_color_spec (255, 0, 0, 0));
-        assert_eq! (ColorSpec::from (make_color (0, 255, 0, 0)), make_color_spec (0, 255, 0, 0));
-        assert_eq! (ColorSpec::from (make_color (0, 0, 255, 0)), make_color_spec (0, 0, 255, 0));
-        assert_eq! (ColorSpec::from (make_color (0, 0, 0, 255)), make_color_spec (0, 0, 0, 255));
+    fn invalid_hash_hex_colors_yield_error () {
+        assert_eq! (parse ("#"), make_error ());
+        assert_eq! (parse ("#xyz"), make_error ());
+        assert_eq! (parse ("#112233gg"), make_error ());
     }
 
     #[test]
-    fn color_spec_from_css_current_color () {
-        assert_eq! (ColorSpec::from (cssparser::Color::CurrentColor), make_color_spec_current_color ());
+    fn invalid_colors_yield_error () {
+        assert_eq! (parse (""), make_error ());
+        assert_eq! (parse ("foo"), make_error ());
     }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]