|
| 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 | +} |
0 commit comments