[librsvg: 36/43] Convert AspectRatio, MarkerUnits to CssParseError
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 36/43] Convert AspectRatio, MarkerUnits to CssParseError
- Date: Sat, 21 Dec 2019 02:32:31 +0000 (UTC)
commit 104b3531c2135aafd51edd0412f2d7f1cfb373ac
Author: Federico Mena Quintero <federico gnome org>
Date: Fri Dec 20 19:03:02 2019 -0600
Convert AspectRatio, MarkerUnits to CssParseError
rsvg_internals/src/aspect_ratio.rs | 75 +++++++++++++++++--------------------
rsvg_internals/src/filters/image.rs | 4 +-
rsvg_internals/src/image.rs | 4 +-
rsvg_internals/src/marker.rs | 23 ++++++------
rsvg_internals/src/pattern.rs | 4 +-
rsvg_internals/src/structure.rs | 6 +--
6 files changed, 55 insertions(+), 61 deletions(-)
---
diff --git a/rsvg_internals/src/aspect_ratio.rs b/rsvg_internals/src/aspect_ratio.rs
index d0109d61..7568a0d5 100644
--- a/rsvg_internals/src/aspect_ratio.rs
+++ b/rsvg_internals/src/aspect_ratio.rs
@@ -25,7 +25,7 @@ use std::ops::Deref;
use cairo;
use crate::error::*;
-use crate::parsers::Parse;
+use crate::parsers::ParseToParseError;
use crate::rect::Rect;
use crate::viewbox::ViewBox;
use cssparser::{BasicParseError, Parser};
@@ -221,24 +221,19 @@ fn parse_fit_mode<'i>(parser: &mut Parser<'i, '_>) -> Result<FitMode, BasicParse
)
}
-impl Parse for AspectRatio {
- fn parse(parser: &mut Parser<'_, '_>) -> Result<AspectRatio, ValueErrorKind> {
+impl ParseToParseError for AspectRatio {
+ fn parse_to_parse_error<'i>(parser: &mut Parser<'i, '_>) -> Result<AspectRatio, CssParseError<'i>> {
let defer = parser
.try_parse(|p| p.expect_ident_matching("defer"))
.is_ok();
- let align_xy = parser
- .try_parse(|p| parse_align_xy(p))
- .map_err(|_e| ValueErrorKind::parse_error("parse error"))?;
+ let align_xy = parser.try_parse(|p| parse_align_xy(p))?;
let fit = parser
.try_parse(|p| parse_fit_mode(p))
- .map_err(|_e| ValueErrorKind::parse_error("parse error"))
.unwrap_or_default();
- parser
- .expect_exhausted()
- .map_err(|_| ValueErrorKind::parse_error("extra data in AspectRatio"))?;
+ parser.expect_exhausted()?;
let align = align_xy.map(|(x, y)| Align { x, y, fit });
@@ -253,18 +248,18 @@ mod tests {
#[test]
fn parsing_invalid_strings_yields_error() {
- assert!(AspectRatio::parse_str("").is_err());
- assert!(AspectRatio::parse_str("defer").is_err());
- assert!(AspectRatio::parse_str("defer foo").is_err());
- assert!(AspectRatio::parse_str("defer xMidYMid foo").is_err());
- assert!(AspectRatio::parse_str("xMidYMid foo").is_err());
- assert!(AspectRatio::parse_str("defer xMidYMid meet foo").is_err());
+ assert!(AspectRatio::parse_str_to_parse_error("").is_err());
+ assert!(AspectRatio::parse_str_to_parse_error("defer").is_err());
+ assert!(AspectRatio::parse_str_to_parse_error("defer foo").is_err());
+ assert!(AspectRatio::parse_str_to_parse_error("defer xMidYMid foo").is_err());
+ assert!(AspectRatio::parse_str_to_parse_error("xMidYMid foo").is_err());
+ assert!(AspectRatio::parse_str_to_parse_error("defer xMidYMid meet foo").is_err());
}
#[test]
fn parses_valid_strings() {
assert_eq!(
- AspectRatio::parse_str("defer none"),
+ AspectRatio::parse_str_to_parse_error("defer none"),
Ok(AspectRatio {
defer: true,
align: None,
@@ -272,7 +267,7 @@ mod tests {
);
assert_eq!(
- AspectRatio::parse_str("xMidYMid"),
+ AspectRatio::parse_str_to_parse_error("xMidYMid"),
Ok(AspectRatio {
defer: false,
align: Some(Align {
@@ -284,7 +279,7 @@ mod tests {
);
assert_eq!(
- AspectRatio::parse_str("defer xMidYMid"),
+ AspectRatio::parse_str_to_parse_error("defer xMidYMid"),
Ok(AspectRatio {
defer: true,
align: Some(Align {
@@ -296,7 +291,7 @@ mod tests {
);
assert_eq!(
- AspectRatio::parse_str("defer xMinYMax"),
+ AspectRatio::parse_str_to_parse_error("defer xMinYMax"),
Ok(AspectRatio {
defer: true,
align: Some(Align {
@@ -308,7 +303,7 @@ mod tests {
);
assert_eq!(
- AspectRatio::parse_str("defer xMaxYMid meet"),
+ AspectRatio::parse_str_to_parse_error("defer xMaxYMid meet"),
Ok(AspectRatio {
defer: true,
align: Some(Align {
@@ -320,7 +315,7 @@ mod tests {
);
assert_eq!(
- AspectRatio::parse_str("defer xMinYMax slice"),
+ AspectRatio::parse_str_to_parse_error("defer xMinYMax slice"),
Ok(AspectRatio {
defer: true,
align: Some(Align {
@@ -343,75 +338,75 @@ mod tests {
fn aligns() {
let viewbox = ViewBox(Rect::from_size(1.0, 10.0));
- let foo = AspectRatio::parse_str("xMinYMin meet").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMinYMin meet").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::from_size(0.1, 1.0));
- let foo = AspectRatio::parse_str("xMinYMin slice").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMinYMin slice").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::from_size(10.0, 100.0));
- let foo = AspectRatio::parse_str("xMinYMid meet").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMinYMid meet").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::from_size(0.1, 1.0));
- let foo = AspectRatio::parse_str("xMinYMid slice").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMinYMid slice").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(0.0, -49.5, 10.0, 100.0 - 49.5));
- let foo = AspectRatio::parse_str("xMinYMax meet").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMinYMax meet").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::from_size(0.1, 1.0));
- let foo = AspectRatio::parse_str("xMinYMax slice").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMinYMax slice").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(0.0, -99.0, 10.0, 1.0));
- let foo = AspectRatio::parse_str("xMidYMin meet").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMidYMin meet").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(4.95, 0.0, 4.95 + 0.1, 1.0));
- let foo = AspectRatio::parse_str("xMidYMin slice").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMidYMin slice").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::from_size(10.0, 100.0));
- let foo = AspectRatio::parse_str("xMidYMid meet").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMidYMid meet").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(4.95, 0.0, 4.95 + 0.1, 1.0));
- let foo = AspectRatio::parse_str("xMidYMid slice").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMidYMid slice").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(0.0, -49.5, 10.0, 100.0 - 49.5));
- let foo = AspectRatio::parse_str("xMidYMax meet").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMidYMax meet").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(4.95, 0.0, 4.95 + 0.1, 1.0));
- let foo = AspectRatio::parse_str("xMidYMax slice").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMidYMax slice").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(0.0, -99.0, 10.0, 1.0));
- let foo = AspectRatio::parse_str("xMaxYMin meet").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMaxYMin meet").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(9.9, 0.0, 10.0, 1.0));
- let foo = AspectRatio::parse_str("xMaxYMin slice").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMaxYMin slice").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::from_size(10.0, 100.0));
- let foo = AspectRatio::parse_str("xMaxYMid meet").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMaxYMid meet").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(9.9, 0.0, 10.0, 1.0));
- let foo = AspectRatio::parse_str("xMaxYMid slice").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMaxYMid slice").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(0.0, -49.5, 10.0, 100.0 - 49.5));
- let foo = AspectRatio::parse_str("xMaxYMax meet").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMaxYMax meet").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(9.9, 0.0, 10.0, 1.0));
- let foo = AspectRatio::parse_str("xMaxYMax slice").unwrap();
+ let foo = AspectRatio::parse_str_to_parse_error("xMaxYMax slice").unwrap();
let foo = foo.compute(&viewbox, &Rect::from_size(10.0, 1.0));
assert_rect_equal(&foo, &Rect::new(0.0, -99.0, 10.0, 1.0));
}
diff --git a/rsvg_internals/src/filters/image.rs b/rsvg_internals/src/filters/image.rs
index 06e24df4..af5ff42d 100644
--- a/rsvg_internals/src/filters/image.rs
+++ b/rsvg_internals/src/filters/image.rs
@@ -6,7 +6,7 @@ use crate::aspect_ratio::AspectRatio;
use crate::drawing_ctx::DrawingCtx;
use crate::error::*;
use crate::node::{CascadedValues, NodeResult, NodeTrait, RsvgNode};
-use crate::parsers::ParseValue;
+use crate::parsers::ParseValueToParseError;
use crate::property_bag::PropertyBag;
use crate::rect::Rect;
use crate::surface_utils::shared_surface::{SharedImageSurface, SurfaceType};
@@ -162,7 +162,7 @@ impl NodeTrait for FeImage {
for (attr, value) in pbag.iter() {
match attr.expanded() {
- expanded_name!(svg "preserveAspectRatio") => self.aspect = attr.parse(value)?,
+ expanded_name!(svg "preserveAspectRatio") => self.aspect = attr.parse_to_parse_error(value)?,
// "path" is used by some older Adobe Illustrator versions
expanded_name!(xlink "href") | expanded_name!(svg "path") => {
diff --git a/rsvg_internals/src/image.rs b/rsvg_internals/src/image.rs
index c4c846db..b16230d3 100644
--- a/rsvg_internals/src/image.rs
+++ b/rsvg_internals/src/image.rs
@@ -11,7 +11,7 @@ use crate::error::*;
use crate::float_eq_cairo::ApproxEqCairo;
use crate::length::*;
use crate::node::*;
-use crate::parsers::{ParseValue, ParseValueToParseError};
+use crate::parsers::ParseValueToParseError;
use crate::property_bag::PropertyBag;
use crate::rect::Rect;
use crate::viewbox::ViewBox;
@@ -38,7 +38,7 @@ impl NodeTrait for Image {
expanded_name!(svg "height") => {
self.h = attr.parse_to_parse_error_and_validate(value, Length::check_nonnegative)?
}
- expanded_name!(svg "preserveAspectRatio") => self.aspect = attr.parse(value)?,
+ expanded_name!(svg "preserveAspectRatio") => self.aspect = attr.parse_to_parse_error(value)?,
// "path" is used by some older Adobe Illustrator versions
expanded_name!(xlink "href") | expanded_name!(svg "path") => {
diff --git a/rsvg_internals/src/marker.rs b/rsvg_internals/src/marker.rs
index a327b281..3423e50d 100644
--- a/rsvg_internals/src/marker.rs
+++ b/rsvg_internals/src/marker.rs
@@ -16,7 +16,7 @@ use crate::float_eq_cairo::ApproxEqCairo;
use crate::iri::IRI;
use crate::length::*;
use crate::node::*;
-use crate::parsers::{Parse, ParseToParseError, ParseValue, ParseValueToParseError};
+use crate::parsers::{ParseToParseError, ParseValueToParseError};
use crate::path_builder::*;
use crate::properties::{ComputedValues, SpecifiedValue, SpecifiedValues};
use crate::property_bag::PropertyBag;
@@ -36,14 +36,13 @@ impl Default for MarkerUnits {
}
}
-impl Parse for MarkerUnits {
- fn parse(parser: &mut Parser<'_, '_>) -> Result<MarkerUnits, ValueErrorKind> {
- parse_identifiers!(
+impl ParseToParseError for MarkerUnits {
+ fn parse_to_parse_error<'i>(parser: &mut Parser<'i, '_>) -> Result<MarkerUnits, CssParseError<'i>> {
+ Ok(parse_identifiers!(
parser,
"userSpaceOnUse" => MarkerUnits::UserSpaceOnUse,
"strokeWidth" => MarkerUnits::StrokeWidth,
- )
- .map_err(|_| ValueErrorKind::parse_error("parse error"))
+ )?)
}
}
@@ -184,7 +183,7 @@ impl NodeTrait for Marker {
fn set_atts(&mut self, _: Option<&RsvgNode>, pbag: &PropertyBag<'_>) -> NodeResult {
for (attr, value) in pbag.iter() {
match attr.expanded() {
- expanded_name!(svg "markerUnits") => self.units = attr.parse(value)?,
+ expanded_name!(svg "markerUnits") => self.units = attr.parse_to_parse_error(value)?,
expanded_name!(svg "refX") => self.ref_x = attr.parse_to_parse_error(value)?,
expanded_name!(svg "refY") => self.ref_y = attr.parse_to_parse_error(value)?,
expanded_name!(svg "markerWidth") => {
@@ -200,7 +199,7 @@ impl NodeTrait for Marker {
)?
}
expanded_name!(svg "orient") => self.orient = attr.parse_to_parse_error(value)?,
- expanded_name!(svg "preserveAspectRatio") => self.aspect = attr.parse(value)?,
+ expanded_name!(svg "preserveAspectRatio") => self.aspect = attr.parse_to_parse_error(value)?,
expanded_name!(svg "viewBox") => {
self.vbox = Some(attr.parse_to_parse_error(value)?)
}
@@ -813,18 +812,18 @@ mod parser_tests {
#[test]
fn parsing_invalid_marker_units_yields_error() {
- assert!(MarkerUnits::parse_str("").is_err());
- assert!(MarkerUnits::parse_str("foo").is_err());
+ assert!(MarkerUnits::parse_str_to_parse_error("").is_err());
+ assert!(MarkerUnits::parse_str_to_parse_error("foo").is_err());
}
#[test]
fn parses_marker_units() {
assert_eq!(
- MarkerUnits::parse_str("userSpaceOnUse"),
+ MarkerUnits::parse_str_to_parse_error("userSpaceOnUse"),
Ok(MarkerUnits::UserSpaceOnUse)
);
assert_eq!(
- MarkerUnits::parse_str("strokeWidth"),
+ MarkerUnits::parse_str_to_parse_error("strokeWidth"),
Ok(MarkerUnits::StrokeWidth)
);
}
diff --git a/rsvg_internals/src/pattern.rs b/rsvg_internals/src/pattern.rs
index 658cb740..488023ce 100644
--- a/rsvg_internals/src/pattern.rs
+++ b/rsvg_internals/src/pattern.rs
@@ -15,7 +15,7 @@ use crate::float_eq_cairo::ApproxEqCairo;
use crate::length::*;
use crate::node::*;
use crate::paint_server::{AsPaintSource, PaintSource};
-use crate::parsers::{ParseValue, ParseValueToParseError};
+use crate::parsers::ParseValueToParseError;
use crate::properties::ComputedValues;
use crate::property_bag::PropertyBag;
use crate::rect::Rect;
@@ -126,7 +126,7 @@ impl NodeTrait for Pattern {
}
expanded_name!(svg "viewBox") => self.common.vbox =
Some(Some(attr.parse_to_parse_error(value)?)),
expanded_name!(svg "preserveAspectRatio") => {
- self.common.preserve_aspect_ratio = Some(attr.parse(value)?)
+ self.common.preserve_aspect_ratio = Some(attr.parse_to_parse_error(value)?)
}
expanded_name!(svg "patternTransform") => {
self.common.affine = Some(attr.parse_to_parse_error(value)?)
diff --git a/rsvg_internals/src/structure.rs b/rsvg_internals/src/structure.rs
index a43cfb1e..9097b418 100644
--- a/rsvg_internals/src/structure.rs
+++ b/rsvg_internals/src/structure.rs
@@ -11,7 +11,7 @@ use crate::error::*;
use crate::float_eq_cairo::ApproxEqCairo;
use crate::length::*;
use crate::node::*;
-use crate::parsers::{ParseToParseError, ParseValue, ParseValueToParseError};
+use crate::parsers::{ParseToParseError, ParseValueToParseError};
use crate::properties::ComputedValues;
use crate::property_bag::PropertyBag;
use crate::property_defs::Overflow;
@@ -184,7 +184,7 @@ impl NodeTrait for Svg {
for (attr, value) in pbag.iter() {
match attr.expanded() {
expanded_name!(svg "preserveAspectRatio") => {
- self.preserve_aspect_ratio = attr.parse(value)?
+ self.preserve_aspect_ratio = attr.parse_to_parse_error(value)?
}
expanded_name!(svg "x") if is_inner_svg => self.x = Some(attr.parse_to_parse_error(value)?),
expanded_name!(svg "y") if is_inner_svg => self.y = Some(attr.parse_to_parse_error(value)?),
@@ -427,7 +427,7 @@ impl NodeTrait for Symbol {
for (attr, value) in pbag.iter() {
match attr.expanded() {
expanded_name!(svg "preserveAspectRatio") => {
- self.preserve_aspect_ratio = attr.parse(value)?
+ self.preserve_aspect_ratio = attr.parse_to_parse_error(value)?
}
expanded_name!(svg "viewBox") => self.vbox = attr.parse_to_parse_error(value).map(Some)?,
_ => (),
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]