1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
 * Rust type for
 * [box](https://www.postgresql.org/docs/current/datatype-geometric.html#id-1.5.7.16.8).
 */
#[cfg_attr(docsrs, doc(cfg(feature = "geo")))]
#[derive(Clone, Debug, PartialEq)]
pub struct Box(geo_types::Rect<f64>);

impl Box {
    #[must_use]
    pub fn new(start: &crate::Point, end: &crate::Point) -> Self {
        Self(geo_types::Rect::new(**start, **end))
    }
}

impl std::ops::Deref for Box {
    type Target = geo_types::Rect<f64>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl std::fmt::Display for Box {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "(({}, {}), ({}, {}))",
            self.0.min().x,
            self.0.min().y,
            self.0.max().x,
            self.0.max().y
        )
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for Box {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        let b = Self::new(&crate::Point::arbitrary(u)?, &crate::Point::arbitrary(u)?);

        Ok(b)
    }
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo")))]
impl crate::ToSql for Box {
    fn ty(&self) -> crate::pq::Type {
        crate::pq::types::BOX
    }

    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/geo_ops.c#L443
     */
    fn to_text(&self) -> crate::Result<Option<String>> {
        self.to_string().to_text()
    }

    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/geo_ops.c#L489
     */
    fn to_binary(&self) -> crate::Result<Option<Vec<u8>>> {
        let mut buf = Vec::new();

        crate::to_sql::write_f64(&mut buf, self.0.max().x)?;
        crate::to_sql::write_f64(&mut buf, self.0.max().y)?;
        crate::to_sql::write_f64(&mut buf, self.0.min().x)?;
        crate::to_sql::write_f64(&mut buf, self.0.min().y)?;

        Ok(Some(buf))
    }
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo")))]
impl crate::FromSql for Box {
    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/geo_ops.c#L413
     */
    fn from_text(ty: &crate::pq::Type, raw: Option<&str>) -> crate::Result<Self> {
        let segment = crate::Segment::from_text(ty, raw)?;

        Ok(Self::new(&segment.end.into(), &segment.start.into()))
    }

    /*
     * https://github.com/postgres/postgres/blob/REL_12_0/src/backend/utils/adt/geo_ops.c#L454
     */
    fn from_binary(ty: &crate::pq::Type, raw: Option<&[u8]>) -> crate::Result<Self> {
        let segment = crate::Segment::from_binary(ty, raw)?;

        Ok(Self::new(&segment.end.into(), &segment.start.into()))
    }
}

#[cfg_attr(docsrs, doc(cfg(feature = "geo")))]
impl crate::entity::Simple for Box {}

#[cfg(test)]
mod test {
    #![allow(non_snake_case)]
    crate::sql_test!(
        Box,
        crate::Box,
        [
            (
                "'((1, 2), (3, 4))'",
                crate::Box::new(&crate::Point::new(1., 2.), &crate::Point::new(3., 4.))
            ),
            (
                "'((0.5, 0.003), (10.3, 20.0))'",
                crate::Box::new(
                    &crate::Point::new(0.5, 0.003),
                    &crate::Point::new(10.3, 20.)
                )
            ),
        ]
    );
}