From 4fb9980dd855dec607ff3246177be8a8a3b2a7dc Mon Sep 17 00:00:00 2001 From: Dmitri Lebedev Date: Fri, 15 Sep 2023 16:39:04 +0600 Subject: [PATCH 1/5] created a simpler syntax for macros --- geo-types/src/macros.rs | 71 +++++++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/geo-types/src/macros.rs b/geo-types/src/macros.rs index 491a8e73a..ef20df601 100644 --- a/geo-types/src/macros.rs +++ b/geo-types/src/macros.rs @@ -67,11 +67,23 @@ macro_rules! coord { /// ``` /// /// # Examples +/// +/// Creating a [`LineString`] with WKT-like syntax: +/// +/// ``` +/// use geo_types::{line_string, coord}; +/// let ls = line_string![76.9454 43.2497, 76.9636 43.2308, 77.0591 43.1575, 77.1108 43.1131]; +/// +/// assert_eq!(ls[1], coord! { +/// x: 76.9636, +/// y: 43.2308 +/// }); +/// ``` /// /// Creating a [`LineString`], supplying x/y values: /// /// ``` -/// use geo_types::line_string; +/// use geo_types::{line_string, coord}; /// /// let ls = line_string![ /// (x: -21.95156, y: 64.1446), @@ -80,7 +92,7 @@ macro_rules! coord { /// (x: -21.951445, y: 64.145508), /// ]; /// -/// assert_eq!(ls[1], geo_types::coord! { +/// assert_eq!(ls[1], coord! { /// x: -21.951, /// y: 64.14479 /// }); @@ -89,21 +101,21 @@ macro_rules! coord { /// Creating a [`LineString`], supplying [`Coord`]s: /// /// ``` -/// use geo_types::line_string; +/// use geo_types::{line_string, coord}; /// -/// let coord1 = geo_types::coord! { +/// let coord1 = coord! { /// x: -21.95156, /// y: 64.1446, /// }; -/// let coord2 = geo_types::coord! { +/// let coord2 = coord! { /// x: -21.951, /// y: 64.14479, /// }; -/// let coord3 = geo_types::coord! { +/// let coord3 = coord! { /// x: -21.95044, /// y: 64.14527, /// }; -/// let coord4 = geo_types::coord! { +/// let coord4 = coord! { /// x: -21.951445, /// y: 64.145508, /// }; @@ -112,7 +124,7 @@ macro_rules! coord { /// /// assert_eq!( /// ls[1], -/// geo_types::coord! { +/// coord! { /// x: -21.951, /// y: 64.14479 /// } @@ -124,6 +136,9 @@ macro_rules! coord { #[macro_export] macro_rules! line_string { () => { $crate::LineString::new(vec![]) }; + ($($x:literal $y:literal),+) => { + line_string![$((x: $x, y: $y)),+] + }; ( $(( $($tag:tt : $val:expr),* $(,)? )),* $(,)? @@ -165,12 +180,25 @@ macro_rules! line_string { /// ``` /// /// # Examples -/// +/// +/// Creating a [`Polygon'] with WKT-like syntax: +/// +/// ``` +/// use geo_types::{polygon, coord}; +/// +/// let simple_poly = polygon!(0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0); +/// assert_eq!(simple_poly.exterior()[1], geo_types::coord!{ x: 30.0, y: 0.0 }); +/// assert_eq!(simple_poly.interiors().len(), 0); +/// +/// let poly_with_hole = polygon!([0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0], [10.0 10.0, 20.0 10.0, 20.0 20.0, 10.0 20.0, 10.0 10.0]); +/// assert_eq!(poly_with_hole.interiors()[0][1], geo_types::coord!{ x: 20.0, y: 10.0 }); +/// ``` +/// /// Creating a [`Polygon`] without interior rings, supplying x/y values: /// /// ``` /// use geo_types::polygon; -/// +/// /// let poly = polygon![ /// (x: -111., y: 45.), /// (x: -111., y: 41.), @@ -217,6 +245,19 @@ macro_rules! line_string { #[macro_export] macro_rules! polygon { () => { $crate::Polygon::new($crate::line_string![], vec![]) }; + ($($x:literal $y:literal),+) => { + polygon!($(coord! { x: $x, y: $y }),+) + }; + ([$($xi:literal $yi:literal),+ $(,)?], $([$($xo:literal $yo:literal),+ $(,)?]),*) => { + polygon!( + exterior: [$(coord!(x: $xi, y: $yi)),+], + interiors: [ + $([ + $(coord!(x: $xo, y: $yo),)+ + ]),* + ] + ) + }; ( exterior: [ $(( $($exterior_tag:tt : $exterior_val:expr),* $(,)? )),* @@ -318,6 +359,10 @@ mod test { #[test] fn test_line() { + let ls = line_string![12.345 34.567, 98.765 43.21]; + assert_eq!(ls[0], coord! { x: 12.345, y: 34.567 }); + assert_eq!(ls[1], coord! { x: 98.765, y: 43.21 }); + let ls = line_string![(x: -1.2f32, y: 3.4f32)]; assert_eq!(ls[0], coord! { x: -1.2, y: 3.4 }); @@ -376,5 +421,11 @@ mod test { ); assert_eq!(p.exterior()[0], coord! { x: 1, y: 2 }); assert_eq!(p.interiors()[0][0], coord! { x: 3, y: 4 }); + + let simple_poly = polygon!(0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0); + assert_eq!(simple_poly.exterior()[1], coord!{ x: 30.0, y: 0.0 }); + + let poly_with_hole = polygon!([0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0], [10.0 10.0, 20.0 10.0, 20.0 20.0, 10.0 20.0, 10.0 10.0]); + assert_eq!(poly_with_hole.interiors()[0][1], coord!{ x: 20.0, y: 10.0 }); } } From 2aceed823393043470a875eaab9c42e6c9920798 Mon Sep 17 00:00:00 2001 From: Dmitri Lebedev Date: Fri, 15 Sep 2023 23:06:23 +0600 Subject: [PATCH 2/5] import fixes in wkt-like-syntax macros --- geo-types/src/macros.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/geo-types/src/macros.rs b/geo-types/src/macros.rs index ef20df601..87f00f869 100644 --- a/geo-types/src/macros.rs +++ b/geo-types/src/macros.rs @@ -71,10 +71,10 @@ macro_rules! coord { /// Creating a [`LineString`] with WKT-like syntax: /// /// ``` -/// use geo_types::{line_string, coord}; +/// use geo_types::line_string; /// let ls = line_string![76.9454 43.2497, 76.9636 43.2308, 77.0591 43.1575, 77.1108 43.1131]; /// -/// assert_eq!(ls[1], coord! { +/// assert_eq!(ls[1], geo_types::coord! { /// x: 76.9636, /// y: 43.2308 /// }); @@ -83,7 +83,7 @@ macro_rules! coord { /// Creating a [`LineString`], supplying x/y values: /// /// ``` -/// use geo_types::{line_string, coord}; +/// use geo_types::line_string; /// /// let ls = line_string![ /// (x: -21.95156, y: 64.1446), @@ -92,7 +92,7 @@ macro_rules! coord { /// (x: -21.951445, y: 64.145508), /// ]; /// -/// assert_eq!(ls[1], coord! { +/// assert_eq!(ls[1], geo_types::coord! { /// x: -21.951, /// y: 64.14479 /// }); @@ -101,9 +101,9 @@ macro_rules! coord { /// Creating a [`LineString`], supplying [`Coord`]s: /// /// ``` -/// use geo_types::{line_string, coord}; +/// use geo_types::line_string; /// -/// let coord1 = coord! { +/// let coord1 = geo_types::coord! { /// x: -21.95156, /// y: 64.1446, /// }; @@ -124,7 +124,7 @@ macro_rules! coord { /// /// assert_eq!( /// ls[1], -/// coord! { +/// geo_types::coord! { /// x: -21.951, /// y: 64.14479 /// } @@ -184,7 +184,7 @@ macro_rules! line_string { /// Creating a [`Polygon'] with WKT-like syntax: /// /// ``` -/// use geo_types::{polygon, coord}; +/// use geo_types::polygon; /// /// let simple_poly = polygon!(0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0); /// assert_eq!(simple_poly.exterior()[1], geo_types::coord!{ x: 30.0, y: 0.0 }); @@ -246,14 +246,14 @@ macro_rules! line_string { macro_rules! polygon { () => { $crate::Polygon::new($crate::line_string![], vec![]) }; ($($x:literal $y:literal),+) => { - polygon!($(coord! { x: $x, y: $y }),+) + polygon!($($crate::coord! { x: $x, y: $y }),+) }; ([$($xi:literal $yi:literal),+ $(,)?], $([$($xo:literal $yo:literal),+ $(,)?]),*) => { polygon!( - exterior: [$(coord!(x: $xi, y: $yi)),+], + exterior: [$($crate::coord!(x: $xi, y: $yi)),+], interiors: [ $([ - $(coord!(x: $xo, y: $yo),)+ + $($crate::coord!(x: $xo, y: $yo),)+ ]),* ] ) From 4b1248b97d99b313aaf11c1af7ab240cbcabeb6c Mon Sep 17 00:00:00 2001 From: Dmitri Lebedev Date: Fri, 15 Sep 2023 23:08:25 +0600 Subject: [PATCH 3/5] minor import fixes in wkt-like-syntax --- geo-types/src/macros.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/geo-types/src/macros.rs b/geo-types/src/macros.rs index 87f00f869..6df761c5f 100644 --- a/geo-types/src/macros.rs +++ b/geo-types/src/macros.rs @@ -107,15 +107,15 @@ macro_rules! coord { /// x: -21.95156, /// y: 64.1446, /// }; -/// let coord2 = coord! { +/// let coord2 = geo_types::coord! { /// x: -21.951, /// y: 64.14479, /// }; -/// let coord3 = coord! { +/// let coord3 = geo_types::coord! { /// x: -21.95044, /// y: 64.14527, /// }; -/// let coord4 = coord! { +/// let coord4 = geo_types::coord! { /// x: -21.951445, /// y: 64.145508, /// }; From cdf077361374f9a47ca649fc8c1dc11527b451c0 Mon Sep 17 00:00:00 2001 From: Dmitri Lebedev Date: Fri, 15 Sep 2023 23:20:59 +0600 Subject: [PATCH 4/5] fixed doc tests due to linter complaints --- geo-types/src/macros.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/geo-types/src/macros.rs b/geo-types/src/macros.rs index 6df761c5f..f8c605ad3 100644 --- a/geo-types/src/macros.rs +++ b/geo-types/src/macros.rs @@ -67,9 +67,9 @@ macro_rules! coord { /// ``` /// /// # Examples -/// +/// /// Creating a [`LineString`] with WKT-like syntax: -/// +/// /// ``` /// use geo_types::line_string; /// let ls = line_string![76.9454 43.2497, 76.9636 43.2308, 77.0591 43.1575, 77.1108 43.1131]; @@ -180,25 +180,25 @@ macro_rules! line_string { /// ``` /// /// # Examples -/// +/// /// Creating a [`Polygon'] with WKT-like syntax: -/// +/// /// ``` /// use geo_types::polygon; -/// +/// /// let simple_poly = polygon!(0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0); /// assert_eq!(simple_poly.exterior()[1], geo_types::coord!{ x: 30.0, y: 0.0 }); /// assert_eq!(simple_poly.interiors().len(), 0); -/// +/// /// let poly_with_hole = polygon!([0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0], [10.0 10.0, 20.0 10.0, 20.0 20.0, 10.0 20.0, 10.0 10.0]); /// assert_eq!(poly_with_hole.interiors()[0][1], geo_types::coord!{ x: 20.0, y: 10.0 }); /// ``` -/// +/// /// Creating a [`Polygon`] without interior rings, supplying x/y values: /// /// ``` /// use geo_types::polygon; -/// +/// /// let poly = polygon![ /// (x: -111., y: 45.), /// (x: -111., y: 41.), @@ -423,9 +423,12 @@ mod test { assert_eq!(p.interiors()[0][0], coord! { x: 3, y: 4 }); let simple_poly = polygon!(0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0); - assert_eq!(simple_poly.exterior()[1], coord!{ x: 30.0, y: 0.0 }); + assert_eq!(simple_poly.exterior()[1], coord! { x: 30.0, y: 0.0 }); let poly_with_hole = polygon!([0.0 0.0, 30.0 0.0, 30.0 30.0, 0.0 30.0, 0.0 0.0], [10.0 10.0, 20.0 10.0, 20.0 20.0, 10.0 20.0, 10.0 10.0]); - assert_eq!(poly_with_hole.interiors()[0][1], coord!{ x: 20.0, y: 10.0 }); + assert_eq!( + poly_with_hole.interiors()[0][1], + coord! { x: 20.0, y: 10.0 } + ); } } From 0c979bde02c29ec8a33643e335996c56d963b7e8 Mon Sep 17 00:00:00 2001 From: Dmitri Lebedev Date: Sat, 16 Sep 2023 15:20:42 +0600 Subject: [PATCH 5/5] added a line to CHANGES.md --- geo-types/CHANGES.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/geo-types/CHANGES.md b/geo-types/CHANGES.md index ede3ff159..a2f99f0a9 100644 --- a/geo-types/CHANGES.md +++ b/geo-types/CHANGES.md @@ -1,5 +1,10 @@ # Changes +## 0.7.12 + +* Simpler syntax for `line_string` and `polygon` macros, like in WKT format. + * + ## 0.7.11 * Bump rstar dependency