[librsvg: 23/43] impl ParseToParseError for i32 instead of using an integer() function
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 23/43] impl ParseToParseError for i32 instead of using an integer() function
- Date: Sat, 21 Dec 2019 02:31:25 +0000 (UTC)
commit 245f8735523f489ac8274c44d056e30b0e950c3c
Author: Federico Mena Quintero <federico gnome org>
Date: Fri Dec 20 14:23:21 2019 -0600
impl ParseToParseError for i32 instead of using an integer() function
rsvg_internals/src/filters/convolve_matrix.rs | 49 +++++++++++++--------------
rsvg_internals/src/filters/turbulence.rs | 4 +--
rsvg_internals/src/parsers.rs | 25 +++++++-------
3 files changed, 38 insertions(+), 40 deletions(-)
---
diff --git a/rsvg_internals/src/filters/convolve_matrix.rs b/rsvg_internals/src/filters/convolve_matrix.rs
index 6c2b706f..1c24a022 100644
--- a/rsvg_internals/src/filters/convolve_matrix.rs
+++ b/rsvg_internals/src/filters/convolve_matrix.rs
@@ -7,7 +7,7 @@ use crate::drawing_ctx::DrawingCtx;
use crate::error::*;
use crate::node::{NodeResult, NodeTrait, RsvgNode};
use crate::number_list::{NumberList, NumberListLength};
-use crate::parsers::{self, Parse, ParseValue};
+use crate::parsers::{self, Parse, ParseValue, ParseValueToParseError};
use crate::property_bag::PropertyBag;
use crate::rect::IRect;
use crate::surface_utils::{
@@ -112,34 +112,32 @@ impl NodeTrait for FeConvolveMatrix {
for (attr, value) in pbag.iter() {
match attr.expanded() {
expanded_name!(svg "targetX") => {
- self.target_x = Some(
- parsers::integer(value)
- .and_then(|x| {
- if x >= 0 && x < self.order.0 as i32 {
- Ok(x as u32)
- } else {
- Err(ValueErrorKind::value_error(
+ self.target_x = {
+ let v = attr.parse_to_parse_error_and_validate(value, |v: i32| {
+ if v >= 0 && v < self.order.0 as i32 {
+ Ok(v)
+ } else {
+ Err(ValueErrorKind::value_error(
"targetX must be greater or equal to zero and less than orderX",
))
- }
- })
- .attribute(attr)?,
- )
+ }
+ })?;
+ Some(v as u32)
+ }
}
expanded_name!(svg "targetY") => {
- self.target_y = Some(
- parsers::integer(value)
- .and_then(|x| {
- if x >= 0 && x < self.order.1 as i32 {
- Ok(x as u32)
- } else {
- Err(ValueErrorKind::value_error(
+ self.target_y = {
+ let v = attr.parse_to_parse_error_and_validate(value, |v: i32| {
+ if v >= 0 && v < self.order.1 as i32 {
+ Ok(v)
+ } else {
+ Err(ValueErrorKind::value_error(
"targetY must be greater or equal to zero and less than orderY",
))
- }
- })
- .attribute(attr)?,
- )
+ }
+ })?;
+ Some(v as u32)
+ }
}
_ => (),
}
@@ -163,8 +161,9 @@ impl NodeTrait for FeConvolveMatrix {
// #352: Parse as an unbounded list rather than exact length to prevent aborts due
// to huge allocation attempts by underlying Vec::with_capacity().
- let NumberList(v) = NumberList::parse_str_to_parse_error(value, NumberListLength::Unbounded)
- .attribute(attr.clone())?;
+ let NumberList(v) =
+ NumberList::parse_str_to_parse_error(value, NumberListLength::Unbounded)
+ .attribute(attr.clone())?;
if v.len() != number_of_elements {
return Err(ValueErrorKind::value_error(&format!(
diff --git a/rsvg_internals/src/filters/turbulence.rs b/rsvg_internals/src/filters/turbulence.rs
index e562f6bd..50a5b020 100644
--- a/rsvg_internals/src/filters/turbulence.rs
+++ b/rsvg_internals/src/filters/turbulence.rs
@@ -5,7 +5,7 @@ use markup5ever::{expanded_name, local_name, namespace_url, ns};
use crate::drawing_ctx::DrawingCtx;
use crate::error::*;
use crate::node::{CascadedValues, NodeResult, NodeTrait, RsvgNode};
-use crate::parsers::{self, Parse, ParseValue};
+use crate::parsers::{self, Parse, ParseValue, ParseValueToParseError};
use crate::property_bag::PropertyBag;
use crate::surface_utils::{
shared_surface::{SharedImageSurface, SurfaceType},
@@ -76,7 +76,7 @@ impl NodeTrait for FeTurbulence {
.attribute(attr)?
}
expanded_name!(svg "numOctaves") => {
- self.num_octaves = parsers::integer(value).attribute(attr)?
+ self.num_octaves = attr.parse_to_parse_error(value)?;
}
// Yes, seed needs to be parsed as a number and then truncated.
expanded_name!(svg "seed") => {
diff --git a/rsvg_internals/src/parsers.rs b/rsvg_internals/src/parsers.rs
index 31adbd35..c96c59ed 100644
--- a/rsvg_internals/src/parsers.rs
+++ b/rsvg_internals/src/parsers.rs
@@ -153,14 +153,13 @@ pub fn number_optional_number(s: &str) -> Result<(f64, f64), ValueErrorKind> {
}
}
-// integer
-//
-// https://www.w3.org/TR/SVG11/types.html#DataTypeInteger
-pub fn integer(s: &str) -> Result<i32, ValueErrorKind> {
- let mut input = ParserInput::new(s);
- let mut parser = Parser::new(&mut input);
-
- Ok(parser.expect_integer()?)
+impl ParseToParseError for i32 {
+ /// CSS integer
+ ///
+ /// https://www.w3.org/TR/SVG11/types.html#DataTypeInteger
+ fn parse_to_parse_error<'i>(parser: &mut Parser<'i, '_>) -> Result<Self, CssParseError<'i>> {
+ Ok(parser.expect_integer()?)
+ }
}
// integer-optional-integer
@@ -240,15 +239,15 @@ mod tests {
#[test]
fn parses_integer() {
- assert_eq!(integer("1"), Ok(1));
- assert_eq!(integer("-1"), Ok(-1));
+ assert_eq!(i32::parse_str_to_parse_error("1"), Ok(1));
+ assert_eq!(i32::parse_str_to_parse_error("-1"), Ok(-1));
}
#[test]
fn invalid_integer() {
- assert!(integer("").is_err());
- assert!(integer("1x").is_err());
- assert!(integer("1.5").is_err());
+ assert!(i32::parse_str_to_parse_error("").is_err());
+ assert!(i32::parse_str_to_parse_error("1x").is_err());
+ assert!(i32::parse_str_to_parse_error("1.5").is_err());
}
#[test]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]