@@ -203,26 +203,22 @@ impl PythonDTO {
203
203
204
204
Ok ( json ! ( vec_serde_values) )
205
205
}
206
- PythonDTO :: PyArray ( array) => {
207
- let py_list = Python :: with_gil ( |gil| {
208
- let py_list = postgres_array_to_py ( gil, Some ( array. clone ( ) ) ) ;
209
- if let Some ( py_list) = py_list {
210
- let mut vec_serde_values: Vec < Value > = vec ! [ ] ;
211
-
212
- for py_object in py_list. bind ( gil) {
213
- vec_serde_values. push ( py_to_rust ( & py_object) ?. to_serde_value ( ) ?) ;
214
- }
206
+ PythonDTO :: PyArray ( array) => Python :: with_gil ( |gil| {
207
+ let py_list = postgres_array_to_py ( gil, Some ( array. clone ( ) ) ) ;
208
+ if let Some ( py_list) = py_list {
209
+ let mut vec_serde_values: Vec < Value > = vec ! [ ] ;
215
210
216
- return Ok ( json ! ( vec_serde_values) ) ;
211
+ for py_object in py_list. bind ( gil) {
212
+ vec_serde_values. push ( py_to_rust ( & py_object) ?. to_serde_value ( ) ?) ;
217
213
}
218
214
219
- return Err ( RustPSQLDriverError :: PyToRustValueConversionError (
220
- "Cannot convert Python sequence into JSON" . into ( ) ,
221
- ) ) ;
222
- } ) ;
215
+ return Ok ( json ! ( vec_serde_values) ) ;
216
+ }
223
217
224
- py_list
225
- }
218
+ Err ( RustPSQLDriverError :: PyToRustValueConversionError (
219
+ "Cannot convert Python sequence into JSON" . into ( ) ,
220
+ ) )
221
+ } ) ,
226
222
PythonDTO :: PyJsonb ( py_dict) | PythonDTO :: PyJson ( py_dict) => Ok ( py_dict. clone ( ) ) ,
227
223
_ => Err ( RustPSQLDriverError :: PyToRustValueConversionError (
228
224
"Cannot convert your type into Rust type" . into ( ) ,
@@ -412,6 +408,10 @@ pub fn convert_parameters(parameters: Py<PyAny>) -> RustPSQLDriverPyResult<Vec<P
412
408
Ok ( result_vec)
413
409
}
414
410
411
+ /// Convert Sequence from Python (except String) into flat vec.
412
+ ///
413
+ /// # Errors
414
+ /// May return Err Result if cannot convert element into Rust one.
415
415
pub fn py_sequence_into_flat_vec (
416
416
parameter : & Bound < PyAny > ,
417
417
) -> RustPSQLDriverPyResult < Vec < PythonDTO > > {
@@ -435,21 +435,25 @@ pub fn py_sequence_into_flat_vec(
435
435
436
436
let possible_next_seq = ok_seq_elem. downcast :: < PySequence > ( ) ;
437
437
438
- match possible_next_seq {
439
- Ok ( next_seq) => {
440
- let mut next_vec = py_sequence_into_flat_vec ( next_seq) ?;
441
- final_vec. append ( & mut next_vec) ;
442
- }
443
- Err ( _) => {
444
- final_vec. push ( py_to_rust ( & ok_seq_elem) ?) ;
445
- continue ;
446
- }
438
+ if let Ok ( next_seq) = possible_next_seq {
439
+ let mut next_vec = py_sequence_into_flat_vec ( next_seq) ?;
440
+ final_vec. append ( & mut next_vec) ;
441
+ } else {
442
+ final_vec. push ( py_to_rust ( & ok_seq_elem) ?) ;
443
+ continue ;
447
444
}
448
445
}
449
446
450
- return Ok ( final_vec) ;
447
+ Ok ( final_vec)
451
448
}
452
449
450
+ /// Convert Sequence from Python into Postgres ARRAY.
451
+ ///
452
+ /// # Errors
453
+ ///
454
+ /// May return Err Result if cannot convert at least one element.
455
+ #[ allow( clippy:: cast_possible_truncation) ]
456
+ #[ allow( clippy:: cast_possible_wrap) ]
453
457
pub fn py_sequence_into_postgres_array (
454
458
parameter : & Bound < PyAny > ,
455
459
) -> RustPSQLDriverPyResult < Array < PythonDTO > > {
@@ -471,7 +475,7 @@ pub fn py_sequence_into_postgres_array(
471
475
lower_bound : 1 ,
472
476
} ) ;
473
477
474
- let first_seq_elem = py_seq. iter ( ) ?. nth ( 0 ) ;
478
+ let first_seq_elem = py_seq. iter ( ) ?. next ( ) ;
475
479
match first_seq_elem {
476
480
Some ( first_seq_elem) => {
477
481
if let Ok ( first_seq_elem) = first_seq_elem {
@@ -500,14 +504,10 @@ pub fn py_sequence_into_postgres_array(
500
504
let array_data = py_sequence_into_flat_vec ( parameter) ?;
501
505
502
506
match postgres_array:: Array :: from_parts_no_panic ( array_data, dimensions) {
503
- Ok ( result_array) => {
504
- return Ok ( result_array) ;
505
- }
506
- Err ( err) => {
507
- return Err ( RustPSQLDriverError :: PyToRustValueConversionError ( format ! (
508
- "Cannot convert python sequence to PostgreSQL ARRAY, error - {err}"
509
- ) ) )
510
- }
507
+ Ok ( result_array) => Ok ( result_array) ,
508
+ Err ( err) => Err ( RustPSQLDriverError :: PyToRustValueConversionError ( format ! (
509
+ "Cannot convert python sequence to PostgreSQL ARRAY, error - {err}"
510
+ ) ) ) ,
511
511
}
512
512
}
513
513
@@ -771,18 +771,17 @@ fn postgres_array_to_py<T: ToPyObject>(
771
771
return Some ( _postgres_array_to_py (
772
772
py,
773
773
array. dimensions ( ) ,
774
- array. iter ( ) . map ( |arg| arg ) . collect :: < Vec < & T > > ( ) . as_slice ( ) ,
774
+ array. iter ( ) . collect :: < Vec < & T > > ( ) . as_slice ( ) ,
775
775
0 ,
776
776
0 ,
777
777
) ) ;
778
778
}
779
- None => {
780
- return None ;
781
- }
779
+ None => None ,
782
780
}
783
781
}
784
782
785
783
/// Inner postgres array conversion to python list.
784
+ #[ allow( clippy:: cast_sign_loss) ]
786
785
fn _postgres_array_to_py < T > (
787
786
py : Python < ' _ > ,
788
787
dimensions : & [ Dimension ] ,
@@ -793,15 +792,15 @@ fn _postgres_array_to_py<T>(
793
792
where
794
793
T : ToPyObject ,
795
794
{
796
- let current_dimension = dimensions. iter ( ) . nth ( dimension_index) . unwrap ( ) ;
795
+ let current_dimension = dimensions. get ( dimension_index) . unwrap ( ) ;
797
796
798
- let possible_next_dimension = dimensions. iter ( ) . nth ( dimension_index + 1 ) ;
797
+ let possible_next_dimension = dimensions. get ( dimension_index + 1 ) ;
799
798
match possible_next_dimension {
800
799
Some ( next_dimension) => {
801
800
let final_list = PyList :: empty_bound ( py) ;
802
801
803
802
for _ in 0 ..current_dimension. len as usize {
804
- if dimensions. iter ( ) . nth ( dimension_index + 1 ) . is_some ( ) {
803
+ if dimensions. get ( dimension_index + 1 ) . is_some ( ) {
805
804
let inner_pylist = _postgres_array_to_py (
806
805
py,
807
806
dimensions,
@@ -814,7 +813,7 @@ where
814
813
} ;
815
814
}
816
815
817
- return final_list. unbind ( ) ;
816
+ final_list. unbind ( )
818
817
}
819
818
None => {
820
819
return PyList :: new_bound ( py, data) . unbind ( ) ;
@@ -1009,14 +1008,14 @@ fn postgres_bytes_to_py(
1009
1008
. to_object ( py) ) ,
1010
1009
// Convert ARRAY of Integer into Vec<i32>, then into list[int]
1011
1010
Type :: INT4_ARRAY => {
1012
- return Ok ( postgres_array_to_py (
1011
+ Ok ( postgres_array_to_py (
1013
1012
py,
1014
1013
_composite_field_postgres_to_py :: < Option < Array < i32 > > > (
1015
1014
type_,
1016
1015
buf,
1017
1016
is_simple,
1018
1017
) ?
1019
- ) . to_object ( py) ) ;
1018
+ ) . to_object ( py) )
1020
1019
} ,
1021
1020
// Convert ARRAY of BigInt into Vec<i64>, then into list[int]
1022
1021
Type :: INT8_ARRAY | Type :: MONEY_ARRAY => Ok ( _composite_field_postgres_to_py :: < Option < Vec < i64 > > > (
0 commit comments