108 lines
2.5 KiB
Rust
Raw Normal View History

2021-06-25 23:53:13 +08:00
use std::future::Future;
2021-06-24 23:37:45 +08:00
use crate::{
error::{InternalError, SystemError},
module::Event,
2021-06-25 23:53:13 +08:00
request::payload::Payload,
2021-06-24 23:37:45 +08:00
util::ready::{ready, Ready},
};
use futures_core::ready;
use std::{
fmt::{Debug, Display},
hash::Hash,
pin::Pin,
task::{Context, Poll},
};
2021-06-26 23:52:03 +08:00
#[derive(Clone, Debug)]
2021-06-27 15:11:41 +08:00
pub struct EventRequest {
2021-06-25 23:53:13 +08:00
id: String,
event: Event,
2021-06-28 14:27:16 +08:00
data: Option<Vec<u8>>,
2021-06-25 23:53:13 +08:00
}
2021-06-27 15:11:41 +08:00
impl EventRequest {
pub fn new<E>(event: E) -> EventRequest
where
E: Eq + Hash + Debug + Clone + Display,
{
2021-06-26 23:52:03 +08:00
Self {
id: uuid::Uuid::new_v4().to_string(),
event: event.into(),
2021-06-28 14:27:16 +08:00
data: None,
2021-06-26 23:52:03 +08:00
}
}
2021-06-24 23:37:45 +08:00
2021-06-28 14:27:16 +08:00
pub fn data(mut self, data: Vec<u8>) -> Self {
self.data = Some(data);
self
}
pub fn get_event(&self) -> &Event { &self.event }
2021-06-28 14:27:16 +08:00
2021-06-27 22:07:33 +08:00
pub fn get_id(&self) -> &str { &self.id }
2021-06-28 16:27:46 +08:00
pub fn from_data(_data: Vec<u8>) -> Self { unimplemented!() }
2021-06-24 23:37:45 +08:00
}
pub trait FromRequest: Sized {
2021-06-25 23:53:13 +08:00
type Error: Into<SystemError>;
2021-06-24 23:37:45 +08:00
type Future: Future<Output = Result<Self, Self::Error>>;
2021-06-27 15:11:41 +08:00
fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future;
2021-06-24 23:37:45 +08:00
}
#[doc(hidden)]
impl FromRequest for () {
2021-06-25 23:53:13 +08:00
type Error = SystemError;
type Future = Ready<Result<(), SystemError>>;
2021-06-24 23:37:45 +08:00
2021-06-27 15:11:41 +08:00
fn from_request(_req: &EventRequest, _payload: &mut Payload) -> Self::Future { ready(Ok(())) }
2021-06-24 23:37:45 +08:00
}
#[doc(hidden)]
impl FromRequest for String {
2021-06-25 23:53:13 +08:00
type Error = SystemError;
type Future = Ready<Result<Self, Self::Error>>;
fn from_request(req: &EventRequest, _payload: &mut Payload) -> Self::Future {
match &req.data {
None => ready(Err(InternalError::new("Expected string but request had data").into())),
Some(buf) => ready(Ok(String::from_utf8_lossy(buf).into_owned())),
}
}
}
#[doc(hidden)]
impl<T> FromRequest for Result<T, T::Error>
where
T: FromRequest,
{
type Error = SystemError;
type Future = FromRequestFuture<T::Future>;
2021-06-24 23:37:45 +08:00
fn from_request(req: &EventRequest, payload: &mut Payload) -> Self::Future {
FromRequestFuture {
fut: T::from_request(req, payload),
}
}
}
#[pin_project::pin_project]
pub struct FromRequestFuture<Fut> {
#[pin]
fut: Fut,
}
impl<Fut, T, E> Future for FromRequestFuture<Fut>
where
Fut: Future<Output = Result<T, E>>,
{
type Output = Result<Result<T, E>, SystemError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let res = ready!(this.fut.poll(cx));
Poll::Ready(Ok(res))
}
2021-06-24 23:37:45 +08:00
}