[librsvg: 38/43] Convert all the remaining uses of f64::parse to the CssParseError version
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 38/43] Convert all the remaining uses of f64::parse to the CssParseError version
- Date: Sat, 21 Dec 2019 02:32:41 +0000 (UTC)
commit b1a3738dc72fb8f802da7bf1e66fafc3d2bee840
Author: Federico Mena Quintero <federico gnome org>
Date: Fri Dec 20 19:25:04 2019 -0600
Convert all the remaining uses of f64::parse to the CssParseError version
rsvg_internals/src/filters/light/light_source.rs | 28 ++++-----
rsvg_internals/src/filters/light/lighting.rs | 72 ++++++++++--------------
rsvg_internals/src/filters/offset.rs | 6 +-
rsvg_internals/src/parsers.rs | 11 ----
4 files changed, 47 insertions(+), 70 deletions(-)
---
diff --git a/rsvg_internals/src/filters/light/light_source.rs
b/rsvg_internals/src/filters/light/light_source.rs
index 45d43070..1f217b86 100644
--- a/rsvg_internals/src/filters/light/light_source.rs
+++ b/rsvg_internals/src/filters/light/light_source.rs
@@ -4,7 +4,7 @@ use nalgebra::Vector3;
use crate::filters::context::FilterContext;
use crate::node::{NodeResult, NodeTrait, RsvgNode};
-use crate::parsers::{ParseValue};
+use crate::parsers::ParseValueToParseError;
use crate::property_bag::PropertyBag;
use crate::util::clamp;
@@ -106,8 +106,8 @@ impl NodeTrait for FeDistantLight {
fn set_atts(&mut self, _: Option<&RsvgNode>, pbag: &PropertyBag<'_>) -> NodeResult {
for (attr, value) in pbag.iter() {
match attr.expanded() {
- expanded_name!(svg "azimuth") => self.azimuth = attr.parse(value)?,
- expanded_name!(svg "elevation") => self.elevation = attr.parse(value)?,
+ expanded_name!(svg "azimuth") => self.azimuth = attr.parse_to_parse_error(value)?,
+ expanded_name!(svg "elevation") => self.elevation = attr.parse_to_parse_error(value)?,
_ => (),
}
}
@@ -138,9 +138,9 @@ impl NodeTrait for FePointLight {
fn set_atts(&mut self, _: Option<&RsvgNode>, pbag: &PropertyBag<'_>) -> NodeResult {
for (attr, value) in pbag.iter() {
match attr.expanded() {
- expanded_name!(svg "x") => self.x = attr.parse(value)?,
- expanded_name!(svg "y") => self.y = attr.parse(value)?,
- expanded_name!(svg "z") => self.z = attr.parse(value)?,
+ expanded_name!(svg "x") => self.x = attr.parse_to_parse_error(value)?,
+ expanded_name!(svg "y") => self.y = attr.parse_to_parse_error(value)?,
+ expanded_name!(svg "z") => self.z = attr.parse_to_parse_error(value)?,
_ => (),
}
}
@@ -187,19 +187,19 @@ impl NodeTrait for FeSpotLight {
fn set_atts(&mut self, _: Option<&RsvgNode>, pbag: &PropertyBag<'_>) -> NodeResult {
for (attr, value) in pbag.iter() {
match attr.expanded() {
- expanded_name!(svg "x") => self.x = attr.parse(value)?,
- expanded_name!(svg "y") => self.y = attr.parse(value)?,
- expanded_name!(svg "z") => self.z = attr.parse(value)?,
- expanded_name!(svg "pointsAtX") => self.points_at_x = attr.parse(value)?,
- expanded_name!(svg "pointsAtY") => self.points_at_y = attr.parse(value)?,
- expanded_name!(svg "pointsAtZ") => self.points_at_z = attr.parse(value)?,
+ expanded_name!(svg "x") => self.x = attr.parse_to_parse_error(value)?,
+ expanded_name!(svg "y") => self.y = attr.parse_to_parse_error(value)?,
+ expanded_name!(svg "z") => self.z = attr.parse_to_parse_error(value)?,
+ expanded_name!(svg "pointsAtX") => self.points_at_x = attr.parse_to_parse_error(value)?,
+ expanded_name!(svg "pointsAtY") => self.points_at_y = attr.parse_to_parse_error(value)?,
+ expanded_name!(svg "pointsAtZ") => self.points_at_z = attr.parse_to_parse_error(value)?,
expanded_name!(svg "specularExponent") => {
- self.specular_exponent = attr.parse(value)?
+ self.specular_exponent = attr.parse_to_parse_error(value)?
}
expanded_name!(svg "limitingConeAngle") => {
- self.limiting_cone_angle = Some(attr.parse(value)?)
+ self.limiting_cone_angle = Some(attr.parse_to_parse_error(value)?)
}
_ => (),
diff --git a/rsvg_internals/src/filters/light/lighting.rs b/rsvg_internals/src/filters/light/lighting.rs
index a17087c7..321f4ac9 100644
--- a/rsvg_internals/src/filters/light/lighting.rs
+++ b/rsvg_internals/src/filters/light/lighting.rs
@@ -12,32 +12,19 @@ use crate::error::*;
use crate::filters::{
context::{FilterContext, FilterOutput, FilterResult},
light::{
- bottom_left_normal,
- bottom_right_normal,
- bottom_row_normal,
- interior_normal,
- left_column_normal,
- light_source::FeDistantLight,
- light_source::FePointLight,
- light_source::FeSpotLight,
- light_source::LightSource,
- right_column_normal,
- top_left_normal,
- top_right_normal,
- top_row_normal,
- Normal,
+ bottom_left_normal, bottom_right_normal, bottom_row_normal, interior_normal,
+ left_column_normal, light_source::FeDistantLight, light_source::FePointLight,
+ light_source::FeSpotLight, light_source::LightSource, right_column_normal, top_left_normal,
+ top_right_normal, top_row_normal, Normal,
},
- FilterEffect,
- FilterError,
- PrimitiveWithInput,
+ FilterEffect, FilterError, PrimitiveWithInput,
};
use crate::node::{CascadedValues, NodeResult, NodeTrait, NodeType, RsvgNode};
-use crate::parsers::{NumberOptionalNumber, Parse, ParseValue, ParseValueToParseError};
+use crate::parsers::{NumberOptionalNumber, ParseValueToParseError};
use crate::property_bag::PropertyBag;
use crate::surface_utils::{
shared_surface::{SharedImageSurface, SurfaceType},
- ImageSurfaceDataExt,
- Pixel,
+ ImageSurfaceDataExt, Pixel,
};
use crate::util::clamp;
@@ -67,7 +54,9 @@ impl Common {
for (attr, value) in pbag.iter() {
match attr.expanded() {
- expanded_name!(svg "surfaceScale") => self.surface_scale = attr.parse(value)?,
+ expanded_name!(svg "surfaceScale") => {
+ self.surface_scale = attr.parse_to_parse_error(value)?
+ }
expanded_name!(svg "kernelUnitLength") => {
let NumberOptionalNumber(x, y) = attr.parse_to_parse_error_and_validate(
@@ -80,7 +69,7 @@ impl Common {
"kernelUnitLength can't be less or equal to zero",
))
}
- }
+ },
)?;
self.kernel_unit_length = Some((x, y));
@@ -117,17 +106,15 @@ impl NodeTrait for FeDiffuseLighting {
for (attr, value) in pbag.iter() {
match attr.expanded() {
expanded_name!(svg "diffuseConstant") => {
- self.diffuse_constant = f64::parse_str(value)
- .and_then(|x| {
- if x >= 0.0 {
- Ok(x)
- } else {
- Err(ValueErrorKind::value_error(
- "diffuseConstant can't be negative",
- ))
- }
- })
- .attribute(attr)?;
+ self.diffuse_constant = attr.parse_to_parse_error_and_validate(value, |x| {
+ if x >= 0.0 {
+ Ok(x)
+ } else {
+ Err(ValueErrorKind::value_error(
+ "diffuseConstant can't be negative",
+ ))
+ }
+ })?;
}
_ => (),
}
@@ -188,8 +175,8 @@ impl NodeTrait for FeSpecularLighting {
for (attr, value) in pbag.iter() {
match attr.expanded() {
expanded_name!(svg "specularConstant") => {
- self.specular_constant = f64::parse_str(value)
- .and_then(|x| {
+ self.specular_constant =
+ attr.parse_to_parse_error_and_validate(value, |x| {
if x >= 0.0 {
Ok(x)
} else {
@@ -197,12 +184,11 @@ impl NodeTrait for FeSpecularLighting {
"specularConstant can't be negative",
))
}
- })
- .attribute(attr)?;
+ })?;
}
expanded_name!(svg "specularExponent") => {
- self.specular_exponent = f64::parse_str(value)
- .and_then(|x| {
+ self.specular_exponent =
+ attr.parse_to_parse_error_and_validate(value, |x| {
if x >= 1.0 && x <= 128.0 {
Ok(x)
} else {
@@ -210,7 +196,7 @@ impl NodeTrait for FeSpecularLighting {
"specularExponent should be between 1.0 and 128.0",
))
}
- }).attribute(attr)?;
+ })?;
}
_ => (),
}
@@ -299,7 +285,8 @@ macro_rules! impl_lighting_filter {
if let Some((ox, oy)) = scale {
// Scale the input surface to match kernel_unit_length.
- let (new_surface, new_bounds) = input_surface.scale(bounds, 1.0 / ox, 1.0 / oy)?;
+ let (new_surface, new_bounds) =
+ input_surface.scale(bounds, 1.0 / ox, 1.0 / oy)?;
input_surface = new_surface;
bounds = new_bounds;
@@ -338,7 +325,8 @@ macro_rules! impl_lighting_filter {
// compute the factor just once for the three colors
let factor = self.compute_factor(normal, light_vector);
- let compute = |x| (clamp(factor * f64::from(x), 0.0, 255.0) + 0.5) as u8;
+ let compute =
+ |x| (clamp(factor * f64::from(x), 0.0, 255.0) + 0.5) as u8;
let r = compute(light_color.red);
let g = compute(light_color.green);
diff --git a/rsvg_internals/src/filters/offset.rs b/rsvg_internals/src/filters/offset.rs
index 76f35931..150b9008 100644
--- a/rsvg_internals/src/filters/offset.rs
+++ b/rsvg_internals/src/filters/offset.rs
@@ -3,7 +3,7 @@ use markup5ever::{expanded_name, local_name, namespace_url, ns};
use crate::drawing_ctx::DrawingCtx;
use crate::node::{NodeResult, NodeTrait, RsvgNode};
-use crate::parsers::{ParseValue};
+use crate::parsers::ParseValueToParseError;
use crate::property_bag::PropertyBag;
use crate::surface_utils::shared_surface::SharedImageSurface;
@@ -37,8 +37,8 @@ impl NodeTrait for FeOffset {
for (attr, value) in pbag.iter() {
match attr.expanded() {
- expanded_name!(svg "dx") => self.dx = attr.parse(value)?,
- expanded_name!(svg "dy") => self.dy = attr.parse(value)?,
+ expanded_name!(svg "dx") => self.dx = attr.parse_to_parse_error(value)?,
+ expanded_name!(svg "dy") => self.dy = attr.parse_to_parse_error(value)?,
_ => (),
}
}
diff --git a/rsvg_internals/src/parsers.rs b/rsvg_internals/src/parsers.rs
index f3de8892..d5f63127 100644
--- a/rsvg_internals/src/parsers.rs
+++ b/rsvg_internals/src/parsers.rs
@@ -90,17 +90,6 @@ impl<T: ParseToParseError> ParseValueToParseError<T> for QualName {
}
}
-impl Parse for f64 {
- /// Avoid infinities, and convert to `f64`.
- /// https://www.w3.org/TR/SVG11/types.html#DataTypeNumber
- fn parse(parser: &mut Parser<'_, '_>) -> Result<f64, ValueErrorKind> {
- parser
- .expect_number()
- .map_err(|_| ValueErrorKind::parse_error("parse error"))
- .and_then(|n| Ok(f64::from(finite_f32(n)?)))
- }
-}
-
pub trait ParseToParseError: Sized {
fn parse_to_parse_error<'i>(parser: &mut Parser<'i, '_>) -> Result<Self, CssParseError<'i>>;
fn parse_str_to_parse_error<'i>(s: &'i str) -> Result<Self, CssParseError<'i>> {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]