|
| 1 | +extern crate geo; |
| 2 | + |
| 3 | +use postgres_protocol::types; |
| 4 | +use self::geo::{Bbox, Point}; |
| 5 | +use std::error::Error; |
| 6 | + |
| 7 | +use types::{FromSql, ToSql, IsNull, Type}; |
| 8 | + |
| 9 | +impl FromSql for Point<f64> { |
| 10 | + fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<Error + Sync + Send>> { |
| 11 | + if raw.len() != 16 { |
| 12 | + return Err("invalid message length".into()); |
| 13 | + } |
| 14 | + |
| 15 | + let x = types::float8_from_sql(&raw[0..8])?; |
| 16 | + let y = types::float8_from_sql(&raw[8..16])?; |
| 17 | + Ok(Point::new(x, y)) |
| 18 | + } |
| 19 | + |
| 20 | + accepts!(Type::Point); |
| 21 | +} |
| 22 | + |
| 23 | +impl ToSql for Point<f64> { |
| 24 | + fn to_sql(&self, _: &Type, out: &mut Vec<u8>) -> Result<IsNull, Box<Error + Sync + Send>> { |
| 25 | + types::float8_to_sql(self.x(), out); |
| 26 | + types::float8_to_sql(self.y(), out); |
| 27 | + Ok(IsNull::No) |
| 28 | + } |
| 29 | + |
| 30 | + accepts!(Type::Point); |
| 31 | + to_sql_checked!(); |
| 32 | +} |
| 33 | + |
| 34 | +impl FromSql for Bbox<f64> { |
| 35 | + fn from_sql(_: &Type, raw: &[u8]) -> Result<Self, Box<Error + Sync + Send>> { |
| 36 | + if raw.len() != 32 { |
| 37 | + return Err("invalid message length".into()); |
| 38 | + } |
| 39 | + |
| 40 | + let xmax = types::float8_from_sql(&raw[0..8])?; |
| 41 | + let ymax = types::float8_from_sql(&raw[8..16])?; |
| 42 | + let xmin = types::float8_from_sql(&raw[16..24])?; |
| 43 | + let ymin = types::float8_from_sql(&raw[24..32])?; |
| 44 | + Ok(Bbox{xmax: xmax, ymax: ymax, xmin: xmin, ymin: ymin}) |
| 45 | + } |
| 46 | + |
| 47 | + accepts!(Type::Box); |
| 48 | +} |
| 49 | + |
| 50 | +impl ToSql for Bbox<f64> { |
| 51 | + fn to_sql(&self, _: &Type, out: &mut Vec<u8>) -> Result<IsNull, Box<Error + Sync + Send>> { |
| 52 | + types::float8_to_sql(self.xmax, out); |
| 53 | + types::float8_to_sql(self.ymax, out); |
| 54 | + types::float8_to_sql(self.xmin, out); |
| 55 | + types::float8_to_sql(self.ymin, out); |
| 56 | + Ok(IsNull::No) |
| 57 | + } |
| 58 | + |
| 59 | + accepts!(Type::Box); |
| 60 | + to_sql_checked!(); |
| 61 | +} |
0 commit comments