[librsvg: 1/2] dasharray: split to its own module
- From: Federico Mena Quintero <federico src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [librsvg: 1/2] dasharray: split to its own module
- Date: Mon, 25 Nov 2019 02:51:40 +0000 (UTC)
commit 28fde5d4ae085a9f6ff684adce72b2962c1abb5b
Author: Paolo Borelli <pborelli gnome org>
Date: Sun Nov 24 22:08:05 2019 +0100
dasharray: split to its own module
There is no reason for it to be in length.rs
Makefile.am | 1 +
rsvg_internals/src/dasharray.rs | 118 ++++++++++++++++++++++++++++++++++++
rsvg_internals/src/drawing_ctx.rs | 2 +-
rsvg_internals/src/length.rs | 114 ----------------------------------
rsvg_internals/src/lib.rs | 1 +
rsvg_internals/src/property_defs.rs | 1 +
6 files changed, 122 insertions(+), 115 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 299407e3..4c93fba3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -41,6 +41,7 @@ LIBRSVG_INTERNALS_SRC = \
rsvg_internals/src/create_node.rs \
rsvg_internals/src/css.rs \
rsvg_internals/src/error.rs \
+ rsvg_internals/src/dasharray.rs \
rsvg_internals/src/document.rs \
rsvg_internals/src/dpi.rs \
rsvg_internals/src/drawing_ctx.rs \
diff --git a/rsvg_internals/src/dasharray.rs b/rsvg_internals/src/dasharray.rs
new file mode 100644
index 00000000..6e55e889
--- /dev/null
+++ b/rsvg_internals/src/dasharray.rs
@@ -0,0 +1,118 @@
+use cssparser::Parser;
+
+use crate::error::*;
+use crate::length::*;
+use crate::parsers::{CssParserExt, Parse};
+
+#[derive(Debug, PartialEq, Clone)]
+pub enum Dasharray {
+ None,
+ Array(Vec<Length<Both>>),
+}
+
+impl Default for Dasharray {
+ fn default() -> Dasharray {
+ Dasharray::None
+ }
+}
+
+impl Parse for Dasharray {
+ type Err = ValueErrorKind;
+
+ fn parse(parser: &mut Parser<'_, '_>) -> Result<Dasharray, ValueErrorKind> {
+ if parser
+ .try_parse(|p| p.expect_ident_matching("none"))
+ .is_ok()
+ {
+ return Ok(Dasharray::None);
+ }
+
+ let mut dasharray = Vec::new();
+
+ loop {
+ let d = Length::<Both>::parse(parser).and_then(Length::<Both>::check_nonnegative)?;
+ dasharray.push(d);
+
+ if parser.is_exhausted() {
+ break;
+ }
+
+ parser.optional_comma();
+ }
+
+ Ok(Dasharray::Array(dasharray))
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn parses_dash_array() {
+ // helper to cut down boilderplate
+ let length_parse = |s| Length::<Both>::parse_str(s).unwrap();
+
+ let expected = Dasharray::Array(vec![
+ length_parse("1"),
+ length_parse("2in"),
+ length_parse("3"),
+ length_parse("4%"),
+ ]);
+
+ let sample_1 = Dasharray::Array(vec![length_parse("10"), length_parse("6")]);
+
+ let sample_2 = Dasharray::Array(vec![
+ length_parse("5"),
+ length_parse("5"),
+ length_parse("20"),
+ ]);
+
+ let sample_3 = Dasharray::Array(vec![
+ length_parse("10px"),
+ length_parse("20px"),
+ length_parse("20px"),
+ ]);
+
+ let sample_4 = Dasharray::Array(vec![
+ length_parse("25"),
+ length_parse("5"),
+ length_parse("5"),
+ length_parse("5"),
+ ]);
+
+ let sample_5 = Dasharray::Array(vec![length_parse("3.1415926"), length_parse("8")]);
+ let sample_6 = Dasharray::Array(vec![length_parse("5"), length_parse("3.14")]);
+ let sample_7 = Dasharray::Array(vec![length_parse("2")]);
+
+ assert_eq!(Dasharray::parse_str("none").unwrap(), Dasharray::None);
+ assert_eq!(Dasharray::parse_str("1 2in,3 4%").unwrap(), expected);
+ assert_eq!(Dasharray::parse_str("10,6").unwrap(), sample_1);
+ assert_eq!(Dasharray::parse_str("5,5,20").unwrap(), sample_2);
+ assert_eq!(Dasharray::parse_str("10px 20px 20px").unwrap(), sample_3);
+ assert_eq!(Dasharray::parse_str("25 5 , 5 5").unwrap(), sample_4);
+ assert_eq!(Dasharray::parse_str("3.1415926,8").unwrap(), sample_5);
+ assert_eq!(Dasharray::parse_str("5, 3.14").unwrap(), sample_6);
+ assert_eq!(Dasharray::parse_str("2").unwrap(), sample_7);
+
+ // Negative numbers
+ assert_eq!(
+ Dasharray::parse_str("20,40,-20"),
+ Err(ValueErrorKind::Value(String::from(
+ "value must be non-negative"
+ )))
+ );
+
+ // Empty dash_array
+ assert!(Dasharray::parse_str("").is_err());
+ assert!(Dasharray::parse_str("\t \n ").is_err());
+ assert!(Dasharray::parse_str(",,,").is_err());
+ assert!(Dasharray::parse_str("10, \t, 20 \n").is_err());
+
+ // No trailing commas allowed, parse error
+ assert!(Dasharray::parse_str("10,").is_err());
+
+ // A comma should be followed by a number
+ assert!(Dasharray::parse_str("20,,10").is_err());
+ }
+}
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
index 991d9363..29e805f1 100644
--- a/rsvg_internals/src/drawing_ctx.rs
+++ b/rsvg_internals/src/drawing_ctx.rs
@@ -8,12 +8,12 @@ use crate::aspect_ratio::AspectRatio;
use crate::bbox::BoundingBox;
use crate::clip_path::{ClipPath, ClipPathUnits};
use crate::coord_units::CoordUnits;
+use crate::dasharray::Dasharray;
use crate::document::Document;
use crate::dpi::Dpi;
use crate::error::{AcquireError, RenderingError};
use crate::filters;
use crate::gradient::{LinearGradient, RadialGradient};
-use crate::length::Dasharray;
use crate::limits;
use crate::mask::Mask;
use crate::node::{CascadedValues, NodeDraw, NodeType, RsvgNode};
diff --git a/rsvg_internals/src/length.rs b/rsvg_internals/src/length.rs
index 144194af..0ba865d9 100644
--- a/rsvg_internals/src/length.rs
+++ b/rsvg_internals/src/length.rs
@@ -398,50 +398,6 @@ fn viewport_percentage(x: f64, y: f64) -> f64 {
(x * x + y * y).sqrt() / SQRT_2
}
-#[derive(Debug, PartialEq, Clone)]
-pub enum Dasharray {
- None,
- Array(Vec<Length<Both>>),
-}
-
-impl Default for Dasharray {
- fn default() -> Dasharray {
- Dasharray::None
- }
-}
-
-impl Parse for Dasharray {
- type Err = ValueErrorKind;
-
- fn parse(parser: &mut Parser<'_, '_>) -> Result<Dasharray, ValueErrorKind> {
- if parser
- .try_parse(|p| p.expect_ident_matching("none"))
- .is_ok()
- {
- Ok(Dasharray::None)
- } else {
- Ok(Dasharray::Array(parse_dash_array(parser)?))
- }
- }
-}
-
-// This does not handle "inherit" or "none" state, the caller is responsible for that.
-fn parse_dash_array(parser: &mut Parser<'_, '_>) -> Result<Vec<Length<Both>>, ValueErrorKind> {
- let mut dasharray = Vec::new();
-
- loop {
- dasharray.push(Length::<Both>::parse(parser).and_then(Length::<Both>::check_nonnegative)?);
-
- if parser.is_exhausted() {
- break;
- } else if parser.try_parse(|p| p.expect_comma()).is_ok() {
- continue;
- }
- }
-
- Ok(dasharray)
-}
-
#[cfg(test)]
mod tests {
use super::*;
@@ -616,74 +572,4 @@ mod tests {
6.0
);
}
-
- fn parse_dash_array_str(s: &str) -> Result<Dasharray, ValueErrorKind> {
- Dasharray::parse_str(s)
- }
-
- #[test]
- fn parses_dash_array() {
- // helper to cut down boilderplate
- let length_parse = |s| Length::<Both>::parse_str(s).unwrap();
-
- let expected = Dasharray::Array(vec![
- length_parse("1"),
- length_parse("2in"),
- length_parse("3"),
- length_parse("4%"),
- ]);
-
- let sample_1 = Dasharray::Array(vec![length_parse("10"), length_parse("6")]);
-
- let sample_2 = Dasharray::Array(vec![
- length_parse("5"),
- length_parse("5"),
- length_parse("20"),
- ]);
-
- let sample_3 = Dasharray::Array(vec![
- length_parse("10px"),
- length_parse("20px"),
- length_parse("20px"),
- ]);
-
- let sample_4 = Dasharray::Array(vec![
- length_parse("25"),
- length_parse("5"),
- length_parse("5"),
- length_parse("5"),
- ]);
-
- let sample_5 = Dasharray::Array(vec![length_parse("3.1415926"), length_parse("8")]);
- let sample_6 = Dasharray::Array(vec![length_parse("5"), length_parse("3.14")]);
- let sample_7 = Dasharray::Array(vec![length_parse("2")]);
-
- assert_eq!(parse_dash_array_str("none").unwrap(), Dasharray::None);
- assert_eq!(parse_dash_array_str("1 2in,3 4%").unwrap(), expected);
- assert_eq!(parse_dash_array_str("10,6").unwrap(), sample_1);
- assert_eq!(parse_dash_array_str("5,5,20").unwrap(), sample_2);
- assert_eq!(parse_dash_array_str("10px 20px 20px").unwrap(), sample_3);
- assert_eq!(parse_dash_array_str("25 5 , 5 5").unwrap(), sample_4);
- assert_eq!(parse_dash_array_str("3.1415926,8").unwrap(), sample_5);
- assert_eq!(parse_dash_array_str("5, 3.14").unwrap(), sample_6);
- assert_eq!(parse_dash_array_str("2").unwrap(), sample_7);
-
- // Negative numbers
- assert_eq!(
- parse_dash_array_str("20,40,-20"),
- Err(ValueErrorKind::Value(String::from(
- "value must be non-negative"
- )))
- );
-
- // Empty dash_array
- assert!(parse_dash_array_str("").is_err());
- assert!(parse_dash_array_str("\t \n ").is_err());
- assert!(parse_dash_array_str(",,,").is_err());
- assert!(parse_dash_array_str("10, \t, 20 \n").is_err());
- // No trailing commas allowed, parse error
- assert!(parse_dash_array_str("10,").is_err());
- // A comma should be followed by a number
- assert!(parse_dash_array_str("20,,10").is_err());
- }
}
diff --git a/rsvg_internals/src/lib.rs b/rsvg_internals/src/lib.rs
index 87f930cc..2872782f 100644
--- a/rsvg_internals/src/lib.rs
+++ b/rsvg_internals/src/lib.rs
@@ -89,6 +89,7 @@ mod color;
mod cond;
mod create_node;
mod css;
+mod dasharray;
mod document;
mod dpi;
mod drawing_ctx;
diff --git a/rsvg_internals/src/property_defs.rs b/rsvg_internals/src/property_defs.rs
index 70a415da..7df5c0aa 100644
--- a/rsvg_internals/src/property_defs.rs
+++ b/rsvg_internals/src/property_defs.rs
@@ -2,6 +2,7 @@ use cssparser::{self, Parser, Token};
use crate::error::*;
use crate::font_props::{FontSizeSpec, FontWeightSpec, LetterSpacingSpec, SingleFontFamily};
+use crate::dasharray::Dasharray;
use crate::iri::IRI;
use crate::length::*;
use crate::paint_server::PaintServer;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]