[librsvg: 3/30] parsers: helper structs for NonZero and NonNegative numbers




commit 6fe72dd708c3b0b2bad01d6c565ad383b9e3183c
Author: Paolo Borelli <pborelli gnome org>
Date:   Wed Dec 23 17:56:03 2020 +0100

    parsers: helper structs for NonZero and NonNegative numbers
    
    Add two helper struct that raise an error if the parsed value
    is not valid

 src/parsers.rs | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
---
diff --git a/src/parsers.rs b/src/parsers.rs
index 3a63bbf1..f5950d2b 100644
--- a/src/parsers.rs
+++ b/src/parsers.rs
@@ -96,6 +96,38 @@ impl Parse for f64 {
     }
 }
 
+/// Non-Zero number (for instance useful for "divisor")
+#[derive(Debug, Copy, Clone, PartialEq)]
+pub struct NonZero(pub f64);
+
+impl Parse for NonZero {
+    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Self, ParseError<'i>> {
+        let loc = parser.current_source_location();
+        let n = Parse::parse(parser)?;
+        if n != 0.0 {
+            Ok(NonZero(n))
+        } else {
+            Err(loc.new_custom_error(ValueErrorKind::value_error("expected non zero number")))
+        }
+    }
+}
+
+/// Non-Negative number
+#[derive(Debug, Copy, Clone, PartialEq)]
+pub struct NonNegative(pub f64);
+
+impl Parse for NonNegative {
+    fn parse<'i>(parser: &mut Parser<'i, '_>) -> Result<Self, ParseError<'i>> {
+        let loc = parser.current_source_location();
+        let n = Parse::parse(parser)?;
+        if n >= 0.0 {
+            Ok(NonNegative(n))
+        } else {
+            Err(loc.new_custom_error(ValueErrorKind::value_error("expected non negative number")))
+        }
+    }
+}
+
 /// CSS number-optional-number
 ///
 /// https://www.w3.org/TR/SVG/types.html#DataTypeNumberOptionalNumber


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