mirror of
https://github.com/gorules/zen.git
synced 2026-10-04 16:02:18 +00:00
* precompile first * test * add Opcode cache through Option<T> * Compiled support for decisionTable, default compile of new DecisionContent for node.js and py bindings * format * DecisionContent refactored to model module * fmt * added back BaseDecisionContent * tests and benchmark --------- Co-authored-by: Stefan <stefan@gorules.io>
46 lines
1.4 KiB
Rust
46 lines
1.4 KiB
Rust
use anyhow::Context;
|
|
use pyo3::prelude::{PyAnyMethods, PyStringMethods};
|
|
use pyo3::types::PyString;
|
|
use pyo3::{pyclass, pymethods, Bound, FromPyObject, PyAny, PyResult};
|
|
use pythonize::depythonize;
|
|
use std::sync::Arc;
|
|
use zen_engine::model::DecisionContent;
|
|
|
|
#[pyclass]
|
|
#[pyo3(name = "ZenDecisionContent")]
|
|
pub struct PyZenDecisionContent(pub Arc<DecisionContent>);
|
|
|
|
#[pymethods]
|
|
impl PyZenDecisionContent {
|
|
#[new]
|
|
pub fn new(data: &str) -> PyResult<Self> {
|
|
let mut content: DecisionContent =
|
|
serde_json::from_str(data).context("Failed to parse JSON")?;
|
|
content.compile();
|
|
Ok(Self(Arc::new(content)))
|
|
}
|
|
}
|
|
|
|
pub struct PyZenDecisionContentJson(pub PyZenDecisionContent);
|
|
|
|
impl<'py> FromPyObject<'py> for PyZenDecisionContentJson {
|
|
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
|
|
if let Ok(s) = ob.downcast::<PyZenDecisionContent>() {
|
|
let borrow_ref = s.borrow();
|
|
let content = borrow_ref.0.clone();
|
|
|
|
return Ok(Self(PyZenDecisionContent(content)));
|
|
}
|
|
|
|
if let Ok(b) = ob.downcast::<PyString>() {
|
|
let str = b.to_str()?;
|
|
let content = serde_json::from_str(str).context("Invalid JSON")?;
|
|
|
|
return Ok(Self(PyZenDecisionContent(Arc::new(content))));
|
|
}
|
|
|
|
let content = depythonize(ob)?;
|
|
Ok(Self(PyZenDecisionContent(Arc::new(content))))
|
|
}
|
|
}
|