[librsvg] error.rs: Move the NodeError machinery here, so other files can use it
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] error.rs: Move the NodeError machinery here, so other files can use it
- Date: Thu, 16 Mar 2017 05:16:10 +0000 (UTC)
commit 7c8ddd0017321d966cbc9c27357babc31c10e144
Author: Federico Mena Quintero <federico gnome org>
Date: Fri Mar 3 15:52:00 2017 -0600
error.rs: Move the NodeError machinery here, so other files can use it
... also, we renamed our struct from Error to NodeError, avoid getting
it confused with std::error::Error.
rust/src/error.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
rust/src/lib.rs | 1 +
rust/src/node.rs | 65 +++------------------------------------------------
rust/src/shapes.rs | 3 +-
4 files changed, 68 insertions(+), 62 deletions(-)
---
diff --git a/rust/src/error.rs b/rust/src/error.rs
new file mode 100644
index 0000000..ea07cd1
--- /dev/null
+++ b/rust/src/error.rs
@@ -0,0 +1,61 @@
+use std::fmt;
+use std::error;
+
+use parsers::ParseError;
+
+#[derive(Debug)]
+pub enum AttributeError {
+ // parse error
+ Parse (ParseError),
+
+ // invalid value
+ Value (String)
+}
+
+#[derive(Debug)]
+pub struct NodeError {
+ attr_name: &'static str,
+ err: AttributeError
+}
+
+impl NodeError {
+ pub fn parse_error (attr_name: &'static str, error: ParseError) -> NodeError {
+ NodeError {
+ attr_name: attr_name,
+ err: AttributeError::Parse (error)
+ }
+ }
+
+ pub fn value_error (attr_name: &'static str, description: String) -> NodeError {
+ NodeError {
+ attr_name: attr_name,
+ err: AttributeError::Value (description)
+ }
+ }
+}
+
+impl error::Error for NodeError {
+ fn description (&self) -> &str {
+ match self.err {
+ AttributeError::Parse (ref n) => &"parse error",
+ AttributeError::Value (_) => &"invalid attribute value"
+ }
+ }
+}
+
+impl fmt::Display for NodeError {
+ fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result {
+ match self.err {
+ AttributeError::Parse (ref n) => write! (f,
+ "error parsing value for attribute \"{}\": {}",
+ self.attr_name,
+ n.display),
+
+ AttributeError::Value (ref s) => write! (f,
+ "invalid value for attribute \"{}\": {}",
+ self.attr_name,
+ s)
+ }
+ }
+}
+
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 5ae6c63..f3ae8f9 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -102,6 +102,7 @@ mod aspect_ratio;
mod bbox;
mod cnode;
mod drawing_ctx;
+mod error;
mod handle;
mod gradient;
mod length;
diff --git a/rust/src/node.rs b/rust/src/node.rs
index 557bc5b..be53efb 100644
--- a/rust/src/node.rs
+++ b/rust/src/node.rs
@@ -1,7 +1,5 @@
extern crate libc;
-use std::fmt;
-use std::error;
use std::rc::Rc;
use std::rc::Weak;
use std::cell::RefCell;
@@ -14,11 +12,12 @@ use drawing_ctx;
use handle::RsvgHandle;
-use parsers::ParseError;
use property_bag::RsvgPropertyBag;
use state::RsvgState;
+use error::*;
+
/* A *const RsvgNode is just a pointer for the C code's benefit: it
* points to an Rc<Node>, which is our refcounted Rust representation
* of nodes.
@@ -38,62 +37,6 @@ pub trait NodeTrait: Downcast {
impl_downcast! (NodeTrait);
-#[derive(Debug)]
-pub enum AttributeError {
- // parse error
- Parse (ParseError),
-
- // invalid value
- Value (String)
-}
-
-#[derive(Debug)]
-pub struct Error {
- attr_name: &'static str,
- err: AttributeError
-}
-
-impl Error {
- pub fn parse_error (attr_name: &'static str, error: ParseError) -> Error {
- Error {
- attr_name: attr_name,
- err: AttributeError::Parse (error)
- }
- }
-
- pub fn value_error (attr_name: &'static str, description: String) -> Error {
- Error {
- attr_name: attr_name,
- err: AttributeError::Value (description)
- }
- }
-}
-
-impl error::Error for Error {
- fn description (&self) -> &str {
- match self.err {
- AttributeError::Parse (ref n) => &n.description,
- AttributeError::Value (_) => &"invalid attribute value"
- }
- }
-}
-
-impl fmt::Display for Error {
- fn fmt (&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self.err {
- AttributeError::Parse (ref n) => write! (f,
- "error parsing value for attribute \"{}\": {}",
- self.attr_name,
- n.display),
-
- AttributeError::Value (ref s) => write! (f,
- "invalid value for attribute \"{}\": {}",
- self.attr_name,
- s)
- }
- }
-}
-
// 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
@@ -111,10 +54,10 @@ impl fmt::Display for Error {
// defined.
//
// Alternatively, we could try to parse/validate all the attributes
-// that come in an element and build up a Vec<Error>. However, we
+// that come in an element and build up a Vec<NodeError>. However, we
// don't do this now. Doing that may be more useful for an SVG
// validator, not a renderer like librsvg is.
-pub type NodeResult = Result<(), Error>;
+pub type NodeResult = Result<(), NodeError>;
pub struct Node {
node_type: NodeType,
diff --git a/rust/src/shapes.rs b/rust/src/shapes.rs
index 8027758..2c22f00 100644
--- a/rust/src/shapes.rs
+++ b/rust/src/shapes.rs
@@ -4,6 +4,7 @@ extern crate libc;
use drawing_ctx;
use drawing_ctx::*;
+use error::*;
use handle::RsvgHandle;
use length::*;
use marker;
@@ -144,7 +145,7 @@ impl NodeTrait for NodePoly {
break;
},
- Err (e) => { return Err (Error::parse_error (name, e)); }
+ Err (e) => { return Err (NodeError::parse_error (name, e)); }
}
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]