2021-07-06 06:14:47 +00:00
|
|
|
use crate::{
|
2021-07-11 09:38:03 +00:00
|
|
|
byte_trait::*,
|
2021-07-10 08:27:20 +00:00
|
|
|
errors::{DispatchError, InternalError},
|
2021-07-06 06:14:47 +00:00
|
|
|
request::{unexpected_none_payload, EventRequest, FromRequest, Payload},
|
2021-07-11 09:38:03 +00:00
|
|
|
response::{EventResponse, Responder, 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 Data<T>(pub T);
|
|
|
|
|
|
|
|
impl<T> Data<T> {
|
|
|
|
pub fn into_inner(self) -> T { self.0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ops::Deref for Data<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &T { &self.0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ops::DerefMut for Data<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut T { &mut self.0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> FromRequest for Data<T>
|
|
|
|
where
|
|
|
|
T: FromBytes + 'static,
|
|
|
|
{
|
2021-07-10 08:27:20 +00:00
|
|
|
type Error = DispatchError;
|
|
|
|
type Future = Ready<Result<Self, DispatchError>>;
|
2021-07-06 06:14:47 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
|
|
|
|
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()) {
|
2021-07-06 06:14:47 +00:00
|
|
|
Ok(data) => ready(Ok(Data(data))),
|
2021-08-21 04:11:33 +00:00
|
|
|
Err(e) => ready(Err(
|
|
|
|
InternalError::DeserializeFromBytes(format!("{}", e)).into()
|
|
|
|
)),
|
2021-07-06 06:14:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Responder for Data<T>
|
|
|
|
where
|
|
|
|
T: ToBytes,
|
|
|
|
{
|
|
|
|
fn respond_to(self, _request: &EventRequest) -> EventResponse {
|
|
|
|
match self.into_inner().into_bytes() {
|
2021-08-20 14:00:03 +00:00
|
|
|
Ok(bytes) => ResponseBuilder::Ok().data(bytes).build(),
|
2021-08-21 04:11:33 +00:00
|
|
|
Err(e) => e.into(),
|
2021-07-06 06:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> std::convert::TryFrom<&Payload> for Data<T>
|
|
|
|
where
|
|
|
|
T: FromBytes,
|
|
|
|
{
|
2021-08-21 04:11:33 +00:00
|
|
|
type Error = DispatchError;
|
2021-07-06 13:20:53 +00:00
|
|
|
fn try_from(payload: &Payload) -> Result<Data<T>, Self::Error> { parse_payload(payload) }
|
|
|
|
}
|
2021-07-06 06:14:47 +00:00
|
|
|
|
2021-07-06 13:20:53 +00:00
|
|
|
impl<T> std::convert::TryFrom<Payload> for Data<T>
|
|
|
|
where
|
|
|
|
T: FromBytes,
|
|
|
|
{
|
2021-08-21 04:11:33 +00:00
|
|
|
type Error = DispatchError;
|
2021-07-06 13:20:53 +00:00
|
|
|
fn try_from(payload: Payload) -> Result<Data<T>, Self::Error> { parse_payload(&payload) }
|
|
|
|
}
|
|
|
|
|
2021-08-21 04:11:33 +00:00
|
|
|
fn parse_payload<T>(payload: &Payload) -> Result<Data<T>, DispatchError>
|
2021-07-06 13:20:53 +00:00
|
|
|
where
|
|
|
|
T: FromBytes,
|
|
|
|
{
|
|
|
|
match payload {
|
2021-08-21 04:11:33 +00:00
|
|
|
Payload::None => {
|
|
|
|
Err(InternalError::UnexpectedNone(format!("Parse fail, expected payload")).into())
|
|
|
|
},
|
|
|
|
Payload::Bytes(bytes) => {
|
|
|
|
let data = T::parse_from_bytes(bytes.clone())?;
|
|
|
|
Ok(Data(data))
|
2021-07-06 13:20:53 +00:00
|
|
|
},
|
2021-07-06 06:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> std::convert::TryInto<Payload> for Data<T>
|
|
|
|
where
|
|
|
|
T: ToBytes,
|
|
|
|
{
|
2021-08-21 04:11:33 +00:00
|
|
|
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 Data<String> {
|
2021-08-21 04:11:33 +00:00
|
|
|
fn into_bytes(self) -> Result<Bytes, DispatchError> { Ok(Bytes::from(self.0)) }
|
2021-07-11 09:38:03 +00:00
|
|
|
}
|