Add support for geo-types=0.5

Support for geo-types=0.4 (via the `with-geo-types_04` feature) has been preserved for convenience.
This commit is contained in:
Jakub Wieczorek 2020-05-25 12:08:17 +02:00
parent 9fe4eee36f
commit 887be86816
7 changed files with 83 additions and 2 deletions

View File

@ -16,6 +16,7 @@ with-bit-vec-0_6 = ["bit-vec-06"]
with-chrono-0_4 = ["chrono-04"] with-chrono-0_4 = ["chrono-04"]
with-eui48-0_4 = ["eui48-04"] with-eui48-0_4 = ["eui48-04"]
with-geo-types-0_4 = ["geo-types-04"] with-geo-types-0_4 = ["geo-types-04"]
with-geo-types-0_5 = ["geo-types-05"]
with-serde_json-1 = ["serde-1", "serde_json-1"] with-serde_json-1 = ["serde-1", "serde_json-1"]
with-uuid-0_8 = ["uuid-08"] with-uuid-0_8 = ["uuid-08"]
with-time-0_2 = ["time-02"] with-time-0_2 = ["time-02"]
@ -30,6 +31,7 @@ bit-vec-06 = { version = "0.6", package = "bit-vec", optional = true }
chrono-04 = { version = "0.4", package = "chrono", optional = true } chrono-04 = { version = "0.4", package = "chrono", optional = true }
eui48-04 = { version = "0.4", package = "eui48", optional = true } eui48-04 = { version = "0.4", package = "eui48", optional = true }
geo-types-04 = { version = "0.4", package = "geo-types", optional = true } geo-types-04 = { version = "0.4", package = "geo-types", optional = true }
geo-types-05 = { version = "0.5", package = "geo-types", optional = true }
serde-1 = { version = "1.0", package = "serde", optional = true } serde-1 = { version = "1.0", package = "serde", optional = true }
serde_json-1 = { version = "1.0", package = "serde_json", optional = true } serde_json-1 = { version = "1.0", package = "serde_json", optional = true }
uuid-08 = { version = "0.8", package = "uuid", optional = true } uuid-08 = { version = "0.8", package = "uuid", optional = true }

View File

@ -0,0 +1,72 @@
use bytes::BytesMut;
use fallible_iterator::FallibleIterator;
use geo_types_05::{Coordinate, LineString, Point, Rect};
use postgres_protocol::types;
use std::error::Error;
use crate::{FromSql, IsNull, ToSql, Type};
impl<'a> FromSql<'a> for Point<f64> {
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
let point = types::point_from_sql(raw)?;
Ok(Point::new(point.x(), point.y()))
}
accepts!(POINT);
}
impl ToSql for Point<f64> {
fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
types::point_to_sql(self.x(), self.y(), out);
Ok(IsNull::No)
}
accepts!(POINT);
to_sql_checked!();
}
impl<'a> FromSql<'a> for Rect<f64> {
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
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<f64> {
fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
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<f64> {
fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<dyn Error + Sync + Send>> {
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<f64> {
fn to_sql(&self, _: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
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!();
}

View File

@ -196,6 +196,8 @@ mod chrono_04;
mod eui48_04; mod eui48_04;
#[cfg(feature = "with-geo-types-0_4")] #[cfg(feature = "with-geo-types-0_4")]
mod geo_types_04; mod geo_types_04;
#[cfg(feature = "with-geo-types-0_5")]
mod geo_types_05;
#[cfg(feature = "with-serde_json-1")] #[cfg(feature = "with-serde_json-1")]
mod serde_json_1; mod serde_json_1;
#[cfg(feature = "with-time-0_2")] #[cfg(feature = "with-time-0_2")]

View File

@ -25,6 +25,7 @@ with-bit-vec-0_6 = ["tokio-postgres/with-bit-vec-0_6"]
with-chrono-0_4 = ["tokio-postgres/with-chrono-0_4"] with-chrono-0_4 = ["tokio-postgres/with-chrono-0_4"]
with-eui48-0_4 = ["tokio-postgres/with-eui48-0_4"] with-eui48-0_4 = ["tokio-postgres/with-eui48-0_4"]
with-geo-types-0_4 = ["tokio-postgres/with-geo-types-0_4"] with-geo-types-0_4 = ["tokio-postgres/with-geo-types-0_4"]
with-geo-types-0_5 = ["tokio-postgres/with-geo-types-0_5"]
with-serde_json-1 = ["tokio-postgres/with-serde_json-1"] with-serde_json-1 = ["tokio-postgres/with-serde_json-1"]
with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"] with-uuid-0_8 = ["tokio-postgres/with-uuid-0_8"]
with-time-0_2 = ["tokio-postgres/with-time-0_2"] with-time-0_2 = ["tokio-postgres/with-time-0_2"]

View File

@ -56,7 +56,8 @@
//! | `with-bit-vec-0_6` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.6 | no | //! | `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-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-eui48-0_4` | Enable support for the `eui48` crate. | [eui48](https://crates.io/crates/eui48) 0.4 | no |
//! | `with-geo-types-0_4` | Enable support for the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types) 0.4 | no | //! | `with-geo-types-0_4` | Enable support for the 0.4 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.4.0) 0.4 | no |
//! | `with-geo-types-0_5` | Enable support for the 0.5 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.5.0) 0.5 | no |
//! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | 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 | //! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no |
//! | `with-time-0_2` | Enable support for the `time` crate. | [time](https://crates.io/crates/time) 0.2 | no | //! | `with-time-0_2` | Enable support for the `time` crate. | [time](https://crates.io/crates/time) 0.2 | no |

View File

@ -31,6 +31,7 @@ with-bit-vec-0_6 = ["postgres-types/with-bit-vec-0_6"]
with-chrono-0_4 = ["postgres-types/with-chrono-0_4"] with-chrono-0_4 = ["postgres-types/with-chrono-0_4"]
with-eui48-0_4 = ["postgres-types/with-eui48-0_4"] with-eui48-0_4 = ["postgres-types/with-eui48-0_4"]
with-geo-types-0_4 = ["postgres-types/with-geo-types-0_4"] with-geo-types-0_4 = ["postgres-types/with-geo-types-0_4"]
with-geo-types-0_5 = ["postgres-types/with-geo-types-0_5"]
with-serde_json-1 = ["postgres-types/with-serde_json-1"] with-serde_json-1 = ["postgres-types/with-serde_json-1"]
with-uuid-0_8 = ["postgres-types/with-uuid-0_8"] with-uuid-0_8 = ["postgres-types/with-uuid-0_8"]
with-time-0_2 = ["postgres-types/with-time-0_2"] with-time-0_2 = ["postgres-types/with-time-0_2"]
@ -60,6 +61,7 @@ bit-vec-06 = { version = "0.6", package = "bit-vec" }
chrono-04 = { version = "0.4", package = "chrono" } chrono-04 = { version = "0.4", package = "chrono" }
eui48-04 = { version = "0.4", package = "eui48" } eui48-04 = { version = "0.4", package = "eui48" }
geo-types-04 = { version = "0.4", package = "geo-types" } geo-types-04 = { version = "0.4", package = "geo-types" }
geo-types-05 = { version = "0.5", package = "geo-types" }
serde-1 = { version = "1.0", package = "serde" } serde-1 = { version = "1.0", package = "serde" }
serde_json-1 = { version = "1.0", package = "serde_json" } serde_json-1 = { version = "1.0", package = "serde_json" }
uuid-08 = { version = "0.8", package = "uuid" } uuid-08 = { version = "0.8", package = "uuid" }

View File

@ -107,7 +107,8 @@
//! | `with-bit-vec-0_6` | Enable support for the `bit-vec` crate. | [bit-vec](https://crates.io/crates/bit-vec) 0.6 | no | //! | `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-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-eui48-0_4` | Enable support for the `eui48` crate. | [eui48](https://crates.io/crates/eui48) 0.4 | no |
//! | `with-geo-types-0_4` | Enable support for the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types) 0.4 | no | //! | `with-geo-types-0_4` | Enable support for the 0.4 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.4.0) 0.4 | no |
//! | `with-geo-types-0_5` | Enable support for the 0.5 version of the `geo-types` crate. | [geo-types](https://crates.io/crates/geo-types/0.5.0) 0.5 | no |
//! | `with-serde_json-1` | Enable support for the `serde_json` crate. | [serde_json](https://crates.io/crates/serde_json) 1.0 | 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 | //! | `with-uuid-0_8` | Enable support for the `uuid` crate. | [uuid](https://crates.io/crates/uuid) 0.8 | no |
//! | `with-time-0_2` | Enable support for the `time` crate. | [time](https://crates.io/crates/time) 0.2 | no | //! | `with-time-0_2` | Enable support for the `time` crate. | [time](https://crates.io/crates/time) 0.2 | no |