Split value conversion into a separate module

This commit is contained in:
Simon Hausmann 2023-12-13 23:56:39 +01:00 committed by Simon Hausmann
parent 10d6aa199c
commit 644ebbb2aa
3 changed files with 60 additions and 54 deletions

View file

@ -9,6 +9,7 @@ use pyo3::prelude::*;
use pyo3::types::PyTuple;
use crate::errors::{PyGetPropertyError, PyInvokeError, PySetCallbackError, PySetPropertyError};
use crate::value::PyValue;
#[pyclass(unsendable)]
pub struct ComponentCompiler {
@ -307,57 +308,3 @@ impl ComponentInstance {
.into())
}
}
struct PyValue(slint_interpreter::Value);
impl IntoPy<PyObject> for PyValue {
fn into_py(self, py: Python<'_>) -> PyObject {
match self.0 {
slint_interpreter::Value::Void => ().into_py(py),
slint_interpreter::Value::Number(num) => num.into_py(py),
slint_interpreter::Value::String(str) => str.into_py(py),
slint_interpreter::Value::Bool(b) => b.into_py(py),
slint_interpreter::Value::Image(_) => todo!(),
slint_interpreter::Value::Model(_) => todo!(),
slint_interpreter::Value::Struct(_) => todo!(),
slint_interpreter::Value::Brush(_) => todo!(),
_ => todo!(),
}
}
}
impl ToPyObject for PyValue {
fn to_object(&self, py: Python<'_>) -> PyObject {
match &self.0 {
slint_interpreter::Value::Void => ().into_py(py),
slint_interpreter::Value::Number(num) => num.into_py(py),
slint_interpreter::Value::String(str) => str.into_py(py),
slint_interpreter::Value::Bool(b) => b.into_py(py),
slint_interpreter::Value::Image(_) => todo!(),
slint_interpreter::Value::Model(_) => todo!(),
slint_interpreter::Value::Struct(_) => todo!(),
slint_interpreter::Value::Brush(_) => todo!(),
_ => todo!(),
}
}
}
impl FromPyObject<'_> for PyValue {
fn extract(ob: &PyAny) -> PyResult<Self> {
Ok(PyValue(
ob.extract::<bool>()
.map(|b| slint_interpreter::Value::Bool(b))
.or_else(|_| {
ob.extract::<&'_ str>().map(|s| slint_interpreter::Value::String(s.into()))
})
.or_else(|_| {
ob.extract::<f64>().map(|num| slint_interpreter::Value::Number(num))
})?,
))
}
}
impl From<slint_interpreter::Value> for PyValue {
fn from(value: slint_interpreter::Value) -> Self {
Self(value)
}
}