Skip to content

Commit d438483

Browse files
author
AGAEV Denis E
committed
Update
1 parent 1d20769 commit d438483

File tree

4 files changed

+94
-68
lines changed

4 files changed

+94
-68
lines changed

Cargo.lock

Lines changed: 32 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ postgres-types = { git = "https://github.com/chandr-andr/rust-postgres.git", ver
3636
"derive",
3737
] }
3838
postgres-protocol = { git = "https://github.com/chandr-andr/rust-postgres.git", version = "*" }
39+
itertools = "0.12.1"

src/additional_types.rs

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use std::{f64::consts::PI, fmt::Debug};
2-
3-
use macaddr::{MacAddr6, MacAddr8};
41
use itertools::Itertools;
5-
use geo_types::{coord, Coord, CoordNum, Line, Polygon};
2+
use macaddr::{MacAddr6, MacAddr8};
3+
use geo_types::{coord, Coord, CoordFloat, CoordNum, Line, LineString, Polygon};
64
use byteorder::{ReadBytesExt, BigEndian};
75
use tokio_postgres::types::{FromSql, Type};
86

@@ -75,13 +73,13 @@ impl<'a> FromSql<'a> for RustLine {
7573
raw: &'a [u8],
7674
) -> Result<RustLine, Box<dyn std::error::Error + Sync + Send>> {
7775
if raw.len() == 4 {
78-
let mut vec_raw: Vec<u8> = vec![];
76+
let mut vec_raw= vec![];
7977
vec_raw.extend_from_slice(raw);
80-
let mut buf: &[u8] = vec_raw.as_slice();
78+
let mut buf = vec_raw.as_slice();
8179

8280
let x1 = buf.read_f64::<BigEndian>()?;
8381
let y1 = buf.read_f64::<BigEndian>()?;
84-
let first_coord: Coord<f64> = coord!(x: x1, y: y1);
82+
let first_coord = coord!(x: x1, y: y1);
8583

8684
let x2 = buf.read_f64::<BigEndian>()?;
8785
let y2 = buf.read_f64::<BigEndian>()?;
@@ -98,34 +96,40 @@ impl<'a> FromSql<'a> for RustLine {
9896
}
9997
}
10098

101-
// impl<'a> FromSql<'a> for RustPolygon {
102-
// fn from_sql(
103-
// _ty: &Type,
104-
// raw: &'a [u8],
105-
// ) -> Result<RustPolygon, Box<dyn std::error::Error + Sync + Send>> {
106-
// if raw.len() % 2 == 0 {
107-
// let mut vec_raw: Vec<u8> = vec![];
108-
// vec_raw.extend_from_slice(raw);
99+
impl<'a> FromSql<'a> for RustPolygon {
100+
fn from_sql(
101+
_ty: &Type,
102+
raw: &'a [u8],
103+
) -> Result<RustPolygon, Box<dyn std::error::Error + Sync + Send>> {
104+
if raw.len() % 2 == 0 {
105+
let mut vec_raw = vec![];
106+
vec_raw.extend_from_slice(raw);
107+
let mut buf = vec_raw.as_slice();
108+
109+
let mut vec_raw_coord = vec![];
110+
buf.read_f64_into::<BigEndian>(&mut vec_raw_coord);
109111

110-
// for (x, y) in vec_raw.tuple_windows() {
111-
// let coord = coord!(x: x, y: y);
112-
// }
112+
let mut vec_coord = vec![];
113+
for (x1, y1) in vec_raw_coord.into_iter().tuples() {
114+
vec_coord.push(coord!(x: x1, y: y1));
115+
}
113116

114-
// let new_polygon = Polygon::new(exterior, interiors)
115-
// return Ok(RustPolygon::new(new_polygon));
116-
// }
117-
// Err("Cannot convert PostgreSQL POLYGON into rust Polygon".into())
118-
// }
117+
let polygon_exterior = LineString::new(vec_coord);
118+
let new_polygon = Polygon::new(polygon_exterior, vec![]);
119+
return Ok(RustPolygon::new(new_polygon));
120+
}
121+
Err("Cannot convert PostgreSQL POLYGON into rust Polygon".into())
122+
}
119123

120-
// fn accepts(_ty: &Type) -> bool {
121-
// true
122-
// }
123-
// }
124+
fn accepts(_ty: &Type) -> bool {
125+
true
126+
}
127+
}
124128

125129

126130
// add macro for creating circles
127131

128-
#[derive(Eq, PartialEq, Clone, Copy, Debug,Hash)]
132+
#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)]
129133
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
130134
pub struct Circle<T: CoordNum = f64> {
131135
center: Coord<T>,
@@ -134,7 +138,7 @@ pub struct Circle<T: CoordNum = f64> {
134138

135139
impl<T: CoordNum> Circle<T> {
136140
pub fn new(x: T, y: T, r: T) -> Self {
137-
Self {center: Coord::new(x, y), radius: r}
141+
Self {center: coord!(x: x, y: y), radius: r}
138142
}
139143

140144
pub fn center(self) -> Coord<T> {
@@ -164,27 +168,25 @@ impl<T: CoordNum> Circle<T> {
164168
}
165169
}
166170

167-
impl<T: CoordNum> Circle<T> {
168-
pub fn area(self) -> T {
169-
PI * self.radius * self.radius
170-
}
171-
pub fn perimeter(self) -> T {
172-
2.0 * PI * self.radius
171+
impl<T: CoordFloat> Circle<T> {
172+
pub fn distance_from_center_to(self, point: &Coord<T>) -> T {
173+
let dx = self.center.x - point.x;
174+
let dy = self.center.y - point.y;
175+
dx.hypot(dy)
173176
}
174177

175178
pub fn contains(self, point: &Coord<T>) -> bool {
176-
self.center.distance_to(point) <= self.radius
179+
self.distance_from_center_to(&point) <= self.radius
177180
}
178181

179182
pub fn intersects(self, other: &Self) -> bool {
180-
self.center.distance_to(&other.center) <= self.radius + other.radius
183+
self.distance_from_center_to(&other.center) <= self.radius + other.radius
181184
}
182185
}
183186

184187
impl<T: CoordNum> Default for Circle<T> {
185188
fn default() -> Self {
186-
let default_center = Coord::default();
187-
Self {center: default_center, radius: 0.0}
189+
Self {center: coord! {x: T::zero(), y: T::zero()}, radius: T::zero()}
188190
}
189191
}
190192

@@ -194,9 +196,9 @@ impl<'a> FromSql<'a> for Circle {
194196
raw: &'a [u8],
195197
) -> Result<Circle, Box<dyn std::error::Error + Sync + Send>> {
196198
if raw.len() == 3 {
197-
let mut vec_raw: Vec<u8> = vec![];
199+
let mut vec_raw = vec![];
198200
vec_raw.extend_from_slice(raw);
199-
let mut buf: &[u8] = vec_raw.as_slice();
201+
let mut buf = vec_raw.as_slice();
200202

201203
let x = buf.read_f64::<BigEndian>()?;
202204
let y = buf.read_f64::<BigEndian>()?;

src/extra_types.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ use geo_types::{Point, Rect, Line, LineString, Polygon};
1010
use serde_json::Value;
1111
use uuid::Uuid;
1212

13-
use crate::{exceptions::rust_errors::RustPSQLDriverPyResult, value_converter::build_serde_value};
13+
use crate::{
14+
additional_types::Circle,
15+
exceptions::rust_errors::RustPSQLDriverPyResult,
16+
value_converter::{
17+
build_serde_value,
18+
build_point,
19+
}
20+
};
1421

1522
macro_rules! build_python_type {
1623
($st_name:ident, $rust_type:ty) => {
@@ -214,15 +221,15 @@ build_geo_type!(PyPath, LineString);
214221
build_geo_type!(PyLine, Line);
215222
build_geo_type!(PyLineSegment, Line);
216223
build_geo_type!(PyPolygon, Polygon);
217-
// build_geo_type!(PyCircle, );
224+
build_geo_type!(PyCircle, Circle);
218225

219226
// #[pymethods]
220227
// impl PyPoint {
221228
// #[new]
222229
// #[allow(clippy::missing_errors_doc)]
223-
// pub fn new_point(value: &PyAny) -> RustPSQLDriverPyResult<Self> {
230+
// pub fn new_point(value: Py<PyAny>) -> RustPSQLDriverPyResult<Self> {
224231
// Ok(Self {
225-
// inner: build_serde_value(value)?,
232+
// inner: build_point(value)?,
226233
// })
227234
// }
228235
// }
@@ -231,7 +238,7 @@ build_geo_type!(PyPolygon, Polygon);
231238
// impl PyBox {
232239
// #[new]
233240
// #[allow(clippy::missing_errors_doc)]
234-
// pub fn new_box(value: &PyAny) -> RustPSQLDriverPyResult<Self> {
241+
// pub fn new_box(value: Py<PyAny>) -> RustPSQLDriverPyResult<Self> {
235242
// Ok(Self {
236243
// inner: build_serde_value(value)?,
237244
// })
@@ -242,7 +249,7 @@ build_geo_type!(PyPolygon, Polygon);
242249
// impl PyPath {
243250
// #[new]
244251
// #[allow(clippy::missing_errors_doc)]
245-
// pub fn new_path(value: &PyAny) -> RustPSQLDriverPyResult<Self> {
252+
// pub fn new_path(value: Py<PyAny>) -> RustPSQLDriverPyResult<Self> {
246253
// Ok(Self {
247254
// inner: build_serde_value(value)?,
248255
// })
@@ -253,7 +260,7 @@ build_geo_type!(PyPolygon, Polygon);
253260
// impl PyLine {
254261
// #[new]
255262
// #[allow(clippy::missing_errors_doc)]
256-
// pub fn new_line(value: &PyAny) -> RustPSQLDriverPyResult<Self> {
263+
// pub fn new_line(value: Py<PyAny>) -> RustPSQLDriverPyResult<Self> {
257264
// Ok(Self {
258265
// inner: build_serde_value(value)?,
259266
// })
@@ -264,7 +271,7 @@ build_geo_type!(PyPolygon, Polygon);
264271
// impl PyLineSegment {
265272
// #[new]
266273
// #[allow(clippy::missing_errors_doc)]
267-
// pub fn new_line_segment(value: &PyAny) -> RustPSQLDriverPyResult<Self> {
274+
// pub fn new_line_segment(value: Py<PyAny>) -> RustPSQLDriverPyResult<Self> {
268275
// Ok(Self {
269276
// inner: build_serde_value(value)?,
270277
// })
@@ -275,7 +282,7 @@ build_geo_type!(PyPolygon, Polygon);
275282
// impl PyPolygon {
276283
// #[new]
277284
// #[allow(clippy::missing_errors_doc)]
278-
// pub fn new_polygon(value: &PyAny) -> RustPSQLDriverPyResult<Self> {
285+
// pub fn new_polygon(value: Py<PyAny>) -> RustPSQLDriverPyResult<Self> {
279286
// Ok(Self {
280287
// inner: build_serde_value(value)?,
281288
// })
@@ -286,7 +293,7 @@ build_geo_type!(PyPolygon, Polygon);
286293
// impl PyCircle {
287294
// #[new]
288295
// #[allow(clippy::missing_errors_doc)]
289-
// pub fn new_circle(value: &PyAny) -> RustPSQLDriverPyResult<Self> {
296+
// pub fn new_circle(value: Py<PyAny>) -> RustPSQLDriverPyResult<Self> {
290297
// Ok(Self {
291298
// inner: build_serde_value(value)?,
292299
// })
@@ -312,6 +319,6 @@ pub fn extra_types_module(_py: Python<'_>, pymod: &Bound<'_, PyModule>) -> PyRes
312319
pymod.add_class::<PyLine>()?;
313320
pymod.add_class::<PyLineSegment>()?;
314321
pymod.add_class::<PyPolygon>()?;
315-
// pymod.add_class::<PyCircle>()?;
322+
pymod.add_class::<PyCircle>()?;
316323
Ok(())
317324
}

0 commit comments

Comments
 (0)