use std::collections::HashMap;
use pyo3::{PyResult, pyclass, pymethods};
use sosaku::Exp;
use crate::{errors::PySosakuError, py_types::jsonobj::PyJsonValue};
#[pyclass(from_py_object, eq, frozen, name = "Exp")]
#[derive(Debug, Clone, PartialEq)]
#[repr(transparent)]
pub struct PyExp {
inner: Exp<'static>,
}
#[pymethods]
impl PyExp {
#[new]
#[pyo3(signature = (exp, /))]
pub fn new(exp: &str) -> PyResult<Self> {
Ok(Self {
inner: Exp::try_from(exp)
.map_err(PySosakuError::from)?
.into_owned(),
})
}
pub fn eval(&self, bindings: HashMap<String, PyJsonValue>) -> PyResult<PyJsonValue> {
Ok(self
.inner
.eval(
&sosaku::Env::<PyJsonValue>::new()
.bind_multiple(bindings)
.build(),
)
.map_err(PySosakuError::from)?
.into_owned()
.into())
}
}