[librsvg/librsvg-2.44] attribute: implement fmt::Display



commit 3ecbe334c723286de1db01c81b7290e0a4ebc1cc
Author: Paolo Borelli <pborelli gnome org>
Date:   Sun Sep 9 23:02:39 2018 +0200

    attribute: implement fmt::Display
    
    Instead of a custome to_str method

 rsvg_internals/src/attributes.rs | 13 +++++++------
 rsvg_internals/src/error.rs      |  4 ++--
 2 files changed, 9 insertions(+), 8 deletions(-)
---
diff --git a/rsvg_internals/src/attributes.rs b/rsvg_internals/src/attributes.rs
index a16b5541..05e4b641 100644
--- a/rsvg_internals/src/attributes.rs
+++ b/rsvg_internals/src/attributes.rs
@@ -1,5 +1,6 @@
 extern crate phf;
 
+use std::fmt;
 use std::str::FromStr;
 
 include!(concat!(env!("OUT_DIR"), "/attributes-codegen.rs"));
@@ -12,15 +13,15 @@ impl FromStr for Attribute {
     }
 }
 
-impl Attribute {
+impl fmt::Display for Attribute {
     // This is horribly inefficient, but for now I'm too lazy to have a
     // compile-time bijective mapping from attributes to names.  Hopefully
     // this function is only called when *printing* errors, which, uh,
     // should not be done too often.
-    pub fn to_str(&self) -> &'static str {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         for (k, v) in ATTRIBUTES.entries() {
             if *v == *self {
-                return k;
+                return write!(f, "{}", k);
             }
         }
 
@@ -44,8 +45,8 @@ mod tests {
 
     #[test]
     fn converts_attributes_back_to_strings() {
-        assert_eq!(Attribute::ClipPath.to_str(), "clip-path");
-        assert_eq!(Attribute::KernelUnitLength.to_str(), "kernelUnitLength");
-        assert_eq!(Attribute::Offset.to_str(), "offset");
+        assert_eq!(Attribute::ClipPath.to_string(), "clip-path");
+        assert_eq!(Attribute::KernelUnitLength.to_string(), "kernelUnitLength");
+        assert_eq!(Attribute::Offset.to_string(), "offset");
     }
 }
diff --git a/rsvg_internals/src/error.rs b/rsvg_internals/src/error.rs
index 1f5d90e2..1fd56b23 100644
--- a/rsvg_internals/src/error.rs
+++ b/rsvg_internals/src/error.rs
@@ -59,14 +59,14 @@ impl fmt::Display for NodeError {
             ValueErrorKind::Parse(ref n) => write!(
                 f,
                 "error parsing value for attribute \"{}\": {}",
-                self.attr.to_str(),
+                self.attr.to_string(),
                 n.display
             ),
 
             ValueErrorKind::Value(ref s) => write!(
                 f,
                 "invalid value for attribute \"{}\": {}",
-                self.attr.to_str(),
+                self.attr.to_string(),
                 s
             ),
         }


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