Skip to content

Commit

Permalink
Merge #771
Browse files Browse the repository at this point in the history
771: Generalize geo-type macros r=frewsxcv a=nyurik

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.

- [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md).
- ~[ ] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.~
---



Co-authored-by: Yuri Astrakhan <[email protected]>
Co-authored-by: Yuri Astrakhan <[email protected]>
  • Loading branch information
3 people authored Mar 16, 2022
2 parents ec903db + f823051 commit 7325164
Showing 1 changed file with 87 additions and 8 deletions.
95 changes: 87 additions & 8 deletions geo-types/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 , )* },
)*
]
};
Expand Down Expand Up @@ -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),* $(,)? )),*
$(,)?
]),*
$(,)?
Expand All @@ -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 , )* }),*
]),*
],
)
Expand Down Expand Up @@ -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 , )* }),*
]
};
(
Expand All @@ -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 });
}
}

0 comments on commit 7325164

Please sign in to comment.