1
1
use chrono:: { self , DateTime , FixedOffset , NaiveDate , NaiveDateTime , NaiveTime } ;
2
2
use serde_json:: { json, Map , Value } ;
3
- use std:: fmt:: Debug ;
3
+ use std:: { fmt:: Debug , net :: IpAddr } ;
4
4
use uuid:: Uuid ;
5
5
6
6
use bytes:: { BufMut , BytesMut } ;
@@ -45,6 +45,7 @@ pub enum PythonDTO {
45
45
PyTime ( NaiveTime ) ,
46
46
PyDateTime ( NaiveDateTime ) ,
47
47
PyDateTimeTz ( DateTime < FixedOffset > ) ,
48
+ PyIpAddress ( IpAddr ) ,
48
49
PyList ( Vec < PythonDTO > ) ,
49
50
PyTuple ( Vec < PythonDTO > ) ,
50
51
PyJson ( Value ) ,
@@ -70,6 +71,7 @@ impl PythonDTO {
70
71
PythonDTO :: PyIntI64 ( _) => Ok ( tokio_postgres:: types:: Type :: INT8_ARRAY ) ,
71
72
PythonDTO :: PyFloat32 ( _) => Ok ( tokio_postgres:: types:: Type :: FLOAT4_ARRAY ) ,
72
73
PythonDTO :: PyFloat64 ( _) => Ok ( tokio_postgres:: types:: Type :: FLOAT8_ARRAY ) ,
74
+ PythonDTO :: PyIpAddress ( _) => Ok ( tokio_postgres:: types:: Type :: INET_ARRAY ) ,
73
75
PythonDTO :: PyJson ( _) => Ok ( tokio_postgres:: types:: Type :: JSONB_ARRAY ) ,
74
76
_ => Err ( RustPSQLDriverError :: PyToRustValueConversionError (
75
77
"Can't process array type, your type doesn't have support yet" . into ( ) ,
@@ -175,6 +177,9 @@ impl ToSql for PythonDTO {
175
177
PythonDTO :: PyDateTimeTz ( pydatetime_tz) => {
176
178
<& DateTime < FixedOffset > as ToSql >:: to_sql ( & pydatetime_tz, ty, out) ?;
177
179
}
180
+ PythonDTO :: PyIpAddress ( pyidaddress) => {
181
+ <& IpAddr as ToSql >:: to_sql ( & pyidaddress, ty, out) ?;
182
+ }
178
183
PythonDTO :: PyList ( py_iterable) | PythonDTO :: PyTuple ( py_iterable) => {
179
184
let mut items = Vec :: new ( ) ;
180
185
for inner in py_iterable {
@@ -343,6 +348,10 @@ pub fn py_to_rust(parameter: &PyAny) -> RustPSQLDriverPyResult<PythonDTO> {
343
348
) ) ;
344
349
}
345
350
351
+ if let Ok ( id_address) = parameter. extract :: < IpAddr > ( ) {
352
+ return Ok ( PythonDTO :: PyIpAddress ( id_address) ) ;
353
+ }
354
+
346
355
Err ( RustPSQLDriverError :: PyToRustValueConversionError ( format ! (
347
356
"Can not covert you type {parameter} into inner one" ,
348
357
) ) )
@@ -408,6 +417,8 @@ pub fn postgres_to_py(
408
417
None => Ok ( py. None ( ) ) ,
409
418
}
410
419
}
420
+ // ---------- IpAddress Types ----------
421
+ Type :: INET => Ok ( row. try_get :: < _ , Option < IpAddr > > ( column_i) ?. to_object ( py) ) ,
411
422
// ---------- Array Text Types ----------
412
423
// Convert ARRAY of TEXT or VARCHAR into Vec<String>, then into list[str]
413
424
Type :: TEXT_ARRAY | Type :: VARCHAR_ARRAY => Ok ( row
@@ -454,6 +465,10 @@ pub fn postgres_to_py(
454
465
}
455
466
None => Ok ( py. None ( ) . to_object ( py) ) ,
456
467
} ,
468
+ // Convert ARRAY of INET into Vec<INET>, then into list[IPv4Address | IPv6Address]
469
+ Type :: INET_ARRAY => Ok ( row
470
+ . try_get :: < _ , Option < Vec < IpAddr > > > ( column_i) ?
471
+ . to_object ( py) ) ,
457
472
Type :: JSONB | Type :: JSON => {
458
473
let db_json = row. try_get :: < _ , Option < Value > > ( column_i) ?;
459
474
0 commit comments