[librsvg] property_bag.rs: New function parse_or_default()
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] property_bag.rs: New function parse_or_default()
- Date: Thu, 16 Mar 2017 05:16:40 +0000 (UTC)
commit d483d93f838ba0c3eafc155d3bfc9f516bc5f675
Author: Federico Mena Quintero <federico gnome org>
Date: Wed Mar 15 21:37:27 2017 -0600
property_bag.rs: New function parse_or_default()
This looks up a key in the property bag. If the key is not found,
returns a default value.
If the key is found, then the string value is parsed. If the parse is
successful, that value is returned. Otherwise, we turn that into a
NodeError with a parse error inside.
Make marker.rs use that for the "aspect" attribute.
rust/src/aspect_ratio.rs | 46 +++++++++++++++++++++-------------------------
rust/src/marker.rs | 1 +
rust/src/property_bag.rs | 14 ++++++++++++++
3 files changed, 36 insertions(+), 25 deletions(-)
---
diff --git a/rust/src/aspect_ratio.rs b/rust/src/aspect_ratio.rs
index 67c4e1d..cea00c4 100644
--- a/rust/src/aspect_ratio.rs
+++ b/rust/src/aspect_ratio.rs
@@ -20,9 +20,10 @@ extern crate glib;
use self::glib::translate::*;
-use std::fmt;
use std::str::FromStr;
+use parsers::ParseError;
+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FitMode {
Meet,
@@ -254,10 +255,14 @@ enum ParseState {
Finished
}
+fn make_err () -> ParseError {
+ ParseError::new ("expected \"[defer] <align> [meet | slice]\"")
+}
+
impl FromStr for AspectRatio {
- type Err = ParseAspectRatioError;
+ type Err = ParseError;
- fn from_str(s: &str) -> Result<AspectRatio, ParseAspectRatioError> {
+ fn from_str(s: &str) -> Result<AspectRatio, ParseError> {
let mut defer = false;
let mut align: Align = Default::default ();
let mut fit_mode = FitMode::Meet;
@@ -275,7 +280,7 @@ impl FromStr for AspectRatio {
align = parsed_align;
state = ParseState::Fit;
} else {
- return Err(ParseAspectRatioError);
+ return Err(make_err ());
}
},
@@ -284,7 +289,7 @@ impl FromStr for AspectRatio {
align = parsed_align;
state = ParseState::Fit;
} else {
- return Err(ParseAspectRatioError);
+ return Err(make_err ());
}
},
@@ -293,12 +298,12 @@ impl FromStr for AspectRatio {
fit_mode = parsed_fit;
state = ParseState::Finished;
} else {
- return Err(ParseAspectRatioError);
+ return Err(make_err ());
}
},
_ => {
- return Err(ParseAspectRatioError);
+ return Err(make_err ());
}
}
}
@@ -308,7 +313,7 @@ impl FromStr for AspectRatio {
// of the following states:
match state {
ParseState::Fit | ParseState::Finished => {},
- _ => { return Err(ParseAspectRatioError); }
+ _ => { return Err(make_err ()); }
}
Ok (AspectRatio {
@@ -326,15 +331,6 @@ impl FromStr for AspectRatio {
}
}
-#[derive(Debug, Clone, PartialEq, Eq)]
-pub struct ParseAspectRatioError;
-
-impl fmt::Display for ParseAspectRatioError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- "provided string did not match `[defer] <align> [meet | slice]`".fmt (f)
- }
-}
-
#[no_mangle]
pub extern fn rsvg_aspect_ratio_parse (c_str: *const libc::c_char) -> u32 {
let my_str = unsafe { &String::from_glib_none (c_str) };
@@ -374,21 +370,21 @@ mod tests {
#[test]
fn parsing_invalid_strings_yields_error () {
- assert_eq! (AspectRatio::from_str (""), Err(ParseAspectRatioError));
+ assert! (AspectRatio::from_str ("").is_err ());
- assert_eq! (AspectRatio::from_str ("defer"), Err(ParseAspectRatioError));
+ assert! (AspectRatio::from_str ("defer").is_err ());
- assert_eq! (AspectRatio::from_str ("defer foo"), Err(ParseAspectRatioError));
+ assert! (AspectRatio::from_str ("defer foo").is_err ());
- assert_eq! (AspectRatio::from_str ("defer xmidymid"), Err(ParseAspectRatioError));
+ assert! (AspectRatio::from_str ("defer xmidymid").is_err ());
- assert_eq! (AspectRatio::from_str ("defer XmidYmid foo"), Err(ParseAspectRatioError));
+ assert! (AspectRatio::from_str ("defer XmidYmid foo").is_err ());
- assert_eq! (AspectRatio::from_str ("xmidymid"), Err(ParseAspectRatioError));
+ assert! (AspectRatio::from_str ("xmidymid").is_err ());
- assert_eq! (AspectRatio::from_str ("XmidYmid foo"), Err(ParseAspectRatioError));
+ assert! (AspectRatio::from_str ("XmidYmid foo").is_err ());
- assert_eq! (AspectRatio::from_str ("defer XmidYmid meet foo"), Err(ParseAspectRatioError));
+ assert! (AspectRatio::from_str ("defer XmidYmid meet foo").is_err ());
}
#[test]
diff --git a/rust/src/marker.rs b/rust/src/marker.rs
index c577829..3ab2160 100644
--- a/rust/src/marker.rs
+++ b/rust/src/marker.rs
@@ -226,6 +226,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.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 b907951..b553d2f 100644
--- a/rust/src/property_bag.rs
+++ b/rust/src/property_bag.rs
@@ -5,7 +5,9 @@ use std::str::FromStr;
use self::glib::translate::*;
+use error::*;
use length::*;
+use parsers::ParseError;
pub enum RsvgPropertyBag {}
@@ -53,3 +55,15 @@ pub fn lookup_length (pbag: *const RsvgPropertyBag, key: &str, length_dir: Lengt
RsvgLength::default ()
}
}
+
+pub fn parse_or_default<T> (pbag: *const RsvgPropertyBag, key: &'static str) -> Result <T, NodeError>
+ where T: Default + FromStr<Err = ParseError>
+{
+ let value = lookup (pbag, key);
+
+ if let Some (v) = value {
+ T::from_str (&v).map_err (|e| NodeError::parse_error (key, e))
+ } else {
+ Ok (T::default ())
+ }
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]