[librsvg: 5/43] TextDecoration: return a CssParseError



commit f5ee9e68715cf0f7a40ed860d7a9ede219aeb55e
Author: Federico Mena Quintero <federico gnome org>
Date:   Thu Dec 19 19:02:09 2019 -0600

    TextDecoration: return a CssParseError
    
    This adds a helper method parse_str_to_parse_error() to the
    ParseToParseError trait.

 rsvg_internals/src/parsers.rs       |  9 +++++++++
 rsvg_internals/src/properties.rs    |  2 +-
 rsvg_internals/src/property_defs.rs | 37 ++++++++++++++++++-------------------
 3 files changed, 28 insertions(+), 20 deletions(-)
---
diff --git a/rsvg_internals/src/parsers.rs b/rsvg_internals/src/parsers.rs
index d8f36ad9..07a87b5a 100644
--- a/rsvg_internals/src/parsers.rs
+++ b/rsvg_internals/src/parsers.rs
@@ -89,6 +89,15 @@ impl Parse for f64 {
 
 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>> {
+        let mut input = ParserInput::new(s);
+        let mut parser = Parser::new(&mut input);
+
+        Self::parse_to_parse_error(&mut parser).and_then(|r| {
+            // FIXME: parser.expect_exhausted()?;
+            Ok(r)
+        })
+    }
 }
 
 impl ParseToParseError for f64 {
diff --git a/rsvg_internals/src/properties.rs b/rsvg_internals/src/properties.rs
index ea5227ef..7eb88d50 100644
--- a/rsvg_internals/src/properties.rs
+++ b/rsvg_internals/src/properties.rs
@@ -359,7 +359,7 @@ pub fn parse_property<'i>(prop_name: &QualName, input: &mut Parser<'i, '_>, acce
             Ok(ParsedProperty::TextAnchor(parse_input(input)?)),
 
         expanded_name!(svg "text-decoration") =>
-            Ok(ParsedProperty::TextDecoration(parse_input(input)?)),
+            Ok(ParsedProperty::TextDecoration(parse_input_to_parse_error(input)?)),
 
         expanded_name!(svg "text-rendering") =>
             Ok(ParsedProperty::TextRendering(parse_input(input)?)),
diff --git a/rsvg_internals/src/property_defs.rs b/rsvg_internals/src/property_defs.rs
index 1a0b63f1..7b1e0693 100644
--- a/rsvg_internals/src/property_defs.rs
+++ b/rsvg_internals/src/property_defs.rs
@@ -1,6 +1,6 @@
 //! Definitions for CSS property types.
 
-use cssparser::{self, Parser};
+use cssparser::{self, Parser, Token};
 
 use crate::dasharray::Dasharray;
 use crate::error::*;
@@ -8,7 +8,7 @@ use crate::font_props::{FontSizeSpec, FontWeightSpec, LetterSpacingSpec, SingleF
 use crate::iri::IRI;
 use crate::length::*;
 use crate::paint_server::PaintServer;
-use crate::parsers::Parse;
+use crate::parsers::{Parse, ParseToParseError};
 use crate::properties::ComputedValues;
 use crate::property_macros::Property;
 use crate::unit_interval::UnitInterval;
@@ -533,8 +533,8 @@ make_property!(
     }
 
     parse_impl: {
-        impl Parse for TextDecoration {
-            fn parse(parser: &mut Parser<'_, '_>) -> Result<TextDecoration, ValueErrorKind> {
+        impl ParseToParseError for TextDecoration {
+            fn parse_to_parse_error<'i>(parser: &mut Parser<'i, '_>) -> Result<TextDecoration, 
CssParseError<'i>> {
                 let mut overline = false;
                 let mut underline = false;
                 let mut strike = false;
@@ -544,15 +544,14 @@ make_property!(
                 }
 
                 while !parser.is_exhausted() {
-                    let cow = parser.expect_ident().map_err(|_| {
-                        crate::error::ValueErrorKind::parse_error("expected identifier")
-                    })?;
-
-                    match cow.as_ref() {
-                        "overline" => overline = true,
-                        "underline" => underline = true,
-                        "line-through" => strike = true,
-                        _ => return Err(ValueErrorKind::parse_error("invalid syntax")),
+                    let loc = parser.current_source_location();
+                    let token = parser.next()?;
+
+                    match token {
+                        Token::Ident(ref cow) if cow.eq_ignore_ascii_case("overline") => overline = true,
+                        Token::Ident(ref cow) if cow.eq_ignore_ascii_case("underline") => underline = true,
+                        Token::Ident(ref cow) if cow.eq_ignore_ascii_case("line-through") => strike = true,
+                        _ => Err(loc.new_basic_unexpected_token_error(token.clone()))?,
                     }
                 }
 
@@ -570,7 +569,7 @@ make_property!(
 #[test]
 fn parses_text_decoration() {
     assert_eq!(
-        TextDecoration::parse_str("none").unwrap(),
+        TextDecoration::parse_str_to_parse_error("none").unwrap(),
         TextDecoration {
             overline: false,
             underline: false,
@@ -579,7 +578,7 @@ fn parses_text_decoration() {
     );
 
     assert_eq!(
-        TextDecoration::parse_str("overline").unwrap(),
+        TextDecoration::parse_str_to_parse_error("overline").unwrap(),
         TextDecoration {
             overline: true,
             underline: false,
@@ -588,7 +587,7 @@ fn parses_text_decoration() {
     );
 
     assert_eq!(
-        TextDecoration::parse_str("underline").unwrap(),
+        TextDecoration::parse_str_to_parse_error("underline").unwrap(),
         TextDecoration {
             overline: false,
             underline: true,
@@ -597,7 +596,7 @@ fn parses_text_decoration() {
     );
 
     assert_eq!(
-        TextDecoration::parse_str("line-through").unwrap(),
+        TextDecoration::parse_str_to_parse_error("line-through").unwrap(),
         TextDecoration {
             overline: false,
             underline: false,
@@ -606,7 +605,7 @@ fn parses_text_decoration() {
     );
 
     assert_eq!(
-        TextDecoration::parse_str("underline overline").unwrap(),
+        TextDecoration::parse_str_to_parse_error("underline overline").unwrap(),
         TextDecoration {
             overline: true,
             underline: true,
@@ -614,7 +613,7 @@ fn parses_text_decoration() {
         }
     );
 
-    assert!(TextDecoration::parse_str("airline").is_err())
+    assert!(TextDecoration::parse_str_to_parse_error("airline").is_err())
 }
 
 // https://www.w3.org/TR/SVG/painting.html#TextRenderingProperty


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