[librsvg: 9/43] Convert FontWeightSpec / FontWeight to CssParseError
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 9/43] Convert FontWeightSpec / FontWeight to CssParseError
- Date: Sat, 21 Dec 2019 02:30:15 +0000 (UTC)
commit 4d8f8910126d9260f7690a877049ddb5e5261b0c
Author: Federico Mena Quintero <federico gnome org>
Date: Thu Dec 19 20:31:39 2019 -0600
Convert FontWeightSpec / FontWeight to CssParseError
rsvg_internals/src/font_props.rs | 32 ++++++++++++++++----------------
rsvg_internals/src/properties.rs | 2 +-
rsvg_internals/src/property_defs.rs | 2 +-
3 files changed, 18 insertions(+), 18 deletions(-)
---
diff --git a/rsvg_internals/src/font_props.rs b/rsvg_internals/src/font_props.rs
index bf2d7a7b..165817ce 100644
--- a/rsvg_internals/src/font_props.rs
+++ b/rsvg_internals/src/font_props.rs
@@ -1,6 +1,6 @@
//! CSS font properties.
-use cssparser::Parser;
+use cssparser::{BasicParseError, Parser};
use crate::drawing_ctx::ViewParams;
use crate::error::*;
@@ -102,23 +102,23 @@ pub enum FontWeightSpec {
W900,
}
-impl Parse for FontWeightSpec {
- fn parse(parser: &mut Parser<'_, '_>) -> Result<FontWeightSpec, ValueErrorKind> {
+impl ParseToParseError for FontWeightSpec {
+ fn parse_to_parse_error<'i>(parser: &mut Parser<'i, '_>) -> Result<FontWeightSpec, CssParseError<'i>> {
parser
.try_parse(|p| {
- parse_identifiers!(
+ Ok(parse_identifiers!(
p,
"normal" => FontWeightSpec::Normal,
"bold" => FontWeightSpec::Bold,
"bolder" => FontWeightSpec::Bolder,
"lighter" => FontWeightSpec::Lighter,
- )
- .map_err(|_| ValueErrorKind::parse_error("parse error"))
+ )?)
})
- .or_else(|_| {
+ .or_else(|_: CssParseError| {
+ let loc = parser.current_source_location();
parser
.expect_integer()
- .map_err(|_| ValueErrorKind::parse_error("parse error"))
+ .map_err(|e: BasicParseError| e.into())
.and_then(|i| match i {
100 => Ok(FontWeightSpec::W100),
200 => Ok(FontWeightSpec::W200),
@@ -129,7 +129,7 @@ impl Parse for FontWeightSpec {
700 => Ok(FontWeightSpec::W700),
800 => Ok(FontWeightSpec::W800),
900 => Ok(FontWeightSpec::W900),
- _ => Err(ValueErrorKind::parse_error("parse error")),
+ _ => Err(loc.new_custom_error(ValueErrorKind::parse_error("parse error")))
})
})
}
@@ -225,25 +225,25 @@ mod tests {
#[test]
fn parses_font_weight() {
assert_eq!(
- <FontWeightSpec as Parse>::parse_str("normal"),
+ <FontWeightSpec as ParseToParseError>::parse_str_to_parse_error("normal"),
Ok(FontWeightSpec::Normal)
);
assert_eq!(
- <FontWeightSpec as Parse>::parse_str("bold"),
+ <FontWeightSpec as ParseToParseError>::parse_str_to_parse_error("bold"),
Ok(FontWeightSpec::Bold)
);
assert_eq!(
- <FontWeightSpec as Parse>::parse_str("100"),
+ <FontWeightSpec as ParseToParseError>::parse_str_to_parse_error("100"),
Ok(FontWeightSpec::W100)
);
}
#[test]
fn detects_invalid_font_weight() {
- assert!(<FontWeightSpec as Parse>::parse_str("").is_err());
- assert!(<FontWeightSpec as Parse>::parse_str("strange").is_err());
- assert!(<FontWeightSpec as Parse>::parse_str("314").is_err());
- assert!(<FontWeightSpec as Parse>::parse_str("3.14").is_err());
+ assert!(<FontWeightSpec as ParseToParseError>::parse_str_to_parse_error("").is_err());
+ assert!(<FontWeightSpec as ParseToParseError>::parse_str_to_parse_error("strange").is_err());
+ assert!(<FontWeightSpec as ParseToParseError>::parse_str_to_parse_error("314").is_err());
+ assert!(<FontWeightSpec as ParseToParseError>::parse_str_to_parse_error("3.14").is_err());
}
#[test]
diff --git a/rsvg_internals/src/properties.rs b/rsvg_internals/src/properties.rs
index e98b5e62..58340a1f 100644
--- a/rsvg_internals/src/properties.rs
+++ b/rsvg_internals/src/properties.rs
@@ -288,7 +288,7 @@ pub fn parse_property<'i>(prop_name: &QualName, input: &mut Parser<'i, '_>, acce
Ok(ParsedProperty::FontVariant(parse_input_to_parse_error(input)?)),
expanded_name!(svg "font-weight") =>
- Ok(ParsedProperty::FontWeight(parse_input(input)?)),
+ Ok(ParsedProperty::FontWeight(parse_input_to_parse_error(input)?)),
expanded_name!(svg "letter-spacing") =>
Ok(ParsedProperty::LetterSpacing(parse_input_to_parse_error(input)?)),
diff --git a/rsvg_internals/src/property_defs.rs b/rsvg_internals/src/property_defs.rs
index 522bf6ca..c32a47f6 100644
--- a/rsvg_internals/src/property_defs.rs
+++ b/rsvg_internals/src/property_defs.rs
@@ -297,7 +297,7 @@ make_property!(
FontWeight,
default: FontWeightSpec::Normal,
inherits_automatically: true,
- newtype_parse: FontWeightSpec,
+ newtype_parse_to_parse_error: FontWeightSpec,
);
// https://www.w3.org/TR/SVG/text.html#LetterSpacingProperty
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]