[librsvg] parsers::angle_degrees(): Implement with rust-cssparser instead of nom
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg] parsers::angle_degrees(): Implement with rust-cssparser instead of nom
- Date: Thu, 20 Jul 2017 00:28:44 +0000 (UTC)
commit b1dd396128ff6e6e7896cbb9a3f23a574c4f689e
Author: Federico Mena Quintero <federico gnome org>
Date: Wed Jul 19 16:45:18 2017 -0500
parsers::angle_degrees(): Implement with rust-cssparser instead of nom
rust/src/parsers.rs | 48 +++++++++++++++++-------------------------------
1 files changed, 17 insertions(+), 31 deletions(-)
---
diff --git a/rust/src/parsers.rs b/rust/src/parsers.rs
index e3f9b46..0b911cc 100644
--- a/rust/src/parsers.rs
+++ b/rust/src/parsers.rs
@@ -1,8 +1,8 @@
use ::libc;
-use ::cssparser::{Parser, Token, BasicParseError};
+use ::cssparser::{Parser, Token, BasicParseError, NumericValue};
use ::glib::translate::*;
use ::glib_sys;
-use ::nom::{IResult, double, is_alphabetic};
+use ::nom::{IResult, double};
use std::f64::consts::*;
use std::mem;
@@ -70,36 +70,31 @@ named! (comma_wsp,
//
// Returns an f64 angle in degrees
-fn is_alphabetic_or_dash (c: u8) -> bool {
- is_alphabetic (c) || c == '-' as u8 || c == '%' as u8
-}
+pub fn angle_degrees (s: &str) -> Result <f64, ParseError> {
+ let mut parser = Parser::new (s);
-named! (parse_number_and_units<(f64, &[u8])>,
- tuple! (double,
- take_while! (is_alphabetic_or_dash)));
+ let token = parser.next ()
+ .map_err (|_| ParseError::new ("expected angle"))?;
-pub fn number_and_units (s: &str) -> Result <(f64, &str), ParseError> {
- parse_number_and_units (s.as_bytes ()).to_full_result ()
- .map (|(v, slice)| (v, str::from_utf8 (slice).unwrap ()))
- .map_err (|_| ParseError::new ("expected number and symbol"))
-}
+ match token {
+ Token::Number (NumericValue { value, .. }) => Ok (value as f64),
-pub fn angle_degrees (s: &str) -> Result <f64, ParseError> {
- let r = number_and_units (s);
+ Token::Dimension (NumericValue { value, .. }, cow) => {
+ let value = value as f64;
- match r {
- Ok ((value, unit)) => {
- match unit {
+ match cow.as_ref () {
"deg" => Ok (value),
"grad" => Ok (value * 360.0 / 400.0),
"rad" => Ok (value * 180.0 / PI),
- "" => Ok (value),
- _ => Err (ParseError::new ("expected (\"deg\", \"rad\", \"grad\")? after number"))
+ _ => Err (ParseError::new ("expected angle"))
}
},
- _ => Err (ParseError::new ("expected a number"))
- }
+ _ => Err (ParseError::new ("expected angle"))
+ }.and_then (|r|
+ parser.expect_exhausted ()
+ .map (|_| r)
+ .map_err (|_| ParseError::new ("expected angle")))
}
// Parse a viewBox attribute
@@ -445,15 +440,6 @@ mod tests {
}
#[test]
- fn parses_number_and_units () {
- assert_eq! (number_and_units ("-1"), Ok ((-1.0, "")));
- assert_eq! (number_and_units ("0x"), Ok ((0.0, "x")));
- assert_eq! (number_and_units ("-55.5x-large"), Ok ((-55.5, "x-large")));
- assert_eq! (number_and_units ("10%"), Ok ((10.0, "%")));
- assert_eq! (number_and_units ("10em"), Ok ((10.0, "em")));
- }
-
- #[test]
fn parses_angle () {
assert_eq! (angle_degrees ("0"), Ok (0.0));
assert_eq! (angle_degrees ("15"), Ok (15.0));
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]