[librsvg] Opacity::from_opacity_spec(): New function, to deal only in Result<Opacity, ...>



commit 8238741217495fa138279d14714a14dbce4bb62e
Author: Federico Mena Quintero <federico gnome org>
Date:   Wed May 31 13:12:10 2017 -0500

    Opacity::from_opacity_spec(): New function, to deal only in Result<Opacity, ...>
    
    Instead of dealing with OpacitySpec in the Rust code.  This is analogous
    to Color::from_color_spec().

 rust/src/opacity.rs |   32 ++++++++++++++++++++++++++++++++
 1 files changed, 32 insertions(+), 0 deletions(-)
---
diff --git a/rust/src/opacity.rs b/rust/src/opacity.rs
index 6eaeb7c..c9e6d35 100644
--- a/rust/src/opacity.rs
+++ b/rust/src/opacity.rs
@@ -100,6 +100,18 @@ impl FromStr for Opacity {
     }
 }
 
+impl Opacity {
+    pub fn from_opacity_spec (spec: &OpacitySpec) -> Result <Opacity, AttributeError> {
+        match *spec {
+            OpacitySpec { kind: OpacityKind::Inherit, .. }      => Ok (Opacity::Inherit),
+
+            OpacitySpec { kind: OpacityKind::Specified, opacity } => Ok (Opacity::Specified (opacity as f64 
/ 255.0)),
+
+            OpacitySpec { kind: OpacityKind::ParseError, .. } => Err (AttributeError::Parse (ParseError::new 
("parse error")))
+        }
+    }
+}
+
 #[no_mangle]
 pub extern fn rsvg_css_parse_opacity (string: *const libc::c_char) -> OpacitySpec {
     let s = unsafe { String::from_glib_none (string) };
@@ -164,4 +176,24 @@ mod tests {
                     OpacitySpec { kind: OpacityKind::ParseError,
                                   opacity: 0 });
     }
+
+    fn test_roundtrip (s: &str) {
+        let result = Opacity::from_str (s);
+        let result2 = result.clone ();
+        let spec = OpacitySpec::from (result2);
+
+        if result.is_ok () {
+            assert_eq! (Opacity::from_opacity_spec (&spec), result);
+        } else {
+            assert! (Opacity::from_opacity_spec (&spec).is_err ());
+        }
+    }
+
+    #[test]
+    fn roundtrips () {
+        test_roundtrip ("inherit");
+        test_roundtrip ("0");
+        test_roundtrip ("1.0");
+        test_roundtrip ("chilaquil");
+    }
 }


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