[librsvg: 19/95] parsers::parse() - New function, to replace property_bag::parse_*()



commit 82ac28116144fa5cbe08421640e6c9410cf42341
Author: Federico Mena Quintero <federico gnome org>
Date:   Tue Feb 13 10:06:18 2018 -0600

    parsers::parse() - New function, to replace property_bag::parse_*()
    
    property_bag::parse_or_none() would return an Option.  We will no
    longer need it because we'll iterate over attributes that *are*
    present, not try to match against attributes that may not be there.
    
    property_bag::parse_or_default() will not be needed, since the node
    creation functions will set up default values themselves.
    
    property_bag::parse_or_value() will not be needed for the same reason.

 rust/src/parsers.rs | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
---
diff --git a/rust/src/parsers.rs b/rust/src/parsers.rs
index ad03c441..36a7f991 100644
--- a/rust/src/parsers.rs
+++ b/rust/src/parsers.rs
@@ -9,6 +9,7 @@ use std::ptr;
 use std::slice;
 use std::str;
 
+use error::{AttributeError, NodeError};
 use util::utf8_cstr;
 
 #[derive(Debug, Clone, PartialEq)]
@@ -35,6 +36,29 @@ pub trait Parse: Sized {
     fn parse (s: &str, data: Self::Data) -> Result<Self, Self::Err>;
 }
 
+/// Parses a `value` string into a type `T` with an optional validation function.
+///
+/// Some value types need some extra `data` to be parsed.  This
+/// corresponds to the `<T as Parse>::Data` associated type.  For
+/// example, an `RsvgLength` has an associated `type Data =
+/// LengthDir`, so to parse a length value, you could specify
+/// `LengthDir::Horizontal` for `data`, for example.
+pub fn parse<T>(key: &str,
+                value: &str,
+                data: <T as Parse>::Data,
+                validate: Option<fn(T) -> Result<T, AttributeError>>) -> Result <T, NodeError>
+    where T: Parse<Err = AttributeError> + Copy
+{
+    T::parse (value, data)
+        .and_then (|v|
+                   if let Some(validate) = validate {
+                       validate(v)
+                   } else {
+                       Ok(v)
+                   })
+        .map_err (|e| NodeError::attribute_error (key, e))
+}
+
 // angle:
 // https://www.w3.org/TR/SVG/types.html#DataTypeAngle
 //


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