[librsvg: 27/51] aspect_ratio: use parse_identifiers!
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 27/51] aspect_ratio: use parse_identifiers!
- Date: Thu, 19 Dec 2019 01:50:17 +0000 (UTC)
commit 7d9973a64148505d345e5850491074f3682dfa1a
Author: Federico Mena Quintero <federico gnome org>
Date: Tue Dec 17 19:39:12 2019 -0600
aspect_ratio: use parse_identifiers!
rsvg_internals/src/aspect_ratio.rs | 61 +++++++++++++++++---------------------
1 file changed, 27 insertions(+), 34 deletions(-)
---
diff --git a/rsvg_internals/src/aspect_ratio.rs b/rsvg_internals/src/aspect_ratio.rs
index 3cef833d..cf67f173 100644
--- a/rsvg_internals/src/aspect_ratio.rs
+++ b/rsvg_internals/src/aspect_ratio.rs
@@ -24,11 +24,11 @@ use std::ops::Deref;
use cairo;
-use crate::error::ValueErrorKind;
+use crate::error::*;
use crate::parsers::Parse;
use crate::rect::Rect;
use crate::viewbox::ViewBox;
-use cssparser::{CowRcStr, Parser};
+use cssparser::Parser;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct AspectRatio {
@@ -191,34 +191,34 @@ impl AspectRatio {
}
}
-fn parse_align_xy(ident: &CowRcStr) -> Result<Option<(X, Y)>, ValueErrorKind> {
+fn parse_align_xy<'i>(parser: &mut Parser<'i, '_>) -> Result<Option<(X, Y)>, ParseError<'i>> {
use self::Align1D::*;
- match ident.as_ref() {
- "none" => Ok(None),
+ parse_identifiers!(
+ parser,
- "xMinYMin" => Ok(Some((X(Min), Y(Min)))),
- "xMidYMin" => Ok(Some((X(Mid), Y(Min)))),
- "xMaxYMin" => Ok(Some((X(Max), Y(Min)))),
+ "none" => None,
- "xMinYMid" => Ok(Some((X(Min), Y(Mid)))),
- "xMidYMid" => Ok(Some((X(Mid), Y(Mid)))),
- "xMaxYMid" => Ok(Some((X(Max), Y(Mid)))),
+ "xMinYMin" => Some((X(Min), Y(Min))),
+ "xMidYMin" => Some((X(Mid), Y(Min))),
+ "xMaxYMin" => Some((X(Max), Y(Min))),
- "xMinYMax" => Ok(Some((X(Min), Y(Max)))),
- "xMidYMax" => Ok(Some((X(Mid), Y(Max)))),
- "xMaxYMax" => Ok(Some((X(Max), Y(Max)))),
+ "xMinYMid" => Some((X(Min), Y(Mid))),
+ "xMidYMid" => Some((X(Mid), Y(Mid))),
+ "xMaxYMid" => Some((X(Max), Y(Mid))),
- _ => Err(ValueErrorKind::parse_error("invalid alignment")),
- }
+ "xMinYMax" => Some((X(Min), Y(Max))),
+ "xMidYMax" => Some((X(Mid), Y(Max))),
+ "xMaxYMax" => Some((X(Max), Y(Max))),
+ )
}
-fn parse_fit_mode(s: &str) -> Result<FitMode, ValueErrorKind> {
- match s {
- "meet" => Ok(FitMode::Meet),
- "slice" => Ok(FitMode::Slice),
- _ => Err(ValueErrorKind::parse_error("invalid fit mode")),
- }
+fn parse_fit_mode<'i>(parser: &mut Parser<'i, '_>) -> Result<FitMode, ParseError<'i>> {
+ parse_identifiers!(
+ parser,
+ "meet" => FitMode::Meet,
+ "slice" => FitMode::Slice,
+ )
}
impl Parse for AspectRatio {
@@ -227,18 +227,13 @@ impl Parse for AspectRatio {
.try_parse(|p| p.expect_ident_matching("defer"))
.is_ok();
- let align_xy = parser.try_parse(|p| {
- p.expect_ident()
- .map_err(|_| ValueErrorKind::parse_error("expected identifier"))
- .and_then(|ident| parse_align_xy(ident))
- })?;
+ let align_xy = parser
+ .try_parse(|p| parse_align_xy(p))
+ .map_err(|_e| ValueErrorKind::parse_error("parse error"))?;
let fit = parser
- .try_parse(|p| {
- p.expect_ident()
- .map_err(|_| ValueErrorKind::parse_error("expected identifier"))
- .and_then(|ident| parse_fit_mode(ident))
- })
+ .try_parse(|p| parse_fit_mode(p))
+ .map_err(|_e| ValueErrorKind::parse_error("parse error"))
.unwrap_or_default();
parser
@@ -261,9 +256,7 @@ mod tests {
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").is_err());
assert!(AspectRatio::parse_str("defer xMidYMid foo").is_err());
- assert!(AspectRatio::parse_str("xmidymid").is_err());
assert!(AspectRatio::parse_str("xMidYMid foo").is_err());
assert!(AspectRatio::parse_str("defer xMidYMid meet foo").is_err());
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]