Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2f15769

Browse filesBrowse files
committed
Moved common func to utils
1 parent e69a383 commit 2f15769
Copy full SHA for 2f15769

File tree

Expand file treeCollapse file tree

3 files changed

+33
-26
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+33
-26
lines changed

‎src/value_converter/funcs/from_python.rs

Copy file name to clipboardExpand all lines: src/value_converter/funcs/from_python.rs
+7-26Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2020
use 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,

‎src/value_converter/mod.rs

Copy file name to clipboardExpand all lines: src/value_converter/mod.rs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ pub mod additional_types;
22
pub mod consts;
33
pub mod funcs;
44
pub mod models;
5+
pub mod utils;

‎src/value_converter/utils.rs

Copy file name to clipboard
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use pyo3::{types::PyAnyMethods, FromPyObject, PyAny};
2+
3+
use crate::exceptions::rust_errors::RustPSQLDriverError;
4+
5+
/// Extract a value from a Python object, raising an error if missing or invalid
6+
///
7+
/// # Errors
8+
/// This function will return `Err` in the following cases:
9+
/// - The Python object does not have the specified attribute
10+
/// - The attribute exists but cannot be extracted into the specified Rust type
11+
pub fn extract_value_from_python_object_or_raise<'py, T>(
12+
parameter: &'py pyo3::Bound<'_, PyAny>,
13+
attr_name: &str,
14+
) -> Result<T, RustPSQLDriverError>
15+
where
16+
T: FromPyObject<'py>,
17+
{
18+
parameter
19+
.getattr(attr_name)
20+
.ok()
21+
.and_then(|attr| attr.extract::<T>().ok())
22+
.ok_or_else(|| {
23+
RustPSQLDriverError::PyToRustValueConversionError("Invalid attribute".into())
24+
})
25+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.