[librsvg] node.rs: Implement fmt::Display and error::Error for node::Error



commit 28bb98cfb493e7f5c294ccd53faea5f78f9acfa1
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Mar 3 10:23:15 2017 -0600

    node.rs: Implement fmt::Display and error::Error for node::Error
    
    We have an AttributeError struct that we use to hold an attribute's
    name, and the actual error which that attribute caused.
    
    node::Error in turn is an enum for parse errors, invalid values, etc.,
    that each hold an AttributeError to describe the attribute in question.

 rust/src/node.rs |   28 +++++++++++++++++++++++++++-
 1 files changed, 27 insertions(+), 1 deletions(-)
---
diff --git a/rust/src/node.rs b/rust/src/node.rs
index 7e38b98..d04a88b 100644
--- a/rust/src/node.rs
+++ b/rust/src/node.rs
@@ -1,5 +1,6 @@
 extern crate libc;
 
+use std::fmt;
 use std::error;
 use std::rc::Rc;
 use std::rc::Weak;
@@ -44,13 +45,38 @@ pub struct AttributeError {
 
 #[derive(Debug)]
 pub enum Error {
-    // parse error in an attribute
+    // parse error in an attribute's value
     AttributeParse (AttributeError),
 
     // attribute with an invalid value
     AttributeValue (AttributeError),
 }
 
+impl error::Error for Error {
+    fn description (&self) -> &str {
+        match *self {
+            Error::AttributeParse (_) => "parse error for attribute value",
+            Error::AttributeValue (_) => "invalid attribute value"
+        }
+    }
+}
+
+impl fmt::Display for Error {
+    fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result {
+        match *self {
+            Error::AttributeParse (ref ae) => write! (f,
+                                                      "error parsing value for attribute \"{}\": {}",
+                                                      ae.attr_name,
+                                                      ae.error),
+
+            Error::AttributeValue (ref ae) => write! (f,
+                                                      "invalid value for attribute \"{}\": {}",
+                                                      ae.attr_name,
+                                                      ae.error)
+        }
+    }
+}
+
 // After creating/parsing a Node, it will be in a success or an error state.
 // We represent this with a Result, aliased as a NodeResult.  There is no
 // extra information for the Ok case; all the interesting stuff is in the


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