mirror of
https://github.com/gorules/zen.git
synced 2026-10-06 16:02:19 +00:00
feat: update pyo3 (#308)
* feat: update pyo3 * update workflows * python version * update minimum * update pyproject * fix pipeline
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
use anyhow::{anyhow, Context};
|
||||
use pyo3::types::PyDict;
|
||||
use pyo3::{pyfunction, PyObject, PyResult, Python, ToPyObject};
|
||||
use pyo3::{pyfunction, Bound, IntoPyObjectExt, Py, PyAny, PyResult, Python};
|
||||
use pythonize::depythonize;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::value::PyValue;
|
||||
|
||||
#[pyfunction]
|
||||
#[pyo3(signature = (expression, ctx=None))]
|
||||
pub fn evaluate_expression(
|
||||
py: Python,
|
||||
expression: String,
|
||||
ctx: Option<&PyDict>,
|
||||
) -> PyResult<PyObject> {
|
||||
ctx: Option<&Bound<'_, PyDict>>,
|
||||
) -> PyResult<Py<PyAny>> {
|
||||
let context = ctx
|
||||
.map(|ctx| depythonize(ctx))
|
||||
.transpose()
|
||||
@@ -21,11 +22,11 @@ pub fn evaluate_expression(
|
||||
let result = zen_expression::evaluate_expression(expression.as_str(), context.into())
|
||||
.map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?;
|
||||
|
||||
Ok(PyValue(result.to_value()).to_object(py))
|
||||
PyValue(result.to_value()).into_py_any(py)
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
pub fn evaluate_unary_expression(expression: String, ctx: &PyDict) -> PyResult<bool> {
|
||||
pub fn evaluate_unary_expression(expression: String, ctx: &Bound<'_, PyDict>) -> PyResult<bool> {
|
||||
let context: Value = depythonize(ctx).context("Failed to convert context")?;
|
||||
|
||||
let result = zen_expression::evaluate_unary_expression(expression.as_str(), context.into())
|
||||
@@ -35,11 +36,15 @@ pub fn evaluate_unary_expression(expression: String, ctx: &PyDict) -> PyResult<b
|
||||
}
|
||||
|
||||
#[pyfunction]
|
||||
pub fn render_template(py: Python, template: String, ctx: &PyDict) -> PyResult<PyObject> {
|
||||
pub fn render_template(
|
||||
py: Python,
|
||||
template: String,
|
||||
ctx: &Bound<'_, PyDict>,
|
||||
) -> PyResult<Py<PyAny>> {
|
||||
let context: Value = depythonize(ctx).context("Failed to convert context")?;
|
||||
|
||||
let result = zen_tmpl::render(template.as_str(), context.into())
|
||||
.map_err(|e| anyhow!(serde_json::to_string(&e).unwrap_or_else(|_| e.to_string())))?;
|
||||
|
||||
Ok(PyValue(result.to_value()).to_object(py))
|
||||
PyValue(result.to_value()).into_py_any(py)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user