Skip to content

Commit 8b8491f

Browse files
committed
retain support for geo-types-0.6
1 parent 37fb392 commit 8b8491f

File tree

9 files changed

+143
-0
lines changed

9 files changed

+143
-0
lines changed

postgres-types/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ derive = ["postgres-derive"]
1515
with-bit-vec-0_6 = ["bit-vec-06"]
1616
with-chrono-0_4 = ["chrono-04"]
1717
with-eui48-0_4 = ["eui48-04"]
18+
with-geo-types-0_6 = ["geo-types-06"]
1819
with-geo-types-0_7 = ["geo-types-0_7"]
1920
with-serde_json-1 = ["serde-1", "serde_json-1"]
2021
with-uuid-0_8 = ["uuid-08"]
@@ -29,6 +30,7 @@ postgres-derive = { version = "0.4.0", optional = true, path = "../postgres-deri
2930
bit-vec-06 = { version = "0.6", package = "bit-vec", optional = true }
3031
chrono-04 = { version = "0.4.16", package = "chrono", default-features = false, features = ["clock"], optional = true }
3132
eui48-04 = { version = "0.4", package = "eui48", optional = true }
33+
geo-types-06 = { version = "0.6", package = "geo-types", optional = true }
3234
geo-types-0_7 = { version = "0.7", package = "geo-types", optional = true }
3335
serde-1 = { version = "1.0", package = "serde", optional = true }
3436
serde_json-1 = { version = "1.0", package = "serde_json", optional = true }

postgres-types/src/geo_types_06.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
use bytes::BytesMut;
2+
use fallible_iterator::FallibleIterator;
3+
use geo_types_06::{Coordinate, LineString, Point, Rect};
4+
use postgres_protocol::types;
5+
use std::error::Error;
6+
7+
use crate::{FromSql, IsNull, ToSql, Type};
8+
9+
impl<'a> FromSql<'a> for Point<f64> {
10+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
11+
let point = types::point_from_sql(raw)?;
12+
Ok(Point::new(point.x(), point.y()))
13+
}
14+
15+
accepts!(POINT);
16+
}
17+
18+
impl ToSql for Point<f64> {
19+
fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
20+
types::point_to_sql(self.x(), self.y(), out);
21+
Ok(IsNull::No)
22+
}
23+
24+
accepts!(POINT);
25+
to_sql_checked!();
26+
}
27+
28+
impl<'a> FromSql<'a> for Rect<f64> {
29+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
30+
let rect = types::box_from_sql(raw)?;
31+
Ok(Rect::new(
32+
(rect.lower_left().x(), rect.lower_left().y()),
33+
(rect.upper_right().x(), rect.upper_right().y()),
34+
))
35+
}
36+
37+
accepts!(BOX);
38+
}
39+
40+
impl ToSql for Rect<f64> {
41+
fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
42+
types::box_to_sql(self.min().x, self.min().y, self.max().x, self.max().y, out);
43+
Ok(IsNull::No)
44+
}
45+
46+
accepts!(BOX);
47+
to_sql_checked!();
48+
}
49+
50+
impl<'a> FromSql<'a> for LineString<f64> {
51+
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
52+
let path = types::path_from_sql(raw)?;
53+
let points = path
54+
.points()
55+
.map(|p| Ok(Coordinate { x: p.x(), y: p.y() }))
56+
.collect()?;
57+
Ok(LineString(points))
58+
}
59+
60+
accepts!(PATH);
61+
}
62+
63+
impl ToSql for LineString<f64> {
64+
fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
65+
let closed = false; // always encode an open path from LineString
66+
types::path_to_sql(closed, self.0.iter().map(|p| (p.x, p.y)), out)?;
67+
Ok(IsNull::No)
68+
}
69+
70+
accepts!(PATH);
71+
to_sql_checked!();
72+
}

postgres-types/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ mod bit_vec_06;
194194
mod chrono_04;
195195
#[cfg(feature = "with-eui48-0_4")]
196196
mod eui48_04;
197+
#[cfg(feature = "with-geo-types-0_6")]
198+
mod geo_types_06;
197199
#[cfg(feature = "with-geo-types-0_7")]
198200
mod geo_types_07;
199201
#[cfg(feature = "with-serde_json-1")]

postgres/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ circle-ci = { repository = "sfackler/rust-postgres" }
2424
with-bit-vec-0_6 = ["tokio-postgres/with-bit-vec-0_6"]
2525
with-chrono-0_4 = ["tokio-postgres/with-chrono-0_4"]
2626
with-eui48-0_4 = ["tokio-postgres/with-eui48-0_4"]
27+
with-geo-types-0_6 = ["tokio-postgres/with-geo-types-0_6"]
2728
with-geo-types-0_7 = ["tokio-postgres/with-geo-types-0_7"]
2829
with-serde_json-1 = ["tokio-postgres/with-serde_json-1"]
2930
with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"]

postgres/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
//! | `with-bit-vec-0_6` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.6 | no |
5757
//! | `with-chrono-0_4` | Enable support for the `chrono` crate. | [chrono](https://crates.io/crates/chrono) 0.4 | no |
5858
//! | `with-eui48-0_4` | Enable support for the `eui48` crate. | [eui48](https://crates.io/crates/eui48) 0.4 | no |
59+
//! | `with-geo-types-0_6` | Enable support for the 0.6 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.6.0) 0.6 | no |
5960
//! | `with-geo-types-0_7` | Enable support for the 0.7 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.7.0) 0.7 | no |
6061
//! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | no |
6162
//! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no |

tokio-postgres/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ runtime = ["tokio/net", "tokio/time"]
3030
with-bit-vec-0_6 = ["postgres-types/with-bit-vec-0_6"]
3131
with-chrono-0_4 = ["postgres-types/with-chrono-0_4"]
3232
with-eui48-0_4 = ["postgres-types/with-eui48-0_4"]
33+
with-geo-types-0_6 = ["postgres-types/with-geo-types-0_6"]
3334
with-geo-types-0_7 = ["postgres-types/with-geo-types-0_7"]
3435
with-serde_json-1 = ["postgres-types/with-serde_json-1"]
3536
with-uuid-0_8 = ["postgres-types/with-uuid-0_8"]
@@ -60,6 +61,7 @@ criterion = "0.3"
6061
bit-vec-06 = { version = "0.6", package = "bit-vec" }
6162
chrono-04 = { version = "0.4", package = "chrono", default-features = false }
6263
eui48-04 = { version = "0.4", package = "eui48" }
64+
geo-types-06 = { version = "0.6", package = "geo-types" }
6365
geo-types-07 = { version = "0.7", package = "geo-types" }
6466
serde-1 = { version = "1.0", package = "serde" }
6567
serde_json-1 = { version = "1.0", package = "serde_json" }

tokio-postgres/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
//! | `with-bit-vec-0_6` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.6 | no |
108108
//! | `with-chrono-0_4` | Enable support for the `chrono` crate. | [chrono](https://crates.io/crates/chrono) 0.4 | no |
109109
//! | `with-eui48-0_4` | Enable support for the `eui48` crate. | [eui48](https://crates.io/crates/eui48) 0.4 | no |
110+
//! | `with-geo-types-0_6` | Enable support for the 0.6 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.6.0) 0.6 | no |
110111
//! | `with-geo-types-0_7` | Enable support for the 0.7 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.7.0) 0.7 | no |
111112
//! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | no |
112113
//! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use geo_types_06::{Coordinate, LineString, Point, Rect};
2+
3+
use crate::types::test_type;
4+
5+
#[tokio::test]
6+
async fn test_point_params() {
7+
test_type(
8+
"POINT",
9+
&[
10+
(Some(Point::new(0.0, 0.0)), "POINT(0, 0)"),
11+
(Some(Point::new(-3.2, 1.618)), "POINT(-3.2, 1.618)"),
12+
(None, "NULL"),
13+
],
14+
)
15+
.await;
16+
}
17+
18+
#[tokio::test]
19+
async fn test_box_params() {
20+
test_type(
21+
"BOX",
22+
&[
23+
(
24+
Some(Rect::new(
25+
Coordinate { x: -3.2, y: 1.618 },
26+
Coordinate {
27+
x: 160.0,
28+
y: 69701.5615,
29+
},
30+
)),
31+
"BOX(POINT(160.0, 69701.5615), POINT(-3.2, 1.618))",
32+
),
33+
(None, "NULL"),
34+
],
35+
)
36+
.await;
37+
}
38+
39+
#[tokio::test]
40+
async fn test_path_params() {
41+
let points = vec![
42+
Coordinate { x: 0., y: 0. },
43+
Coordinate { x: -3.2, y: 1.618 },
44+
Coordinate {
45+
x: 160.0,
46+
y: 69701.5615,
47+
},
48+
];
49+
test_type(
50+
"PATH",
51+
&[
52+
(
53+
Some(LineString(points)),
54+
"path '((0, 0), (-3.2, 1.618), (160.0, 69701.5615))'",
55+
),
56+
(None, "NULL"),
57+
],
58+
)
59+
.await;
60+
}

tokio-postgres/tests/test/types/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ mod bit_vec_06;
1919
mod chrono_04;
2020
#[cfg(feature = "with-eui48-0_4")]
2121
mod eui48_04;
22+
#[cfg(feature = "with-geo-types-0_6")]
23+
mod geo_types_06;
2224
#[cfg(feature = "with-geo-types-0_7")]
2325
mod geo_types_07;
2426
#[cfg(feature = "with-serde_json-1")]

0 commit comments

Comments
 (0)