|
| 1 | +use std::net::IpAddr; |
| 2 | + |
| 3 | +use chrono::{DateTime, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime}; |
| 4 | +use pg_interval::Interval; |
| 5 | +use pyo3::{ |
| 6 | + types::{PyAnyMethods, PyDateTime, PyDelta, PyDict}, |
| 7 | + Bound, PyAny, |
| 8 | +}; |
| 9 | +use rust_decimal::Decimal; |
| 10 | +use uuid::Uuid; |
| 11 | + |
| 12 | +use crate::{ |
| 13 | + exceptions::rust_errors::{RustPSQLDriverError, RustPSQLDriverPyResult}, |
| 14 | + extra_types::{self, PythonDecimal, PythonUUID}, |
| 15 | + value_converter::{ |
| 16 | + additional_types::NonePyType, |
| 17 | + funcs::from_python::{ |
| 18 | + extract_datetime_from_python_object_attrs, py_sequence_into_postgres_array, |
| 19 | + }, |
| 20 | + models::serde_value::build_serde_value, |
| 21 | + traits::PythonToDTO, |
| 22 | + }, |
| 23 | +}; |
| 24 | + |
| 25 | +use super::enums::PythonDTO; |
| 26 | + |
| 27 | +impl PythonToDTO for NonePyType { |
| 28 | + fn to_python_dto(_python_param: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 29 | + Ok(PythonDTO::PyNone) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +macro_rules! construct_simple_type_matcher { |
| 34 | + ($match_type:ty, $kind:path) => { |
| 35 | + impl PythonToDTO for $match_type { |
| 36 | + fn to_python_dto(python_param: &Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 37 | + Ok($kind(python_param.extract::<$match_type>()?)) |
| 38 | + } |
| 39 | + } |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +construct_simple_type_matcher!(bool, PythonDTO::PyBool); |
| 44 | +construct_simple_type_matcher!(Vec<u8>, PythonDTO::PyBytes); |
| 45 | +construct_simple_type_matcher!(String, PythonDTO::PyString); |
| 46 | +construct_simple_type_matcher!(f32, PythonDTO::PyFloat32); |
| 47 | +construct_simple_type_matcher!(f64, PythonDTO::PyFloat64); |
| 48 | +construct_simple_type_matcher!(i16, PythonDTO::PyIntI16); |
| 49 | +construct_simple_type_matcher!(i32, PythonDTO::PyIntI32); |
| 50 | +construct_simple_type_matcher!(i64, PythonDTO::PyIntI64); |
| 51 | +construct_simple_type_matcher!(NaiveDate, PythonDTO::PyDate); |
| 52 | +construct_simple_type_matcher!(NaiveTime, PythonDTO::PyTime); |
| 53 | + |
| 54 | +impl PythonToDTO for PyDateTime { |
| 55 | + fn to_python_dto(python_param: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 56 | + let timestamp_tz = python_param.extract::<DateTime<FixedOffset>>(); |
| 57 | + if let Ok(pydatetime_tz) = timestamp_tz { |
| 58 | + return Ok(PythonDTO::PyDateTimeTz(pydatetime_tz)); |
| 59 | + } |
| 60 | + |
| 61 | + let timestamp_no_tz = python_param.extract::<NaiveDateTime>(); |
| 62 | + if let Ok(pydatetime_no_tz) = timestamp_no_tz { |
| 63 | + return Ok(PythonDTO::PyDateTime(pydatetime_no_tz)); |
| 64 | + } |
| 65 | + |
| 66 | + let timestamp_tz = extract_datetime_from_python_object_attrs(python_param); |
| 67 | + if let Ok(pydatetime_tz) = timestamp_tz { |
| 68 | + return Ok(PythonDTO::PyDateTimeTz(pydatetime_tz)); |
| 69 | + } |
| 70 | + |
| 71 | + return Err(RustPSQLDriverError::PyToRustValueConversionError( |
| 72 | + "Can not convert you datetime to rust type".into(), |
| 73 | + )); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +impl PythonToDTO for PyDelta { |
| 78 | + fn to_python_dto(python_param: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 79 | + let duration = python_param.extract::<chrono::Duration>()?; |
| 80 | + if let Some(interval) = Interval::from_duration(duration) { |
| 81 | + return Ok(PythonDTO::PyInterval(interval)); |
| 82 | + } |
| 83 | + return Err(RustPSQLDriverError::PyToRustValueConversionError( |
| 84 | + "Cannot convert timedelta from Python to inner Rust type.".to_string(), |
| 85 | + )); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl PythonToDTO for PyDict { |
| 90 | + fn to_python_dto(python_param: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 91 | + let serde_value = build_serde_value(python_param)?; |
| 92 | + |
| 93 | + return Ok(PythonDTO::PyJsonb(serde_value)); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +macro_rules! construct_extra_type_matcher { |
| 98 | + ($match_type:ty, $kind:path) => { |
| 99 | + impl PythonToDTO for $match_type { |
| 100 | + fn to_python_dto(python_param: &Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 101 | + Ok($kind(python_param.extract::<$match_type>()?.inner())) |
| 102 | + } |
| 103 | + } |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +construct_extra_type_matcher!(extra_types::JSONB, PythonDTO::PyJsonb); |
| 108 | +construct_extra_type_matcher!(extra_types::JSON, PythonDTO::PyJson); |
| 109 | +construct_extra_type_matcher!(extra_types::MacAddr6, PythonDTO::PyMacAddr6); |
| 110 | +construct_extra_type_matcher!(extra_types::MacAddr8, PythonDTO::PyMacAddr8); |
| 111 | +construct_extra_type_matcher!(extra_types::Point, PythonDTO::PyPoint); |
| 112 | +construct_extra_type_matcher!(extra_types::Box, PythonDTO::PyBox); |
| 113 | +construct_extra_type_matcher!(extra_types::Path, PythonDTO::PyPath); |
| 114 | +construct_extra_type_matcher!(extra_types::Line, PythonDTO::PyLine); |
| 115 | +construct_extra_type_matcher!(extra_types::LineSegment, PythonDTO::PyLineSegment); |
| 116 | +construct_extra_type_matcher!(extra_types::Circle, PythonDTO::PyCircle); |
| 117 | + |
| 118 | +impl PythonToDTO for PythonDecimal { |
| 119 | + fn to_python_dto(python_param: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 120 | + Ok(PythonDTO::PyDecimal(Decimal::from_str_exact( |
| 121 | + python_param.str()?.extract::<&str>()?, |
| 122 | + )?)) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl PythonToDTO for PythonUUID { |
| 127 | + fn to_python_dto(python_param: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 128 | + Ok(PythonDTO::PyUUID(Uuid::parse_str( |
| 129 | + python_param.str()?.extract::<&str>()?, |
| 130 | + )?)) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +impl PythonToDTO for extra_types::PythonArray { |
| 135 | + fn to_python_dto(python_param: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 136 | + Ok(PythonDTO::PyArray(py_sequence_into_postgres_array( |
| 137 | + python_param, |
| 138 | + )?)) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl PythonToDTO for IpAddr { |
| 143 | + fn to_python_dto(python_param: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 144 | + if let Ok(id_address) = python_param.extract::<IpAddr>() { |
| 145 | + return Ok(PythonDTO::PyIpAddress(id_address)); |
| 146 | + } |
| 147 | + |
| 148 | + Err(RustPSQLDriverError::PyToRustValueConversionError( |
| 149 | + "Parameter passed to IpAddr is incorrect.".to_string(), |
| 150 | + )) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +impl PythonToDTO for extra_types::PythonEnum { |
| 155 | + fn to_python_dto(python_param: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 156 | + let string = python_param.extract::<String>()?; |
| 157 | + return Ok(PythonDTO::PyString(string)); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +macro_rules! construct_array_type_matcher { |
| 162 | + ($match_type:ty) => { |
| 163 | + impl PythonToDTO for $match_type { |
| 164 | + fn to_python_dto(python_param: &Bound<'_, PyAny>) -> RustPSQLDriverPyResult<PythonDTO> { |
| 165 | + python_param |
| 166 | + .extract::<$match_type>()? |
| 167 | + ._convert_to_python_dto() |
| 168 | + } |
| 169 | + } |
| 170 | + }; |
| 171 | +} |
| 172 | + |
| 173 | +construct_array_type_matcher!(extra_types::BoolArray); |
| 174 | +construct_array_type_matcher!(extra_types::UUIDArray); |
| 175 | +construct_array_type_matcher!(extra_types::VarCharArray); |
| 176 | +construct_array_type_matcher!(extra_types::TextArray); |
| 177 | +construct_array_type_matcher!(extra_types::Int16Array); |
| 178 | +construct_array_type_matcher!(extra_types::Int32Array); |
| 179 | +construct_array_type_matcher!(extra_types::Int64Array); |
| 180 | +construct_array_type_matcher!(extra_types::Float32Array); |
| 181 | +construct_array_type_matcher!(extra_types::Float64Array); |
| 182 | +construct_array_type_matcher!(extra_types::MoneyArray); |
| 183 | +construct_array_type_matcher!(extra_types::IpAddressArray); |
| 184 | +construct_array_type_matcher!(extra_types::JSONBArray); |
| 185 | +construct_array_type_matcher!(extra_types::JSONArray); |
| 186 | +construct_array_type_matcher!(extra_types::DateArray); |
| 187 | +construct_array_type_matcher!(extra_types::TimeArray); |
| 188 | +construct_array_type_matcher!(extra_types::DateTimeArray); |
| 189 | +construct_array_type_matcher!(extra_types::DateTimeTZArray); |
| 190 | +construct_array_type_matcher!(extra_types::MacAddr6Array); |
| 191 | +construct_array_type_matcher!(extra_types::MacAddr8Array); |
| 192 | +construct_array_type_matcher!(extra_types::NumericArray); |
| 193 | +construct_array_type_matcher!(extra_types::PointArray); |
| 194 | +construct_array_type_matcher!(extra_types::BoxArray); |
| 195 | +construct_array_type_matcher!(extra_types::PathArray); |
| 196 | +construct_array_type_matcher!(extra_types::LineArray); |
| 197 | +construct_array_type_matcher!(extra_types::LsegArray); |
| 198 | +construct_array_type_matcher!(extra_types::CircleArray); |
| 199 | +construct_array_type_matcher!(extra_types::IntervalArray); |
0 commit comments