From d3733756e1fa7613d7566e5e843bcbb0858476f0 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 15 Mar 2022 00:22:14 -0400 Subject: [PATCH 1/4] Generalize geo-type macros Make all coordinate-specific logic limited just to the `coord!` macro, and all other macros will pass their parameters as is. This change is a noop because `coord!` macro still uses the same parameter pattern as before. This PR removes hardcoded x,y coordinates from the `point!`, `line_string!`, and `polygon!` macros, and instead each macro expects `token: expression` pair(s) that is passed as a parameter to the `coord!` macro. --- geo-types/src/macros.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/geo-types/src/macros.rs b/geo-types/src/macros.rs index abfc58175e..58286ae414 100644 --- a/geo-types/src/macros.rs +++ b/geo-types/src/macros.rs @@ -22,8 +22,8 @@ /// [`Point`]: ./struct.Point.html #[macro_export] macro_rules! point { - (x: $x:expr, y: $y:expr $(,)?) => { - $crate::Point::new($x, $y) + ( $($tag:tt : $val:expr),* ) => { + $crate::Point ( $crate::coord! { $( $tag: $val , )* } ) }; } @@ -118,12 +118,12 @@ macro_rules! coord { macro_rules! line_string { () => { $crate::LineString(vec![]) }; ( - $((x: $x:expr, y: $y:expr $(,)?)),* + $(( $($tag:tt : $val:expr),* )),* $(,)? ) => { line_string![ $( - $crate::coord! { x: $x, y: $y }, + $crate::coord! { $( $tag: $val , )* }, )* ] }; @@ -212,12 +212,12 @@ macro_rules! polygon { () => { $crate::Polygon::new(line_string![], vec![]) }; ( exterior: [ - $((x: $exterior_x:expr, y: $exterior_y:expr $(,)?)),* + $(( $($exterior_tag:tt : $exterior_val:expr),* )),* $(,)? ], interiors: [ $([ - $((x: $interior_x:expr, y: $interior_y:expr $(,)?)),* + $(( $($interior_tag:tt : $interior_val:expr),* )),* $(,)? ]),* $(,)? @@ -227,12 +227,12 @@ macro_rules! polygon { polygon!( exterior: [ $( - $crate::coord! { x: $exterior_x, y: $exterior_y }, + $crate::coord! { $( $exterior_tag: $exterior_val , )* }, )* ], interiors: [ $([ - $($crate::coord! { x: $interior_x, y: $interior_y }),* + $($crate::coord! { $( $interior_tag: $interior_val , )* }),* ]),* ], ) @@ -267,11 +267,11 @@ macro_rules! polygon { ) }; ( - $((x: $x:expr, y: $y:expr $(,)?)),* + $(( $($tag:tt : $val:expr),* )),* $(,)? ) => { polygon![ - $($crate::coord! { x: $x, y: $y }),* + $($crate::coord! { $( $tag: $val , )* }),* ] }; ( From f362244ea9f8e257834d1c91dd757e2a6df41871 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 15 Mar 2022 00:53:34 -0400 Subject: [PATCH 2/4] fix commas, add tests --- geo-types/src/macros.rs | 89 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 5 deletions(-) diff --git a/geo-types/src/macros.rs b/geo-types/src/macros.rs index 58286ae414..d1f1fbc9ad 100644 --- a/geo-types/src/macros.rs +++ b/geo-types/src/macros.rs @@ -22,7 +22,7 @@ /// [`Point`]: ./struct.Point.html #[macro_export] macro_rules! point { - ( $($tag:tt : $val:expr),* ) => { + ( $($tag:tt : $val:expr),* $(,)? ) => { $crate::Point ( $crate::coord! { $( $tag: $val , )* } ) }; } @@ -118,7 +118,7 @@ macro_rules! coord { macro_rules! line_string { () => { $crate::LineString(vec![]) }; ( - $(( $($tag:tt : $val:expr),* )),* + $(( $($tag:tt : $val:expr),* $(,)? )),* $(,)? ) => { line_string![ @@ -212,12 +212,12 @@ macro_rules! polygon { () => { $crate::Polygon::new(line_string![], vec![]) }; ( exterior: [ - $(( $($exterior_tag:tt : $exterior_val:expr),* )),* + $(( $($exterior_tag:tt : $exterior_val:expr),* $(,)? )),* $(,)? ], interiors: [ $([ - $(( $($interior_tag:tt : $interior_val:expr),* )),* + $(( $($interior_tag:tt : $interior_val:expr),* $(,)? )),* $(,)? ]),* $(,)? @@ -267,7 +267,7 @@ macro_rules! polygon { ) }; ( - $(( $($tag:tt : $val:expr),* )),* + $(( $($tag:tt : $val:expr),* $(,)? )),* $(,)? ) => { polygon![ @@ -284,3 +284,82 @@ macro_rules! polygon { ) }; } + +#[cfg(test)] +mod test { + #[test] + fn test_point() { + let p = point!(x: 1.2, y: 3.4); + assert_eq!(p.x(), 1.2); + assert_eq!(p.y(), 3.4); + + let p = point! { + x: 1.2, + y: 3.4, + }; + assert_eq!(p.x(), 1.2); + assert_eq!(p.y(), 3.4); + } + + #[test] + fn test_line() { + let ls = line_string![(x: -1.2f32, y: 3.4f32)]; + assert_eq!(ls[0], coord! {x: -1.2, y: 3.4}); + + let ls = line_string![ + (x: -1.2f32, y: 3.4f32), + ]; + assert_eq!(ls[0], coord! {x: -1.2, y: 3.4}); + + let ls = line_string![( + x: -1.2f32, + y: 3.4f32, + )]; + assert_eq!(ls[0], coord! {x: -1.2, y: 3.4}); + + let ls = line_string![ + (x: -1.2f32, y: 3.4f32), + (x: -5.6, y: 7.8), + ]; + assert_eq!(ls[0], coord! {x: -1.2, y: 3.4}); + assert_eq!(ls[1], coord! {x: -5.6, y: 7.8}); + } + + #[test] + fn test_polygon() { + let p = polygon!( + exterior: [(x: 1, y: 2)], + interiors: [[(x: 3, y: 4)]] + ); + assert_eq!(p.exterior()[0], coord! {x: 1, y: 2}); + assert_eq!(p.interiors()[0][0], coord! {x: 3, y: 4}); + + let p = polygon!( + exterior: [(x: 1, y: 2)], + interiors: [[(x: 3, y: 4)]], + ); + assert_eq!(p.exterior()[0], coord! {x: 1, y: 2}); + assert_eq!(p.interiors()[0][0], coord! {x: 3, y: 4}); + + let p = polygon!( + exterior: [(x: 1, y: 2, )], + interiors: [[(x: 3, y: 4, )]], + ); + assert_eq!(p.exterior()[0], coord! {x: 1, y: 2}); + assert_eq!(p.interiors()[0][0], coord! {x: 3, y: 4}); + + let p = polygon!( + exterior: [(x: 1, y: 2, ), ], + interiors: [[(x: 3, y: 4, ), ]], + ); + assert_eq!(p.exterior()[0], coord! {x: 1, y: 2}); + assert_eq!(p.interiors()[0][0], coord! {x: 3, y: 4}); + + let p = polygon!( + exterior: [(x: 1, y: 2, ), ], + interiors: [[(x: 3, y: 4, ), ], ], + ); + assert_eq!(p.exterior()[0], coord! {x: 1, y: 2}); + assert_eq!(p.interiors()[0][0], coord! {x: 3, y: 4}); + } +} From 55aab92d2fbda7f40f430164d809368abb71f50f Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Wed, 16 Mar 2022 09:47:31 -0400 Subject: [PATCH 3/4] Update geo-types/src/macros.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Laurențiu Nicola --- geo-types/src/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geo-types/src/macros.rs b/geo-types/src/macros.rs index d1f1fbc9ad..b941636f12 100644 --- a/geo-types/src/macros.rs +++ b/geo-types/src/macros.rs @@ -304,7 +304,7 @@ mod test { #[test] fn test_line() { let ls = line_string![(x: -1.2f32, y: 3.4f32)]; - assert_eq!(ls[0], coord! {x: -1.2, y: 3.4}); + assert_eq!(ls[0], coord! { x: -1.2, y: 3.4 }); let ls = line_string![ (x: -1.2f32, y: 3.4f32), From f8230514380052a5f915cae08d6695ca7f431e58 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Wed, 16 Mar 2022 09:57:59 -0400 Subject: [PATCH 4/4] spacing --- geo-types/src/macros.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/geo-types/src/macros.rs b/geo-types/src/macros.rs index b941636f12..c058264cd8 100644 --- a/geo-types/src/macros.rs +++ b/geo-types/src/macros.rs @@ -309,20 +309,20 @@ mod test { let ls = line_string![ (x: -1.2f32, y: 3.4f32), ]; - assert_eq!(ls[0], coord! {x: -1.2, y: 3.4}); + assert_eq!(ls[0], coord! { x: -1.2, y: 3.4 }); let ls = line_string![( x: -1.2f32, y: 3.4f32, )]; - assert_eq!(ls[0], coord! {x: -1.2, y: 3.4}); + assert_eq!(ls[0], coord! { x: -1.2, y: 3.4 }); let ls = line_string![ (x: -1.2f32, y: 3.4f32), (x: -5.6, y: 7.8), ]; - assert_eq!(ls[0], coord! {x: -1.2, y: 3.4}); - assert_eq!(ls[1], coord! {x: -5.6, y: 7.8}); + assert_eq!(ls[0], coord! { x: -1.2, y: 3.4 }); + assert_eq!(ls[1], coord! { x: -5.6, y: 7.8 }); } #[test] @@ -331,35 +331,35 @@ mod test { exterior: [(x: 1, y: 2)], interiors: [[(x: 3, y: 4)]] ); - assert_eq!(p.exterior()[0], coord! {x: 1, y: 2}); - assert_eq!(p.interiors()[0][0], coord! {x: 3, y: 4}); + assert_eq!(p.exterior()[0], coord! { x: 1, y: 2 }); + assert_eq!(p.interiors()[0][0], coord! { x: 3, y: 4 }); let p = polygon!( exterior: [(x: 1, y: 2)], interiors: [[(x: 3, y: 4)]], ); - assert_eq!(p.exterior()[0], coord! {x: 1, y: 2}); - assert_eq!(p.interiors()[0][0], coord! {x: 3, y: 4}); + assert_eq!(p.exterior()[0], coord! { x: 1, y: 2 }); + assert_eq!(p.interiors()[0][0], coord! { x: 3, y: 4 }); let p = polygon!( exterior: [(x: 1, y: 2, )], interiors: [[(x: 3, y: 4, )]], ); - assert_eq!(p.exterior()[0], coord! {x: 1, y: 2}); - assert_eq!(p.interiors()[0][0], coord! {x: 3, y: 4}); + assert_eq!(p.exterior()[0], coord! { x: 1, y: 2 }); + assert_eq!(p.interiors()[0][0], coord! { x: 3, y: 4 }); let p = polygon!( exterior: [(x: 1, y: 2, ), ], interiors: [[(x: 3, y: 4, ), ]], ); - assert_eq!(p.exterior()[0], coord! {x: 1, y: 2}); - assert_eq!(p.interiors()[0][0], coord! {x: 3, y: 4}); + assert_eq!(p.exterior()[0], coord! { x: 1, y: 2 }); + assert_eq!(p.interiors()[0][0], coord! { x: 3, y: 4 }); let p = polygon!( exterior: [(x: 1, y: 2, ), ], interiors: [[(x: 3, y: 4, ), ], ], ); - assert_eq!(p.exterior()[0], coord! {x: 1, y: 2}); - assert_eq!(p.interiors()[0][0], coord! {x: 3, y: 4}); + assert_eq!(p.exterior()[0], coord! { x: 1, y: 2 }); + assert_eq!(p.interiors()[0][0], coord! { x: 3, y: 4 }); } }