Skip to content

Commit 1296b19

Browse files
author
AGAEV Denis E
committed
Add geo types to PythonDTO and py_to_rust method
1 parent 0d596ed commit 1296b19

File tree

2 files changed

+147
-6
lines changed

2 files changed

+147
-6
lines changed

src/extra_types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ macro_rules! build_geo_type {
226226

227227
impl $st_name {
228228
#[must_use]
229-
pub fn retrieve_value(&self) -> &$rust_type {
230-
&self.inner
229+
pub fn retrieve_value(&self) -> $rust_type {
230+
self.inner.clone()
231231
}
232232
}
233233
};

src/value_converter.rs

Lines changed: 145 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use chrono::{self, DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime};
2-
use geo_types::{coord, Coord};
2+
use geo_types::{coord, Coord, Line, LineString, Point, Polygon, Rect};
33
use itertools::Itertools;
44
use macaddr::{MacAddr6, MacAddr8};
55
use postgres_types::{Field, FromSql, Kind};
@@ -22,11 +22,11 @@ use tokio_postgres::{
2222
};
2323

2424
use crate::{
25-
additional_types::{Circle, RustMacAddr6, RustMacAddr8},
25+
additional_types::{Circle, RustLine, RustMacAddr6, RustMacAddr8, RustPolygon},
2626
exceptions::rust_errors::{RustPSQLDriverError, RustPSQLDriverPyResult},
2727
extra_types::{
28-
BigInt, Integer, PyCustomType, PyJSON, PyJSONB, PyMacAddr6, PyMacAddr8, PyText, PyUUID,
29-
PyVarChar, SmallInt,
28+
BigInt, Integer, PyBox, PyCircle, PyCustomType, PyJSON, PyJSONB, PyLine, PyLineSegment,
29+
PyMacAddr6, PyMacAddr8, PyPath, PyPoint, PyPolygon, PyText, PyUUID, PyVarChar, SmallInt,
3030
},
3131
};
3232

@@ -65,6 +65,13 @@ pub enum PythonDTO {
6565
PyMacAddr6(MacAddr6),
6666
PyMacAddr8(MacAddr8),
6767
PyCustomType(Vec<u8>),
68+
PyPoint(Point),
69+
PyBox(Rect),
70+
PyPath(LineString),
71+
PyLine(Line),
72+
PyLineSegment(Line),
73+
PyPolygon(Polygon),
74+
PyCircle(Circle),
6875
}
6976

7077
impl PythonDTO {
@@ -100,6 +107,13 @@ impl PythonDTO {
100107
PythonDTO::PyDateTimeTz(_) => Ok(tokio_postgres::types::Type::TIMESTAMPTZ_ARRAY),
101108
PythonDTO::PyMacAddr6(_) => Ok(tokio_postgres::types::Type::MACADDR_ARRAY),
102109
PythonDTO::PyMacAddr8(_) => Ok(tokio_postgres::types::Type::MACADDR8_ARRAY),
110+
PythonDTO::PyPoint(_) => Ok(tokio_postgres::types::Type::POINT_ARRAY),
111+
PythonDTO::PyBox(_) => Ok(tokio_postgres::types::Type::BOX_ARRAY),
112+
PythonDTO::PyPath(_) => Ok(tokio_postgres::types::Type::PATH_ARRAY),
113+
PythonDTO::PyLine(_) => Ok(tokio_postgres::types::Type::LINE_ARRAY),
114+
PythonDTO::PyLineSegment(_) => Ok(tokio_postgres::types::Type::LSEG_ARRAY),
115+
PythonDTO::PyPolygon(_) => Ok(tokio_postgres::types::Type::POLYGON_ARRAY),
116+
PythonDTO::PyCircle(_) => Ok(tokio_postgres::types::Type::CIRCLE_ARRAY),
103117
_ => Err(RustPSQLDriverError::PyToRustValueConversionError(
104118
"Can't process array type, your type doesn't have support yet".into(),
105119
)),
@@ -224,6 +238,27 @@ impl ToSql for PythonDTO {
224238
PythonDTO::PyMacAddr8(pymacaddr) => {
225239
<&[u8] as ToSql>::to_sql(&pymacaddr.as_bytes(), ty, out)?;
226240
}
241+
PythonDTO::PyPoint(pypoint) => {
242+
<&Point as ToSql>::to_sql(&pypoint, ty, out)?;
243+
}
244+
PythonDTO::PyBox(pybox) => {
245+
<&Rect as ToSql>::to_sql(&pybox, ty, out)?;
246+
}
247+
PythonDTO::PyPath(pypath) => {
248+
<&LineString as ToSql>::to_sql(&pypath, ty, out)?;
249+
}
250+
PythonDTO::PyLine(pyline) => {
251+
<&RustLine as ToSql>::to_sql(&&RustLine::new(*pyline), ty, out)?;
252+
}
253+
PythonDTO::PyLineSegment(pylinesegment) => {
254+
<&RustLine as ToSql>::to_sql(&&RustLine::new(*pylinesegment), ty, out)?;
255+
}
256+
PythonDTO::PyPolygon(pypolygon) => {
257+
<&RustPolygon as ToSql>::to_sql(&&RustPolygon::new(pypolygon.clone()), ty, out)?;
258+
}
259+
PythonDTO::PyCircle(pycircle) => {
260+
<&Circle as ToSql>::to_sql(&pycircle, ty, out)?;
261+
}
227262
PythonDTO::PyList(py_iterable) | PythonDTO::PyTuple(py_iterable) => {
228263
let mut items = Vec::new();
229264
for inner in py_iterable {
@@ -431,6 +466,48 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<
431466
));
432467
}
433468

469+
if parameter.is_instance_of::<PyPoint>() {
470+
return Ok(PythonDTO::PyPoint(
471+
parameter.extract::<PyPoint>()?.retrieve_value(),
472+
));
473+
}
474+
475+
if parameter.is_instance_of::<PyBox>() {
476+
return Ok(PythonDTO::PyBox(
477+
parameter.extract::<PyBox>()?.retrieve_value(),
478+
));
479+
}
480+
481+
if parameter.is_instance_of::<PyPath>() {
482+
return Ok(PythonDTO::PyPath(
483+
parameter.extract::<PyPath>()?.retrieve_value(),
484+
));
485+
}
486+
487+
if parameter.is_instance_of::<PyLine>() {
488+
return Ok(PythonDTO::PyLine(
489+
parameter.extract::<PyLine>()?.retrieve_value(),
490+
));
491+
}
492+
493+
if parameter.is_instance_of::<PyLineSegment>() {
494+
return Ok(PythonDTO::PyLineSegment(
495+
parameter.extract::<PyLineSegment>()?.retrieve_value(),
496+
));
497+
}
498+
499+
if parameter.is_instance_of::<PyPolygon>() {
500+
return Ok(PythonDTO::PyPolygon(
501+
parameter.extract::<PyPolygon>()?.retrieve_value(),
502+
));
503+
}
504+
505+
if parameter.is_instance_of::<PyCircle>() {
506+
return Ok(PythonDTO::PyCircle(
507+
parameter.extract::<PyCircle>()?.retrieve_value(),
508+
));
509+
}
510+
434511
if let Ok(id_address) = parameter.extract::<IpAddr>() {
435512
return Ok(PythonDTO::PyIpAddress(id_address));
436513
}
@@ -571,6 +648,70 @@ fn postgres_bytes_to_py(
571648
Ok(py.None().to_object(py))
572649
}
573650
}
651+
// ---------- Geo Types ----------
652+
// Type::POINT => {
653+
// let point_ =
654+
// _composite_field_postgres_to_py::<Option<Point>>(type_, buf, is_simple)?;
655+
// if let Some(point_) = point_ {
656+
// Ok(point_.to_object(py))
657+
// } else {
658+
// Ok(py.None().to_object(py))
659+
// }
660+
// }
661+
// Type::BOX => {
662+
// let box_ =
663+
// _composite_field_postgres_to_py::<Option<Rect>>(type_, buf, is_simple)?;
664+
// if let Some(box_) = box_ {
665+
// Ok(macaddr_.inner().to_string().to_object(py))
666+
// } else {
667+
// Ok(py.None().to_object(py))
668+
// }
669+
// }
670+
// Type::PATH => {
671+
// let path_ =
672+
// _composite_field_postgres_to_py::<Option<LineString>>(type_, buf, is_simple)?;
673+
// if let Some(path_) = path_ {
674+
// Ok(macaddr_.inner().to_string().to_object(py))
675+
// } else {
676+
// Ok(py.None().to_object(py))
677+
// }
678+
// }
679+
// Type::LINE => {
680+
// let line_ =
681+
// _composite_field_postgres_to_py::<Option<RustLine>>(type_, buf, is_simple)?;
682+
// if let Some(line_) = line_ {
683+
// Ok(macaddr_.inner().to_string().to_object(py))
684+
// } else {
685+
// Ok(py.None().to_object(py))
686+
// }
687+
// }
688+
// Type::LSEG => {
689+
// let line_string_ =
690+
// _composite_field_postgres_to_py::<Option<RustLine>>(type_, buf, is_simple)?;
691+
// if let Some(line_string_) = line_string_ {
692+
// Ok(macaddr_.inner().to_string().to_object(py))
693+
// } else {
694+
// Ok(py.None().to_object(py))
695+
// }
696+
// }
697+
// Type::POLYGON => {
698+
// let polygon_ =
699+
// _composite_field_postgres_to_py::<Option<RustPolygon>>(type_, buf, is_simple)?;
700+
// if let Some(polygon_) = polygon_ {
701+
// Ok(macaddr_.inner().to_string().to_object(py))
702+
// } else {
703+
// Ok(py.None().to_object(py))
704+
// }
705+
// }
706+
// Type::CIRCLE => {
707+
// let circle_ =
708+
// _composite_field_postgres_to_py::<Option<Circle>>(type_, buf, is_simple)?;
709+
// if let Some(circle_) = circle_ {
710+
// Ok(macaddr_.inner().to_string().to_object(py))
711+
// } else {
712+
// Ok(py.None().to_object(py))
713+
// }
714+
// }
574715
// ---------- Array Text Types ----------
575716
Type::BOOL_ARRAY => Ok(_composite_field_postgres_to_py::<Option<Vec<bool>>>(
576717
type_, buf, is_simple,

0 commit comments

Comments
 (0)