Skip to content

Commit 3a4c7b5

Browse files
author
AGAEV Denis E
committed
Add base geo types
1 parent f552001 commit 3a4c7b5

File tree

6 files changed

+188
-0
lines changed

6 files changed

+188
-0
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tokio-postgres = { version = "0.7.10", features = [
1717
"array-impls",
1818
"with-chrono-0_4",
1919
"with-uuid-1",
20+
"with-geo-types-0_7",
2021
] }
2122
pyo3-asyncio = { version = "0.19.0", features = ["tokio-runtime"] }
2223
thiserror = "1.0.56"
@@ -29,3 +30,4 @@ uuid = { version = "1.7.0", features = ["v4"] }
2930
serde_json = "1.0.113"
3031
futures-util = "0.3.30"
3132
macaddr = "1.0.1"
33+
geo-types = "0.7.13"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from typing import Sequence, Union
2+
3+
from typing_extensions import Self
4+
5+
class PyPoint:
6+
"""Represent point field in PostgreSQL and Point in Rust."""
7+
8+
def __init__(
9+
self: Self,
10+
value: Sequence[float],
11+
) -> None:
12+
"""Create new instance of PyPoint.
13+
14+
It accepts any sequence of two float numbers.
15+
16+
### Parameters:
17+
- `value`: sequence of two float numbers.
18+
"""
19+
20+
class PyBox:
21+
"""Represent box field in PostgreSQL and Rect in Rust."""
22+
23+
def __init__(
24+
self: Self,
25+
value: Union[
26+
Sequence[Sequence[float]],
27+
Sequence[float],
28+
],
29+
) -> None:
30+
"""Create new instance of PyBox.
31+
32+
You need to pass any of this structures:
33+
- sequence of two sequences, each with two float numbers
34+
- sequence of four float
35+
36+
### Parameters:
37+
- `value`: any valid sequence with four float numbers.
38+
"""
39+
40+
class PyPath:
41+
"""Represent path field in PostgreSQL and LineString in Rust."""
42+
43+
def __init__(
44+
self: Self,
45+
value: Union[
46+
Sequence[Sequence[float]],
47+
Sequence[float],
48+
],
49+
) -> None:
50+
"""Create new instance of PyPath.
51+
52+
You need to pass any of this structures:
53+
- sequence of sequences, each with two float numbers
54+
- sequence of float numbers which amount must be a multiple of two
55+
56+
### Parameters:
57+
- `value`: any valid structure with float numbers.
58+
"""

python/psqlpy/geometric_types.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from ._internal.geometric_types import (
2+
PyPoint,
3+
PyBox,
4+
PyPath,
5+
)
6+
7+
8+
__all__ = [
9+
"PyPoint",
10+
"PyBox",
11+
"PyPath",
12+
]

src/geometric_types.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
use geo_types::{Point, Rect, LineString};
2+
use pyo3::{pyclass, pymethods, types::PyModule, PyAny, PyResult, Python};
3+
4+
// use crate::{exceptions::rust_errors::RustPSQLDriverPyResult, value_converter::build_serde_value};
5+
6+
#[pyclass]
7+
#[derive(Clone)]
8+
pub struct PyPoint {
9+
inner: Point,
10+
}
11+
12+
impl PyPoint {
13+
#[must_use]
14+
pub fn inner(&self) -> &Point {
15+
&self.inner
16+
}
17+
}
18+
19+
// #[pymethods]
20+
// impl PyPoint {
21+
// #[new]
22+
// #[allow(clippy::missing_errors_doc)]
23+
// pub fn new_point(value: &PyAny) -> RustPSQLDriverPyResult<Self> {
24+
// Ok(Self {
25+
// inner: build_serde_value(value)?,
26+
// })
27+
// }
28+
// }
29+
30+
#[pyclass]
31+
#[derive(Clone)]
32+
pub struct PyBox {
33+
inner: Rect,
34+
}
35+
36+
impl PyBox {
37+
#[must_use]
38+
pub fn inner(&self) -> &Rect {
39+
&self.inner
40+
}
41+
}
42+
43+
// #[pymethods]
44+
// impl PyBox {
45+
// #[new]
46+
// #[allow(clippy::missing_errors_doc)]
47+
// pub fn new_point(value: &PyAny) -> RustPSQLDriverPyResult<Self> {
48+
// Ok(Self {
49+
// inner: build_serde_value(value)?,
50+
// })
51+
// }
52+
// }
53+
54+
#[pyclass]
55+
#[derive(Clone)]
56+
pub struct PyPath {
57+
inner: LineString,
58+
}
59+
60+
impl PyPath {
61+
#[must_use]
62+
pub fn inner(&self) -> &LineString {
63+
&self.inner
64+
}
65+
}
66+
67+
// #[pymethods]
68+
// impl PyPath {
69+
// #[new]
70+
// #[allow(clippy::missing_errors_doc)]
71+
// pub fn new_point(value: &PyAny) -> RustPSQLDriverPyResult<Self> {
72+
// Ok(Self {
73+
// inner: build_serde_value(value)?,
74+
// })
75+
// }
76+
// }
77+
78+
#[allow(clippy::module_name_repetitions)]
79+
#[allow(clippy::missing_errors_doc)]
80+
pub fn geometric_types_module(_py: Python<'_>, pymod: &PyModule) -> PyResult<()> {
81+
pymod.add_class::<PyPoint>()?;
82+
pymod.add_class::<PyBox>()?;
83+
pymod.add_class::<PyPath>()?;
84+
Ok(())
85+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub mod query_result;
77
pub mod value_converter;
88

99
use common::add_module;
10+
use geometric_types::geometric_types_module;
1011
use exceptions::python_errors::python_exceptions_module;
1112
use extra_types::extra_types_module;
1213
use pyo3::{pymodule, types::PyModule, PyResult, Python};
@@ -23,6 +24,7 @@ fn psqlpy(py: Python<'_>, pymod: &PyModule) -> PyResult<()> {
2324
pymod.add_class::<driver::common_options::ConnRecyclingMethod>()?;
2425
pymod.add_class::<query_result::PSQLDriverPyQueryResult>()?;
2526
add_module(py, pymod, "extra_types", extra_types_module)?;
27+
add_module(py, pymod, "geometric_types", geometric_types_module)?;
2628
add_module(py, pymod, "exceptions", python_exceptions_module)?;
2729
Ok(())
2830
}

0 commit comments

Comments
 (0)