1use geo_types::{CoordNum, Coordinate};
2use std::borrow::Borrow;
3
4#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)]
5pub struct Rect<T: CoordNum> {
6 min: Coordinate<T>,
7 max: Coordinate<T>,
8}
9
10impl<T: CoordNum> Rect<T> {
11 pub fn new<C>(c1: C, c2: C) -> Self
12 where
13 C: Into<Coordinate<T>>,
14 {
15 let min = c1.into();
16 let max = c2.into();
17 Self { min, max }
18 }
19
20 pub fn min(self) -> Coordinate<T> {
21 self.min
22 }
23
24 pub fn max(self) -> Coordinate<T> {
25 self.max
26 }
27}
28
29impl<C, G> From<G> for Rect<C>
30where
31 C: CoordNum,
32 G: Borrow<geo_types::Rect<C>>,
33{
34 fn from(geo_rect: G) -> Self {
35 let geo_rect = geo_rect.borrow();
36 Rect::new(geo_rect.min(), geo_rect.max())
37 }
38}
39
40#[cfg(test)]
41mod test {
42 use super::*;
43
44 #[test]
45 fn test_rect_from_geo_types_rect() {
46 let (min_x, max_x) = (30.0, 40.0);
47 let (min_y, max_y) = (38.0, 39.0);
48
49 let bbox_regular = geo_types::Rect::new(
51 Coordinate { x: min_x, y: min_y },
52 Coordinate { x: max_x, y: max_y },
53 );
54
55 let bbox_rect_regular: Rect<f64> = bbox_regular.into();
56
57 assert_eq!(bbox_rect_regular.min().x, min_x);
58 assert_eq!(bbox_rect_regular.min().y, min_y);
59 assert_eq!(bbox_rect_regular.max().x, max_x);
60 assert_eq!(bbox_rect_regular.max().y, max_y);
61
62 let (min_x, max_x) = (179.9, -179.9);
64 let (min_y, max_y) = (32.7, 32.9);
65
66 let bbox_date_line = geo_types::Rect::new(
67 Coordinate { x: min_x, y: min_y },
68 Coordinate { x: max_x, y: max_y },
69 );
70
71 let bbox_rect_date_line: Rect<f64> = bbox_date_line.into();
72
73 assert_eq!(bbox_rect_date_line.min().x, max_x);
75 assert_eq!(bbox_rect_date_line.min().y, min_y);
76 assert_eq!(bbox_rect_date_line.max().x, min_x);
77 assert_eq!(bbox_rect_date_line.max().y, max_y);
78 }
79}