mirror of
https://github.com/gorules/zen.git
synced 2026-10-05 08:02:28 +00:00
feat: py bindings refactoring (#317)
* feat: py bindings refactoring * unset version in pyproject.toml
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use pyo3::prelude::{PyDictMethods, PyListMethods};
|
||||
use pyo3::types::{PyDict, PyList};
|
||||
use pyo3::{Bound, IntoPyObject, IntoPyObjectExt, PyAny, PyErr, PyResult, Python};
|
||||
use anyhow::Context;
|
||||
use pyo3::prelude::{PyAnyMethods, PyBytesMethods, PyDictMethods, PyListMethods, PyStringMethods};
|
||||
use pyo3::types::{PyBytes, PyDict, PyList, PyString};
|
||||
use pyo3::{Bound, FromPyObject, IntoPyObject, IntoPyObjectExt, PyAny, PyErr, PyResult, Python};
|
||||
use pythonize::depythonize;
|
||||
use rust_decimal::prelude::ToPrimitive;
|
||||
use zen_expression::Variable;
|
||||
|
||||
@@ -8,6 +10,12 @@ use zen_expression::Variable;
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PyVariable(pub Variable);
|
||||
|
||||
impl PyVariable {
|
||||
pub fn into_inner(self) -> Variable {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn variable_to_object<'py>(py: Python<'py>, val: &Variable) -> PyResult<Bound<'py, PyAny>> {
|
||||
match val {
|
||||
Variable::Null => py.None().into_bound_py_any(py),
|
||||
@@ -49,3 +57,24 @@ impl<'py> IntoPyObject<'py> for PyVariable {
|
||||
variable_to_object(py, &self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'py> FromPyObject<'py> for PyVariable {
|
||||
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
|
||||
if let Ok(s) = ob.downcast::<PyString>() {
|
||||
let str_slice = s.to_str()?;
|
||||
|
||||
let var = serde_json::from_str(str_slice).context("Invalid JSON")?;
|
||||
return Ok(PyVariable(var));
|
||||
}
|
||||
|
||||
if let Ok(b) = ob.downcast::<PyBytes>() {
|
||||
let bytes = b.as_bytes();
|
||||
|
||||
let var = serde_json::from_slice(bytes).context("Invalid JSON")?;
|
||||
return Ok(PyVariable(var));
|
||||
}
|
||||
|
||||
let var = depythonize(ob)?;
|
||||
Ok(PyVariable(var))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user