post AI cleanup & fixes
This commit is contained in:
38
src/lib.rs
38
src/lib.rs
@@ -1,17 +1,22 @@
|
||||
use pyo3::exceptions::PyRuntimeError;
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::types::{PyDict, PyList};
|
||||
use rhai::{Array, Dynamic, Engine, EvalAltResult, Map, Scope};
|
||||
use std::collections::HashMap;
|
||||
use pyo3::IntoPyObjectExt;
|
||||
use rhai::{Array, Dynamic, Engine, Map, Scope};
|
||||
|
||||
/// Convert Rhai Dynamic to Python object
|
||||
fn dynamic_to_python(py: Python<'_>, value: Dynamic) -> PyResult<PyObject> {
|
||||
match value.type_name() {
|
||||
"i64" => Ok(value.as_int().unwrap().to_object(py)),
|
||||
"f64" => Ok(value.as_float().unwrap().to_object(py)),
|
||||
"bool" => Ok(value.as_bool().unwrap().to_object(py)),
|
||||
"string" => Ok(value.into_string().unwrap().to_object(py)),
|
||||
"char" => Ok(value.as_char().unwrap().to_string().to_object(py)),
|
||||
"i64" => Ok(value.as_int().unwrap().into_py_any(py).unwrap()),
|
||||
"f64" => Ok(value.as_float().unwrap().into_py_any(py).unwrap()),
|
||||
"bool" => Ok(value.as_bool().unwrap().into_py_any(py).unwrap()),
|
||||
"string" => Ok(value.into_string().unwrap().into_py_any(py).unwrap()),
|
||||
"char" => Ok(value
|
||||
.as_char()
|
||||
.unwrap()
|
||||
.to_string()
|
||||
.into_py_any(py)
|
||||
.unwrap()),
|
||||
"()" => Ok(py.None()),
|
||||
"array" => {
|
||||
let array = value.cast::<Array>();
|
||||
@@ -19,17 +24,17 @@ fn dynamic_to_python(py: Python<'_>, value: Dynamic) -> PyResult<PyObject> {
|
||||
for item in array {
|
||||
py_list.append(dynamic_to_python(py, item)?)?;
|
||||
}
|
||||
Ok(py_list.to_object(py))
|
||||
Ok(py_list.into_py_any(py)?.into())
|
||||
}
|
||||
"map" => {
|
||||
let map = value.cast::<Map>();
|
||||
let py_dict = PyDict::new(py);
|
||||
for (key, value) in map {
|
||||
let py_key = key.to_object(py);
|
||||
let py_key = key.to_string().into_py_any(py).unwrap();
|
||||
let py_value = dynamic_to_python(py, value)?;
|
||||
py_dict.set_item(py_key, py_value)?;
|
||||
}
|
||||
Ok(py_dict.to_object(py))
|
||||
Ok(py_dict.into_py_any(py)?.into())
|
||||
}
|
||||
_ => Err(PyRuntimeError::new_err(format!(
|
||||
"Unsupported Rhai type: {}",
|
||||
@@ -39,7 +44,7 @@ fn dynamic_to_python(py: Python<'_>, value: Dynamic) -> PyResult<PyObject> {
|
||||
}
|
||||
|
||||
/// Convert Python object to Rhai Dynamic
|
||||
fn python_to_dynamic(py: Python<'_>, obj: &PyAny) -> PyResult<Dynamic> {
|
||||
fn python_to_dynamic(py: Python<'_>, obj: &Bound<'_, PyAny>) -> PyResult<Dynamic> {
|
||||
if obj.is_none() {
|
||||
Ok(Dynamic::UNIT)
|
||||
} else if let Ok(val) = obj.extract::<bool>() {
|
||||
@@ -53,14 +58,14 @@ fn python_to_dynamic(py: Python<'_>, obj: &PyAny) -> PyResult<Dynamic> {
|
||||
} else if let Ok(py_list) = obj.downcast::<PyList>() {
|
||||
let mut array = Array::new();
|
||||
for item in py_list {
|
||||
array.push(python_to_dynamic(py, item)?);
|
||||
array.push(python_to_dynamic(py, &item)?);
|
||||
}
|
||||
Ok(Dynamic::from(array))
|
||||
} else if let Ok(py_dict) = obj.downcast::<PyDict>() {
|
||||
let mut map = Map::new();
|
||||
for (key, value) in py_dict {
|
||||
let key_str = key.extract::<String>()?;
|
||||
let rhai_value = python_to_dynamic(py, value)?;
|
||||
let rhai_value = python_to_dynamic(py, &value)?;
|
||||
map.insert(key_str.into(), rhai_value);
|
||||
}
|
||||
Ok(Dynamic::from(map))
|
||||
@@ -100,7 +105,7 @@ impl RhaiEngine {
|
||||
}
|
||||
|
||||
/// Set a variable in the engine scope
|
||||
fn set_var(&mut self, py: Python<'_>, name: &str, value: &PyAny) -> PyResult<()> {
|
||||
fn set_var(&mut self, py: Python<'_>, name: &str, value: &Bound<'_, PyAny>) -> PyResult<()> {
|
||||
let dynamic_value = python_to_dynamic(py, value)?;
|
||||
self.scope.set_value(name, dynamic_value);
|
||||
Ok(())
|
||||
@@ -142,15 +147,14 @@ impl RhaiEngine {
|
||||
fn list_vars(&self) -> Vec<String> {
|
||||
self.scope
|
||||
.iter()
|
||||
.map(|(name, _)| name.to_string())
|
||||
.map(|(name, _, _)| name.to_string())
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// A Python module implemented in Rust using PyO3
|
||||
#[pymodule]
|
||||
fn rhai_python(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
fn rhai_python(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
m.add_class::<RhaiEngine>()?;
|
||||
m.add("__version__", "0.1.0")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user