From 8b8491f31dc55e5cdc286c616a9900af622f9fa7 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Mon, 25 Jan 2021 14:49:06 -0600 Subject: [PATCH] retain support for geo-types-0.6 --- postgres-types/Cargo.toml | 2 + postgres-types/src/geo_types_06.rs | 72 +++++++++++++++++++ postgres-types/src/lib.rs | 2 + postgres/Cargo.toml | 1 + postgres/src/lib.rs | 1 + tokio-postgres/Cargo.toml | 2 + tokio-postgres/src/lib.rs | 1 + .../tests/test/types/geo_types_06.rs | 60 ++++++++++++++++ tokio-postgres/tests/test/types/mod.rs | 2 + 9 files changed, 143 insertions(+) create mode 100644 postgres-types/src/geo_types_06.rs create mode 100644 tokio-postgres/tests/test/types/geo_types_06.rs diff --git a/postgres-types/Cargo.toml b/postgres-types/Cargo.toml index ce5b0ab0..7d48596a 100644 --- a/postgres-types/Cargo.toml +++ b/postgres-types/Cargo.toml @@ -15,6 +15,7 @@ derive = ["postgres-derive"] with-bit-vec-0_6 = ["bit-vec-06"] with-chrono-0_4 = ["chrono-04"] with-eui48-0_4 = ["eui48-04"] +with-geo-types-0_6 = ["geo-types-06"] with-geo-types-0_7 = ["geo-types-0_7"] with-serde_json-1 = ["serde-1", "serde_json-1"] with-uuid-0_8 = ["uuid-08"] @@ -29,6 +30,7 @@ postgres-derive = { version = "0.4.0", optional = true, path = "../postgres-deri bit-vec-06 = { version = "0.6", package = "bit-vec", optional = true } chrono-04 = { version = "0.4.16", package = "chrono", default-features = false, features = ["clock"], optional = true } eui48-04 = { version = "0.4", package = "eui48", optional = true } +geo-types-06 = { version = "0.6", package = "geo-types", optional = true } geo-types-0_7 = { version = "0.7", package = "geo-types", optional = true } serde-1 = { version = "1.0", package = "serde", optional = true } serde_json-1 = { version = "1.0", package = "serde_json", optional = true } diff --git a/postgres-types/src/geo_types_06.rs b/postgres-types/src/geo_types_06.rs new file mode 100644 index 00000000..0f0b14fd --- /dev/null +++ b/postgres-types/src/geo_types_06.rs @@ -0,0 +1,72 @@ +use bytes::BytesMut; +use fallible_iterator::FallibleIterator; +use geo_types_06::{Coordinate, LineString, Point, Rect}; +use postgres_protocol::types; +use std::error::Error; + +use crate::{FromSql, IsNull, ToSql, Type}; + +impl<'a> FromSql<'a> for Point { + fn from_sql(_: &Type, raw: &[u8]) -> Result> { + let point = types::point_from_sql(raw)?; + Ok(Point::new(point.x(), point.y())) + } + + accepts!(POINT); +} + +impl ToSql for Point { + fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result> { + types::point_to_sql(self.x(), self.y(), out); + Ok(IsNull::No) + } + + accepts!(POINT); + to_sql_checked!(); +} + +impl<'a> FromSql<'a> for Rect { + fn from_sql(_: &Type, raw: &[u8]) -> Result> { + let rect = types::box_from_sql(raw)?; + Ok(Rect::new( + (rect.lower_left().x(), rect.lower_left().y()), + (rect.upper_right().x(), rect.upper_right().y()), + )) + } + + accepts!(BOX); +} + +impl ToSql for Rect { + fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result> { + types::box_to_sql(self.min().x, self.min().y, self.max().x, self.max().y, out); + Ok(IsNull::No) + } + + accepts!(BOX); + to_sql_checked!(); +} + +impl<'a> FromSql<'a> for LineString { + fn from_sql(_: &Type, raw: &[u8]) -> Result> { + let path = types::path_from_sql(raw)?; + let points = path + .points() + .map(|p| Ok(Coordinate { x: p.x(), y: p.y() })) + .collect()?; + Ok(LineString(points)) + } + + accepts!(PATH); +} + +impl ToSql for LineString { + fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result> { + let closed = false; // always encode an open path from LineString + types::path_to_sql(closed, self.0.iter().map(|p| (p.x, p.y)), out)?; + Ok(IsNull::No) + } + + accepts!(PATH); + to_sql_checked!(); +} diff --git a/postgres-types/src/lib.rs b/postgres-types/src/lib.rs index d4595091..e9a7c10a 100644 --- a/postgres-types/src/lib.rs +++ b/postgres-types/src/lib.rs @@ -194,6 +194,8 @@ mod bit_vec_06; mod chrono_04; #[cfg(feature = "with-eui48-0_4")] mod eui48_04; +#[cfg(feature = "with-geo-types-0_6")] +mod geo_types_06; #[cfg(feature = "with-geo-types-0_7")] mod geo_types_07; #[cfg(feature = "with-serde_json-1")] diff --git a/postgres/Cargo.toml b/postgres/Cargo.toml index 7916a5e0..0128f8a4 100644 --- a/postgres/Cargo.toml +++ b/postgres/Cargo.toml @@ -24,6 +24,7 @@ circle-ci = { repository = "sfackler/rust-postgres" } with-bit-vec-0_6 = ["tokio-postgres/with-bit-vec-0_6"] with-chrono-0_4 = ["tokio-postgres/with-chrono-0_4"] with-eui48-0_4 = ["tokio-postgres/with-eui48-0_4"] +with-geo-types-0_6 = ["tokio-postgres/with-geo-types-0_6"] with-geo-types-0_7 = ["tokio-postgres/with-geo-types-0_7"] with-serde_json-1 = ["tokio-postgres/with-serde_json-1"] with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"] diff --git a/postgres/src/lib.rs b/postgres/src/lib.rs index fe0c1f6c..4513aeef 100644 --- a/postgres/src/lib.rs +++ b/postgres/src/lib.rs @@ -56,6 +56,7 @@ //! | `with-bit-vec-0_6` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.6 | no | //! | `with-chrono-0_4` | Enable support for the `chrono` crate. | [chrono](https://crates.io/crates/chrono) 0.4 | no | //! | `with-eui48-0_4` | Enable support for the `eui48` crate. | [eui48](https://crates.io/crates/eui48) 0.4 | no | +//! | `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 | //! | `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 | //! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | no | //! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no | diff --git a/tokio-postgres/Cargo.toml b/tokio-postgres/Cargo.toml index 47e37167..254ebe62 100644 --- a/tokio-postgres/Cargo.toml +++ b/tokio-postgres/Cargo.toml @@ -30,6 +30,7 @@ runtime = ["tokio/net", "tokio/time"] with-bit-vec-0_6 = ["postgres-types/with-bit-vec-0_6"] with-chrono-0_4 = ["postgres-types/with-chrono-0_4"] with-eui48-0_4 = ["postgres-types/with-eui48-0_4"] +with-geo-types-0_6 = ["postgres-types/with-geo-types-0_6"] with-geo-types-0_7 = ["postgres-types/with-geo-types-0_7"] with-serde_json-1 = ["postgres-types/with-serde_json-1"] with-uuid-0_8 = ["postgres-types/with-uuid-0_8"] @@ -60,6 +61,7 @@ criterion = "0.3" bit-vec-06 = { version = "0.6", package = "bit-vec" } chrono-04 = { version = "0.4", package = "chrono", default-features = false } eui48-04 = { version = "0.4", package = "eui48" } +geo-types-06 = { version = "0.6", package = "geo-types" } geo-types-07 = { version = "0.7", package = "geo-types" } serde-1 = { version = "1.0", package = "serde" } serde_json-1 = { version = "1.0", package = "serde_json" } diff --git a/tokio-postgres/src/lib.rs b/tokio-postgres/src/lib.rs index d62b8631..77713bb1 100644 --- a/tokio-postgres/src/lib.rs +++ b/tokio-postgres/src/lib.rs @@ -107,6 +107,7 @@ //! | `with-bit-vec-0_6` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.6 | no | //! | `with-chrono-0_4` | Enable support for the `chrono` crate. | [chrono](https://crates.io/crates/chrono) 0.4 | no | //! | `with-eui48-0_4` | Enable support for the `eui48` crate. | [eui48](https://crates.io/crates/eui48) 0.4 | no | +//! | `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 | //! | `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 | //! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | no | //! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no | diff --git a/tokio-postgres/tests/test/types/geo_types_06.rs b/tokio-postgres/tests/test/types/geo_types_06.rs new file mode 100644 index 00000000..7195abc0 --- /dev/null +++ b/tokio-postgres/tests/test/types/geo_types_06.rs @@ -0,0 +1,60 @@ +use geo_types_06::{Coordinate, LineString, Point, Rect}; + +use crate::types::test_type; + +#[tokio::test] +async fn test_point_params() { + test_type( + "POINT", + &[ + (Some(Point::new(0.0, 0.0)), "POINT(0, 0)"), + (Some(Point::new(-3.2, 1.618)), "POINT(-3.2, 1.618)"), + (None, "NULL"), + ], + ) + .await; +} + +#[tokio::test] +async fn test_box_params() { + test_type( + "BOX", + &[ + ( + Some(Rect::new( + Coordinate { x: -3.2, y: 1.618 }, + Coordinate { + x: 160.0, + y: 69701.5615, + }, + )), + "BOX(POINT(160.0, 69701.5615), POINT(-3.2, 1.618))", + ), + (None, "NULL"), + ], + ) + .await; +} + +#[tokio::test] +async fn test_path_params() { + let points = vec![ + Coordinate { x: 0., y: 0. }, + Coordinate { x: -3.2, y: 1.618 }, + Coordinate { + x: 160.0, + y: 69701.5615, + }, + ]; + test_type( + "PATH", + &[ + ( + Some(LineString(points)), + "path '((0, 0), (-3.2, 1.618), (160.0, 69701.5615))'", + ), + (None, "NULL"), + ], + ) + .await; +} diff --git a/tokio-postgres/tests/test/types/mod.rs b/tokio-postgres/tests/test/types/mod.rs index abf05829..11d12876 100644 --- a/tokio-postgres/tests/test/types/mod.rs +++ b/tokio-postgres/tests/test/types/mod.rs @@ -19,6 +19,8 @@ mod bit_vec_06; mod chrono_04; #[cfg(feature = "with-eui48-0_4")] mod eui48_04; +#[cfg(feature = "with-geo-types-0_6")] +mod geo_types_06; #[cfg(feature = "with-geo-types-0_7")] mod geo_types_07; #[cfg(feature = "with-serde_json-1")]