#[macro_export]
macro_rules! point {
( $($tag:tt : $val:expr),* $(,)? ) => {
$crate::point! ( $crate::coord! { $( $tag: $val , )* } )
};
( $coord:expr $(,)? ) => {
$crate::Point::from($coord)
};
}
#[macro_export]
macro_rules! coord {
(x: $x:expr, y: $y:expr $(,)* ) => {
$crate::Coord { x: $x, y: $y }
};
}
#[macro_export]
macro_rules! line_string {
() => { $crate::LineString::empty() };
(
$(( $($tag:tt : $val:expr),* $(,)? )),*
$(,)?
) => {
line_string![
$(
$crate::coord! { $( $tag: $val , )* },
)*
]
};
(
$($coord:expr),*
$(,)?
) => {
$crate::LineString::new(
$crate::_alloc::vec![
$($coord),*
]
)
};
}
#[macro_export]
macro_rules! polygon {
() => { $crate::Polygon::empty() };
(
exterior: [
$(( $($exterior_tag:tt : $exterior_val:expr),* $(,)? )),*
$(,)?
],
interiors: [
$([
$(( $($interior_tag:tt : $interior_val:expr),* $(,)? )),*
$(,)?
]),*
$(,)?
]
$(,)?
) => {
polygon!(
exterior: [
$(
$crate::coord! { $( $exterior_tag: $exterior_val , )* },
)*
],
interiors: [
$([
$($crate::coord! { $( $interior_tag: $interior_val , )* }),*
]),*
],
)
};
(
exterior: [
$($exterior_coord:expr),*
$(,)?
],
interiors: [
$([
$($interior_coord:expr),*
$(,)?
]),*
$(,)?
]
$(,)?
) => {
$crate::Polygon::new(
$crate::line_string![
$($exterior_coord), *
],
$crate::_alloc::vec![
$(
$crate::line_string![$($interior_coord),*]
), *
]
)
};
(
$(( $($tag:tt : $val:expr),* $(,)? )),*
$(,)?
) => {
polygon![
$($crate::coord! { $( $tag: $val , )* }),*
]
};
(
$($coord:expr),*
$(,)?
) => {
$crate::Polygon::new(
$crate::line_string![$($coord,)*],
$crate::_alloc::vec![],
)
};
}
#[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);
let p = point!(coord! { x: 1.2, y: 3.4 });
assert_eq!(p.x(), 1.2);
assert_eq!(p.y(), 3.4);
let p = point!(coord! { 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 });
}
}