use crate::errors; pub trait Serializable { fn serialize(&self) -> errors::Result>; } #[cfg(feature = "serialize_bincode")] impl Serializable for T { fn serialize(&self) -> errors::Result> { bincode::serialize(self).map_err(|_| errors::WasmPluginError::SerializationError) } } #[cfg(feature = "serialize_json")] impl Serializable for T { fn serialize(&self) -> errors::Result> { serde_json::to_vec(self).map_err(|_| errors::WasmPluginError::SerializationError) } } #[cfg(feature = "serialize_nanoserde_json")] impl Serializable for T { fn serialize(&self) -> errors::Result> { Ok(nanoserde::SerJson::serialize_json(self).as_bytes().to_vec()) } } pub trait Deserializable { fn deserialize(data: &[u8]) -> errors::Result where Self: Sized; } #[cfg(feature = "serialize_bincode")] impl Deserializable for T { fn deserialize(data: &[u8]) -> errors::Result { bincode::deserialize(data).map_err(|_| errors::WasmPluginError::DeserializationError) } } #[cfg(feature = "serialize_json")] impl Deserializable for T { fn deserialize(data: &[u8]) -> errors::Result { serde_json::from_slice(data).map_err(|_| errors::WasmPluginError::DeserializationError) } } #[cfg(feature = "serialize_nanoserde_json")] impl Deserializable for T { fn deserialize(data: &[u8]) -> errors::Result { nanoserde::DeJson::deserialize_json( std::str::from_utf8(data).map_err(|_| errors::WasmPluginError::DeserializationError)?, ) .map_err(|_| errors::WasmPluginError::DeserializationError) } }