use crate::{
error::BamlError,
proto::baml_cffi_v1::{
host_map_entry, CffiValueClass, CffiValueHolder, HostMapEntry, HostValue,
},
};
pub trait BamlDecode: Sized {
fn baml_decode(holder: &CffiValueHolder) -> Result<Self, BamlError>;
}
pub trait BamlEncode {
fn baml_encode(&self) -> HostValue;
}
pub trait BamlSerializeMapKey: Sized + std::hash::Hash + Eq {
fn baml_encode_map_key(&self) -> host_map_entry::Key;
fn baml_decode_map_key(key: &str) -> Result<Self, BamlError>;
}
pub trait BamlClass: Sized {
const TYPE_NAME: &'static str;
fn from_class_value(class: &CffiValueClass) -> Result<Self, BamlError>;
}
pub trait BamlEnum: Sized {
const ENUM_NAME: &'static str;
fn from_variant_name(name: &str) -> Result<Self, BamlError>;
}
pub(crate) trait IntoKwargs {
fn into_kwargs(self) -> Vec<HostMapEntry>;
}
impl IntoKwargs for () {
fn into_kwargs(self) -> Vec<HostMapEntry> {
vec![]
}
}
impl IntoKwargs for Vec<HostMapEntry> {
fn into_kwargs(self) -> Vec<HostMapEntry> {
self
}
}
impl<V: BamlEncode> IntoKwargs for (&str, V) {
fn into_kwargs(self) -> Vec<HostMapEntry> {
vec![HostMapEntry {
key: Some(host_map_entry::Key::StringKey(self.0.to_string())),
value: Some(self.1.baml_encode()),
}]
}
}
impl<V: BamlEncode + Clone> IntoKwargs for &[(&str, V)] {
fn into_kwargs(self) -> Vec<HostMapEntry> {
self.iter()
.map(|(k, v)| HostMapEntry {
key: Some(host_map_entry::Key::StringKey((*k).to_string())),
value: Some(v.baml_encode()),
})
.collect()
}
}
impl<V1: BamlEncode, V2: BamlEncode> IntoKwargs for ((&str, V1), (&str, V2)) {
fn into_kwargs(self) -> Vec<HostMapEntry> {
vec![
HostMapEntry {
key: Some(host_map_entry::Key::StringKey(self.0 .0.to_string())),
value: Some(self.0 .1.baml_encode()),
},
HostMapEntry {
key: Some(host_map_entry::Key::StringKey(self.1 .0.to_string())),
value: Some(self.1 .1.baml_encode()),
},
]
}
}
pub(crate) trait DecodeHandle: Sized {
fn decode_handle(
handle: crate::proto::baml_cffi_v1::BamlObjectHandle,
runtime: *const std::ffi::c_void,
) -> Result<Self, BamlError>;
}