use fvm_ipld_encoding::{to_vec, RawBytes};
use serde::{de, ser};
use crate::v13::ActorError;
pub fn serialize_vec<T>(value: &T, desc: &str) -> Result<Vec<u8>, ActorError>
where
T: ser::Serialize + ?Sized,
{
to_vec(value)
.map_err(|e| ActorError::serialization(format!("failed to serialize {}: {}", desc, e)))
}
pub fn serialize<T>(value: &T, desc: &str) -> Result<RawBytes, ActorError>
where
T: ser::Serialize + ?Sized,
{
Ok(RawBytes::new(serialize_vec(value, desc)?))
}
pub fn deserialize<O: de::DeserializeOwned>(v: &RawBytes, desc: &str) -> Result<O, ActorError> {
v.deserialize()
.map_err(|e| ActorError::serialization(format!("failed to deserialize {}: {}", desc, e)))
}
pub fn deserialize_params<O: de::DeserializeOwned>(params: &RawBytes) -> Result<O, ActorError> {
deserialize(params, "method parameters")
}