[librsvg] property_bag.rs: Make parse_or_default() know only about AttributeError, not ParseError
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] property_bag.rs: Make parse_or_default() know only about AttributeError, not ParseError
- Date: Thu, 16 Mar 2017 05:16:55 +0000 (UTC)
commit 9452ba089960926cd8475f73e7c19d8dc97cfa15
Author: Federico Mena Quintero <federico gnome org>
Date: Wed Mar 15 22:22:29 2017 -0600
property_bag.rs: Make parse_or_default() know only about AttributeError, not ParseError
Now the parsing step must yield an AttributeError, not a low-level
ParseError.
Make aspect_ratio.rs, length.rs, viewbox.rs emit an AttributeError, not
a ParseError.
Use parse_or_default() for the "vbox" property of marker.rs.
rust/src/aspect_ratio.rs | 9 +++++----
rust/src/error.rs | 7 +++++++
rust/src/length.rs | 24 +++++++++++++++++++++---
rust/src/marker.rs | 2 +-
rust/src/property_bag.rs | 4 ++--
rust/src/viewbox.rs | 43 +++++++++++--------------------------------
6 files changed, 47 insertions(+), 42 deletions(-)
---
diff --git a/rust/src/aspect_ratio.rs b/rust/src/aspect_ratio.rs
index cea00c4..7e492f1 100644
--- a/rust/src/aspect_ratio.rs
+++ b/rust/src/aspect_ratio.rs
@@ -23,6 +23,7 @@ use self::glib::translate::*;
use std::str::FromStr;
use parsers::ParseError;
+use error::*;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FitMode {
@@ -255,14 +256,14 @@ enum ParseState {
Finished
}
-fn make_err () -> ParseError {
- ParseError::new ("expected \"[defer] <align> [meet | slice]\"")
+fn make_err () -> AttributeError {
+ AttributeError::Parse (ParseError::new ("expected \"[defer] <align> [meet | slice]\""))
}
impl FromStr for AspectRatio {
- type Err = ParseError;
+ type Err = AttributeError;
- fn from_str(s: &str) -> Result<AspectRatio, ParseError> {
+ fn from_str(s: &str) -> Result<AspectRatio, AttributeError> {
let mut defer = false;
let mut align: Align = Default::default ();
let mut fit_mode = FitMode::Meet;
diff --git a/rust/src/error.rs b/rust/src/error.rs
index f8fc632..b965fff 100644
--- a/rust/src/error.rs
+++ b/rust/src/error.rs
@@ -32,6 +32,13 @@ impl NodeError {
err: AttributeError::Value (description)
}
}
+
+ pub fn attribute_error (attr_name: &'static str, error: AttributeError) -> NodeError {
+ NodeError {
+ attr_name: attr_name,
+ err: error
+ }
+ }
}
impl error::Error for NodeError {
diff --git a/rust/src/length.rs b/rust/src/length.rs
index 3201cc6..7eb7d60 100644
--- a/rust/src/length.rs
+++ b/rust/src/length.rs
@@ -9,6 +9,7 @@ use drawing_ctx;
use drawing_ctx::RsvgDrawingCtx;
use parsers;
use parsers::ParseError;
+use error::*;
/* Keep this in sync with ../../rsvg-private.h:LengthUnit */
#[repr(C)]
@@ -97,12 +98,12 @@ pub extern fn rsvg_length_parse (string: *const libc::c_char, dir: LengthDir) ->
* length refers.
*/
-fn make_err () -> ParseError {
- ParseError::new ("expected length: number(\"em\" | \"ex\" | \"px\" | \"in\" | \"cm\" | \"mm\" | \"pt\" |
\"pc\" | \"%\")?")
+fn make_err () -> AttributeError {
+ AttributeError::Parse (ParseError::new ("expected length: number(\"em\" | \"ex\" | \"px\" | \"in\" |
\"cm\" | \"mm\" | \"pt\" | \"pc\" | \"%\")?"))
}
impl RsvgLength {
- pub fn parse (string: &str, dir: LengthDir) -> Result <RsvgLength, ParseError> {
+ pub fn parse (string: &str, dir: LengthDir) -> Result <RsvgLength, AttributeError> {
let r = parsers::number_and_units (string.as_bytes ()).to_full_result ();
match r {
@@ -390,6 +391,23 @@ mod tests {
}
}
+ }
+
+ #[test]
+ fn empty_length_yields_error () {
+ assert! (is_parse_error (&RsvgLength::parse ("", LengthDir::Both)));
+ }
+ #[test]
+ fn invalid_unit_yields_error () {
+ assert! (is_parse_error (&RsvgLength::parse ("8furlong", LengthDir::Both)));
+ }
+
+ #[test]
+ fn invalid_font_size_yields_error () {
+ // FIXME: this is intended to test the (absence of) the "larger" et al values.
+ // Since they really be in FontSize, not RsvgLength, we should remember
+ // to move this test to that type later.
+ assert! (is_parse_error (&RsvgLength::parse ("furlong", LengthDir::Both)));
}
}
diff --git a/rust/src/marker.rs b/rust/src/marker.rs
index 3ab2160..8a5ca00 100644
--- a/rust/src/marker.rs
+++ b/rust/src/marker.rs
@@ -225,7 +225,7 @@ impl NodeTrait for NodeMarker {
self.orient.set (property_bag::lookup_and_parse (pbag, "orient"));
self.aspect.set (property_bag::lookup_and_parse (pbag, "preserveAspectRatio"));
- self.vbox.set (property_bag::lookup_and_parse (pbag, "viewBox"));
+ self.vbox.set (property_bag::parse_or_default (pbag, "viewBox")?);
self.aspect.set (property_bag::parse_or_default (pbag, "preserveAspectRatio")?);
Ok (())
diff --git a/rust/src/property_bag.rs b/rust/src/property_bag.rs
index b553d2f..676db6d 100644
--- a/rust/src/property_bag.rs
+++ b/rust/src/property_bag.rs
@@ -57,12 +57,12 @@ pub fn lookup_length (pbag: *const RsvgPropertyBag, key: &str, length_dir: Lengt
}
pub fn parse_or_default<T> (pbag: *const RsvgPropertyBag, key: &'static str) -> Result <T, NodeError>
- where T: Default + FromStr<Err = ParseError>
+ where T: Default + FromStr<Err = AttributeError>
{
let value = lookup (pbag, key);
if let Some (v) = value {
- T::from_str (&v).map_err (|e| NodeError::parse_error (key, e))
+ T::from_str (&v).map_err (|e| NodeError::attribute_error (key, e))
} else {
Ok (T::default ())
}
diff --git a/rust/src/viewbox.rs b/rust/src/viewbox.rs
index d94bf43..52a816e 100644
--- a/rust/src/viewbox.rs
+++ b/rust/src/viewbox.rs
@@ -3,6 +3,8 @@ extern crate cairo;
use std::fmt;
use std::str::FromStr;
+use error::*;
+use parsers::ParseError;
use parsers;
/* Keep this in sync with rsvg-private.h:RsvgViewBox */
@@ -31,30 +33,10 @@ impl Default for RsvgViewBox {
}
}
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub enum ParseViewBoxError {
- NegativeWidthOrHeight, // In "x y w h", w and h must be nonnegative
- Error // General parsing error
-}
-
-impl fmt::Display for ParseViewBoxError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match *self {
- ParseViewBoxError::NegativeWidthOrHeight => {
- "width and height must not be negative".fmt (f)
- },
-
- ParseViewBoxError::Error => {
- "string does not match 'x [,] y [,] w [,] h'".fmt (f)
- }
- }
- }
-}
-
impl FromStr for RsvgViewBox {
- type Err = ParseViewBoxError;
+ type Err = AttributeError;
- fn from_str (s: &str) -> Result<RsvgViewBox, ParseViewBoxError> {
+ fn from_str (s: &str) -> Result<RsvgViewBox, AttributeError> {
let result = parsers::view_box (s.trim ().as_bytes ()).to_full_result ();
match result {
@@ -66,12 +48,12 @@ impl FromStr for RsvgViewBox {
height: h },
active: true })
} else {
- Err (ParseViewBoxError::NegativeWidthOrHeight)
+ Err (AttributeError::Value ("width and height must not be negative".to_string ()))
}
},
Err (_) => {
- Err (ParseViewBoxError::Error)
+ Err (AttributeError::Parse (ParseError::new ("string does not match 'x [,] y [,] w [,] h'")))
}
}
}
@@ -81,6 +63,7 @@ impl FromStr for RsvgViewBox {
mod tests {
use super::*;
use std::str::FromStr;
+ use error::*;
#[test]
fn parses_valid_viewboxes () {
@@ -101,16 +84,12 @@ mod tests {
#[test]
fn parsing_invalid_viewboxes_yields_error () {
- assert_eq! (RsvgViewBox::from_str (""),
- Err (ParseViewBoxError::Error));
+ assert! (is_parse_error (&RsvgViewBox::from_str ("")));
- assert_eq! (RsvgViewBox::from_str (" 1,2,-3,-4 "),
- Err (ParseViewBoxError::NegativeWidthOrHeight));
+ assert! (is_value_error (&RsvgViewBox::from_str (" 1,2,-3,-4 ")));
- assert_eq! (RsvgViewBox::from_str ("qwerasdfzxcv"),
- Err (ParseViewBoxError::Error));
+ assert! (is_parse_error (&RsvgViewBox::from_str ("qwerasdfzxcv")));
- assert_eq! (RsvgViewBox::from_str (" 1 2 3 4 5"),
- Err (ParseViewBoxError::Error));
+ assert! (is_parse_error (&RsvgViewBox::from_str (" 1 2 3 4 5")));
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]