2021-08-21 04:11:33 +00:00
|
|
|
use crate::errors::{DispatchError, InternalError};
|
2021-08-20 14:00:03 +00:00
|
|
|
use bytes::Bytes;
|
2021-08-22 07:32:48 +00:00
|
|
|
|
2021-07-11 09:38:03 +00:00
|
|
|
// To bytes
|
|
|
|
pub trait ToBytes {
|
2021-08-21 04:11:33 +00:00
|
|
|
fn into_bytes(self) -> Result<Bytes, DispatchError>;
|
2021-07-11 09:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "use_protobuf")]
|
|
|
|
impl<T> ToBytes for T
|
|
|
|
where
|
2021-08-21 04:11:33 +00:00
|
|
|
T: std::convert::TryInto<Bytes, Error = protobuf::ProtobufError>,
|
2021-07-11 09:38:03 +00:00
|
|
|
{
|
2021-08-21 04:11:33 +00:00
|
|
|
fn into_bytes(self) -> Result<Bytes, DispatchError> {
|
|
|
|
match self.try_into() {
|
|
|
|
Ok(data) => Ok(data),
|
2022-08-15 14:40:54 +00:00
|
|
|
Err(e) => Err(InternalError::ProtobufError(format!(
|
|
|
|
"Serial {:?} to bytes failed:{:?}",
|
|
|
|
std::any::type_name::<T>(),
|
|
|
|
e
|
|
|
|
))
|
|
|
|
.into()),
|
2021-08-21 04:11:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-11 09:38:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 08:27:17 +00:00
|
|
|
// #[cfg(feature = "use_serde")]
|
|
|
|
// impl<T> ToBytes for T
|
|
|
|
// where
|
|
|
|
// T: serde::Serialize,
|
|
|
|
// {
|
|
|
|
// fn into_bytes(self) -> Result<Bytes, DispatchError> {
|
|
|
|
// match serde_json::to_string(&self.0) {
|
|
|
|
// Ok(s) => Ok(Bytes::from(s)),
|
|
|
|
// Err(e) => Err(InternalError::SerializeToBytes(format!("{:?}", e)).into()),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2021-07-11 09:38:03 +00:00
|
|
|
|
|
|
|
// From bytes
|
|
|
|
|
2022-12-01 00:35:50 +00:00
|
|
|
pub trait AFPluginFromBytes: Sized {
|
2021-08-21 04:11:33 +00:00
|
|
|
fn parse_from_bytes(bytes: Bytes) -> Result<Self, DispatchError>;
|
2021-07-11 09:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "use_protobuf")]
|
2022-12-01 00:35:50 +00:00
|
|
|
impl<T> AFPluginFromBytes for T
|
2021-07-11 09:38:03 +00:00
|
|
|
where
|
2021-08-21 04:34:11 +00:00
|
|
|
// // https://stackoverflow.com/questions/62871045/tryfromu8-trait-bound-in-trait
|
|
|
|
// T: for<'a> std::convert::TryFrom<&'a Bytes, Error =
|
|
|
|
// protobuf::ProtobufError>,
|
|
|
|
T: std::convert::TryFrom<Bytes, Error = protobuf::ProtobufError>,
|
2021-07-11 09:38:03 +00:00
|
|
|
{
|
2021-08-21 04:11:33 +00:00
|
|
|
fn parse_from_bytes(bytes: Bytes) -> Result<Self, DispatchError> {
|
2022-04-26 23:58:40 +00:00
|
|
|
match T::try_from(bytes) {
|
|
|
|
Ok(data) => Ok(data),
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!(
|
|
|
|
"Parse payload to {} failed with error: {:?}",
|
|
|
|
std::any::type_name::<T>(),
|
|
|
|
e
|
|
|
|
);
|
|
|
|
Err(e.into())
|
|
|
|
}
|
|
|
|
}
|
2021-08-21 04:11:33 +00:00
|
|
|
}
|
2021-07-11 09:38:03 +00:00
|
|
|
}
|
2023-01-17 08:27:17 +00:00
|
|
|
//
|
|
|
|
// #[cfg(feature = "use_serde")]
|
|
|
|
// impl<T> AFPluginFromBytes for T
|
|
|
|
// where
|
|
|
|
// T: serde::de::DeserializeOwned + 'static,
|
|
|
|
// {
|
|
|
|
// fn parse_from_bytes(bytes: Bytes) -> Result<Self, String> {
|
|
|
|
// let s = String::from_utf8_lossy(&bytes);
|
|
|
|
//
|
|
|
|
// match serde_json::from_str::<T>(s.as_ref()) {
|
|
|
|
// Ok(data) => Ok(data),
|
|
|
|
// Err(e) => Err(format!("{:?}", e)),
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|