Skip to main content

nautilus_core/python/
params.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Python bindings for [`Params`] type conversion.
17
18use pyo3::{
19    conversion::IntoPyObjectExt,
20    prelude::*,
21    types::{PyDict, PyList, PyModule},
22};
23use serde_json::Value;
24
25use crate::{
26    params::Params,
27    python::{serialization::from_pyobject_pyo3, to_pyvalue_err},
28};
29
30/// Converts a Python dict to `Params` (IndexMap<String, Value>).
31///
32/// # Errors
33///
34/// Returns a `PyErr` if:
35/// - the dict cannot be serialized to JSON
36/// - the JSON is not a valid object
37pub fn pydict_to_params(py: Python<'_>, dict: &Py<PyDict>) -> PyResult<Option<Params>> {
38    let dict_bound = dict.bind(py);
39    if dict_bound.is_empty() {
40        return Ok(None);
41    }
42
43    from_pyobject_pyo3(py, dict_bound.as_any()).map(Some)
44}
45
46/// Converts a `serde_json::Value` to a Python object.
47///
48/// This is a common conversion pattern used when converting `Params` to Python dicts.
49///
50/// # Errors
51///
52/// Returns a `PyErr` if the value type is unsupported, numeric extraction fails,
53/// or conversion fails.
54pub fn value_to_pyobject(py: Python<'_>, val: &Value) -> PyResult<Py<PyAny>> {
55    match val {
56        Value::Null => Ok(py.None()),
57        Value::Bool(b) => b.into_py_any(py),
58        Value::String(s) => s.into_py_any(py),
59        Value::Number(n) => {
60            if n.is_i64() {
61                n.as_i64()
62                    .ok_or_else(|| to_pyvalue_err("JSON number could not be read as i64"))?
63                    .into_py_any(py)
64            } else if n.is_u64() {
65                n.as_u64()
66                    .ok_or_else(|| to_pyvalue_err("JSON number could not be read as u64"))?
67                    .into_py_any(py)
68            } else if n.is_f64() {
69                n.as_f64()
70                    .ok_or_else(|| to_pyvalue_err("JSON number could not be read as f64"))?
71                    .into_py_any(py)
72            } else {
73                Err(to_pyvalue_err("Unsupported JSON number type"))
74            }
75        }
76        Value::Array(arr) => {
77            let py_list = PyList::new(py, &[] as &[Py<PyAny>])?;
78            for item in arr {
79                let py_item = value_to_pyobject(py, item)?;
80                py_list.append(py_item)?;
81            }
82            py_list.into_py_any(py)
83        }
84        Value::Object(_) => {
85            // For nested objects, convert to dict recursively
86            let json_str = serde_json::to_string(val).map_err(to_pyvalue_err)?;
87            let py_dict: Py<PyDict> = PyModule::import(py, "json")?
88                .call_method("loads", (json_str,), None)?
89                .extract()?;
90            py_dict.into_py_any(py)
91        }
92    }
93}
94
95/// Converts `Params` (IndexMap<String, Value>) to a Python dict.
96///
97/// # Errors
98///
99/// Returns a `PyErr` if conversion of any value fails.
100pub fn params_to_pydict(py: Python<'_>, params: &Params) -> PyResult<Py<PyDict>> {
101    let dict = PyDict::new(py);
102    for (key, value) in params {
103        let py_value = value_to_pyobject(py, value)?;
104        dict.set_item(key, py_value)?;
105    }
106    Ok(dict.into())
107}
108
109#[cfg(test)]
110mod tests {
111    use rstest::rstest;
112    use serde_json::json;
113
114    use super::*;
115
116    #[derive(Debug, Clone, Copy)]
117    enum ExpectedNumber {
118        I64(i64),
119        U64(u64),
120        F64(f64),
121    }
122
123    #[rstest]
124    #[case(json!(-100_i64), ExpectedNumber::I64(-100))]
125    #[case(json!(42_u64), ExpectedNumber::U64(42))]
126    #[case(json!(2.5_f64), ExpectedNumber::F64(2.5))]
127    fn test_value_to_pyobject_number_branches(
128        #[case] value: Value,
129        #[case] expected: ExpectedNumber,
130    ) {
131        Python::initialize();
132        Python::attach(|py| {
133            let py_obj = value_to_pyobject(py, &value).unwrap();
134
135            match expected {
136                ExpectedNumber::I64(expected) => {
137                    assert_eq!(py_obj.extract::<i64>(py).unwrap(), expected);
138                }
139                ExpectedNumber::U64(expected) => {
140                    assert_eq!(py_obj.extract::<u64>(py).unwrap(), expected);
141                }
142                ExpectedNumber::F64(expected) => {
143                    let actual = py_obj.extract::<f64>(py).unwrap();
144                    assert!((actual - expected).abs() < f64::EPSILON);
145                }
146            }
147        });
148    }
149}