@@ -14,13 +14,16 @@ use pyo3::{
1414 PyAnyMethods , PyBool , PyBytes , PyDate , PyDateTime , PyDelta , PyDict , PyDictMethods , PyFloat ,
1515 PyInt , PyList , PyMapping , PySequence , PySet , PyString , PyTime , PyTuple , PyTypeMethods ,
1616 } ,
17- Bound , FromPyObject , Py , PyAny , Python ,
17+ Bound , Py , PyAny , Python ,
1818} ;
1919
2020use crate :: {
2121 exceptions:: rust_errors:: { RustPSQLDriverError , RustPSQLDriverPyResult } ,
2222 extra_types:: { self } ,
23- value_converter:: { consts:: KWARGS_QUERYSTRINGS , models:: dto:: PythonDTO } ,
23+ value_converter:: {
24+ consts:: KWARGS_QUERYSTRINGS , models:: dto:: PythonDTO ,
25+ utils:: extract_value_from_python_object_or_raise,
26+ } ,
2427} ;
2528
2629/// Convert single python parameter to `PythonDTO` enum.
@@ -449,28 +452,6 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<
449452 ) ) )
450453}
451454
452- /// Extract a value from a Python object, raising an error if missing or invalid
453- ///
454- /// # Errors
455- /// This function will return `Err` in the following cases:
456- /// - The Python object does not have the specified attribute
457- /// - The attribute exists but cannot be extracted into the specified Rust type
458- fn extract_value_from_python_object_or_raise < ' py , T > (
459- parameter : & ' py pyo3:: Bound < ' _ , PyAny > ,
460- attr_name : & str ,
461- ) -> Result < T , RustPSQLDriverError >
462- where
463- T : FromPyObject < ' py > ,
464- {
465- parameter
466- . getattr ( attr_name)
467- . ok ( )
468- . and_then ( |attr| attr. extract :: < T > ( ) . ok ( ) )
469- . ok_or_else ( || {
470- RustPSQLDriverError :: PyToRustValueConversionError ( "Invalid attribute" . into ( ) )
471- } )
472- }
473-
474455/// Extract a timezone-aware datetime from a Python object.
475456/// This function retrieves various datetime components (`year`, `month`, `day`, etc.)
476457/// from a Python object and constructs a `DateTime<FixedOffset>`
@@ -552,7 +533,7 @@ pub fn py_sequence_into_postgres_array(
552533 lower_bound : 1 ,
553534 } ) ;
554535
555- let first_seq_elem = py_seq. iter ( ) ?. next ( ) ;
536+ let first_seq_elem = py_seq. try_iter ( ) ?. next ( ) ;
556537 match first_seq_elem {
557538 Some ( first_seq_elem) => {
558539 if let Ok ( first_seq_elem) = first_seq_elem {
@@ -602,7 +583,7 @@ pub fn py_sequence_into_flat_vec(
602583
603584 let mut final_vec: Vec < PythonDTO > = vec ! [ ] ;
604585
605- for seq_elem in py_seq. iter ( ) ? {
586+ for seq_elem in py_seq. try_iter ( ) ? {
606587 let ok_seq_elem = seq_elem?;
607588
608589 // Check for the string because it's sequence too,
0 commit comments