AppFlowy/frontend/rust-lib/lib-dispatch/src/data.rs

122 lines
3.1 KiB
Rust
Raw Normal View History

2021-07-06 06:14:47 +00:00
use crate::{
2021-07-11 09:38:03 +00:00
byte_trait::*,
errors::{DispatchError, InternalError},
request::{unexpected_none_payload, AFPluginEventRequest, FromAFPluginRequest, Payload},
2022-12-01 02:59:22 +00:00
response::{AFPluginEventResponse, AFPluginResponder, ResponseBuilder},
2021-07-06 06:14:47 +00:00
util::ready::{ready, Ready},
};
2021-08-20 14:00:03 +00:00
use bytes::Bytes;
2021-07-06 06:14:47 +00:00
use std::ops;
pub struct AFPluginData<T>(pub T);
2021-07-06 06:14:47 +00:00
impl<T> AFPluginData<T> {
2022-01-23 04:14:00 +00:00
pub fn into_inner(self) -> T {
self.0
}
2021-07-06 06:14:47 +00:00
}
impl<T> ops::Deref for AFPluginData<T> {
2021-07-06 06:14:47 +00:00
type Target = T;
2022-01-23 04:14:00 +00:00
fn deref(&self) -> &T {
&self.0
}
2021-07-06 06:14:47 +00:00
}
impl<T> ops::DerefMut for AFPluginData<T> {
2022-01-23 04:14:00 +00:00
fn deref_mut(&mut self) -> &mut T {
&mut self.0
}
2021-07-06 06:14:47 +00:00
}
impl<T> FromAFPluginRequest for AFPluginData<T>
2021-07-06 06:14:47 +00:00
where
T: AFPluginFromBytes + 'static,
2021-07-06 06:14:47 +00:00
{
type Error = DispatchError;
type Future = Ready<Result<Self, DispatchError>>;
2021-07-06 06:14:47 +00:00
#[inline]
fn from_request(req: &AFPluginEventRequest, payload: &mut Payload) -> Self::Future {
2021-07-06 06:14:47 +00:00
match payload {
Payload::None => ready(Err(unexpected_none_payload(req))),
2021-08-20 14:00:03 +00:00
Payload::Bytes(bytes) => match T::parse_from_bytes(bytes.clone()) {
Ok(data) => ready(Ok(AFPluginData(data))),
2021-09-13 07:51:13 +00:00
Err(e) => ready(Err(InternalError::DeserializeFromBytes(format!("{}", e)).into())),
2021-07-06 06:14:47 +00:00
},
}
}
}
impl<T> AFPluginResponder for AFPluginData<T>
2021-07-06 06:14:47 +00:00
where
T: ToBytes,
{
2022-12-01 02:59:22 +00:00
fn respond_to(self, _request: &AFPluginEventRequest) -> AFPluginEventResponse {
2021-07-06 06:14:47 +00:00
match self.into_inner().into_bytes() {
2022-08-15 14:40:54 +00:00
Ok(bytes) => {
log::trace!("Serialize Data: {:?} to event response", std::any::type_name::<T>());
2022-08-16 03:24:37 +00:00
ResponseBuilder::Ok().data(bytes).build()
2022-08-15 14:40:54 +00:00
}
Err(e) => e.into(),
2021-07-06 06:14:47 +00:00
}
}
}
impl<T> std::convert::TryFrom<&Payload> for AFPluginData<T>
2021-07-06 06:14:47 +00:00
where
T: AFPluginFromBytes,
2021-07-06 06:14:47 +00:00
{
type Error = DispatchError;
fn try_from(payload: &Payload) -> Result<AFPluginData<T>, Self::Error> {
2022-01-23 04:14:00 +00:00
parse_payload(payload)
}
}
2021-07-06 06:14:47 +00:00
impl<T> std::convert::TryFrom<Payload> for AFPluginData<T>
where
T: AFPluginFromBytes,
{
type Error = DispatchError;
fn try_from(payload: Payload) -> Result<AFPluginData<T>, Self::Error> {
2022-01-23 04:14:00 +00:00
parse_payload(&payload)
}
}
fn parse_payload<T>(payload: &Payload) -> Result<AFPluginData<T>, DispatchError>
where
T: AFPluginFromBytes,
{
match payload {
2022-08-15 14:40:54 +00:00
Payload::None => Err(InternalError::UnexpectedNone(format!(
"Parse fail, expected payload:{:?}",
std::any::type_name::<T>()
))
.into()),
Payload::Bytes(bytes) => {
let data = T::parse_from_bytes(bytes.clone())?;
Ok(AFPluginData(data))
2022-01-23 04:14:00 +00:00
}
2021-07-06 06:14:47 +00:00
}
}
impl<T> std::convert::TryInto<Payload> for AFPluginData<T>
2021-07-06 06:14:47 +00:00
where
T: ToBytes,
{
type Error = DispatchError;
2021-07-06 06:14:47 +00:00
fn try_into(self) -> Result<Payload, Self::Error> {
let inner = self.into_inner();
let bytes = inner.into_bytes()?;
Ok(Payload::Bytes(bytes))
}
}
2021-07-11 09:38:03 +00:00
impl ToBytes for AFPluginData<String> {
2022-01-23 04:14:00 +00:00
fn into_bytes(self) -> Result<Bytes, DispatchError> {
Ok(Bytes::from(self.0))
}
2021-07-11 09:38:03 +00:00
}