Better values

This commit is contained in:
2025-08-25 10:03:17 +02:00
parent 5f2e8fd25f
commit 87cfb477a7
3 changed files with 30 additions and 34 deletions
+6 -10
View File
@@ -10,29 +10,25 @@ pub enum FluxValue {
Map(BTreeMap<FluxValue, FluxValue>), Map(BTreeMap<FluxValue, FluxValue>),
} }
pub trait Serialize { pub trait ToFlux {
fn serialize(&self) -> FluxValue; fn to_flux(&self) -> FluxValue;
}
pub trait Deserialize {
fn deserialize(v: FluxValue) -> Self;
} }
pub trait FluxModule { pub trait FluxModule {
fn init(&self); fn init(&self);
fn end(&self); fn end(&self);
fn call(&self, func: &str, args: Vec<&dyn Serialize>) -> FluxValue; fn call(&self, func: &str, args: Vec<&dyn ToFlux>) -> FluxValue;
} }
pub mod runtime; pub mod runtime;
pub mod values; pub mod values;
pub mod prelude { pub mod prelude {
pub use crate::{Deserialize, FluxModule, Serialize}; pub use crate::{FluxModule, ToFlux};
} }
impl FluxValue { impl FluxValue {
pub fn extract<T: Deserialize>(self) -> T { pub fn extract<T: From<FluxValue>>(self) -> T {
T::deserialize(self) T::from(self)
} }
} }
+2 -2
View File
@@ -53,13 +53,13 @@ impl FluxModule for PythonModule<'_> {
self.python.run(&code, None, None).unwrap(); self.python.run(&code, None, None).unwrap();
} }
fn call(&self, func: &str, args: Vec<&dyn crate::Serialize>) -> crate::FluxValue { fn call(&self, func: &str, args: Vec<&dyn crate::ToFlux>) -> crate::FluxValue {
let module = self.python.import("__main__").unwrap(); let module = self.python.import("__main__").unwrap();
let func = module.get(self.python, func).unwrap(); let func = module.get(self.python, func).unwrap();
let py_args: Vec<PyObject> = args let py_args: Vec<PyObject> = args
.iter() .iter()
.map(|a| flux_to_object(self.python, &a.serialize())) .map(|a| flux_to_object(self.python, &a.to_flux()))
.collect(); .collect();
let args_tuple = PyTuple::new(self.python, &py_args); let args_tuple = PyTuple::new(self.python, &py_args);
+22 -22
View File
@@ -1,65 +1,65 @@
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
use crate::{Deserialize, FluxValue, Serialize}; use crate::{FluxValue, ToFlux};
impl Serialize for i64 { impl ToFlux for i64 {
fn serialize(&self) -> FluxValue { fn to_flux(&self) -> FluxValue {
FluxValue::Int(*self) FluxValue::Int(*self)
} }
} }
impl Serialize for f64 { impl ToFlux for f64 {
fn serialize(&self) -> FluxValue { fn to_flux(&self) -> FluxValue {
FluxValue::Float(self.to_bits()) FluxValue::Float(self.to_bits())
} }
} }
impl Deserialize for f64 { impl From<FluxValue> for f64 {
fn deserialize(v: FluxValue) -> Self { fn from(value: FluxValue) -> Self {
match v { match value {
FluxValue::Float(bits) => f64::from_bits(bits), FluxValue::Float(bits) => f64::from_bits(bits),
v => panic!("Expected Int, instead got {v:?}"), v => panic!("Expected Int, instead got {v:?}"),
} }
} }
} }
impl Serialize for &str { impl ToFlux for &str {
fn serialize(&self) -> FluxValue { fn to_flux(&self) -> FluxValue {
FluxValue::Str(self.to_string()) FluxValue::Str(self.to_string())
} }
} }
impl Serialize for String { impl ToFlux for String {
fn serialize(&self) -> FluxValue { fn to_flux(&self) -> FluxValue {
FluxValue::Str(self.clone()) FluxValue::Str(self.clone())
} }
} }
impl Deserialize for i64 { impl From<FluxValue> for i64 {
fn deserialize(v: FluxValue) -> Self { fn from(value: FluxValue) -> Self {
match v { match value {
FluxValue::Int(i) => i, FluxValue::Int(i) => i,
v => panic!("Expected Int, instead got {v:?}"), v => panic!("Expected Int, instead got {v:?}"),
} }
} }
} }
impl<K: Serialize, V: Serialize> Serialize for HashMap<K, V> { impl<K: ToFlux, V: ToFlux> ToFlux for HashMap<K, V> {
fn serialize(&self) -> FluxValue { fn to_flux(&self) -> FluxValue {
FluxValue::Map( FluxValue::Map(
self.iter() self.iter()
.map(|(k, v)| (k.serialize(), v.serialize())) .map(|(k, v)| (k.to_flux(), v.to_flux()))
.collect::<BTreeMap<FluxValue, FluxValue>>(), .collect::<BTreeMap<FluxValue, FluxValue>>(),
) )
} }
} }
impl<K: Deserialize + Ord, V: Deserialize> Deserialize for BTreeMap<K, V> { impl<K: From<FluxValue> + Ord, V: From<FluxValue>> From<FluxValue> for BTreeMap<K, V> {
fn deserialize(v: FluxValue) -> Self { fn from(value: FluxValue) -> Self {
match v { match value {
FluxValue::Map(m) => m FluxValue::Map(m) => m
.iter() .iter()
.map(|(k, v)| (K::deserialize(k.clone()), V::deserialize(v.clone()))) .map(|(k, v)| (K::from(k.clone()), V::from(v.clone())))
.collect::<BTreeMap<K, V>>(), .collect::<BTreeMap<K, V>>(),
v => panic!("Expected Int, instead got {v:?}"), v => panic!("Expected Int, instead got {v:?}"),
} }