59 lines
1.4 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::{
2021-06-25 23:53:13 +08:00
error::SystemError,
request::payload::Payload,
2021-06-24 23:37:45 +08:00
util::ready::{ready, Ready},
};
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,
2021-06-27 15:11:41 +08:00
event: String,
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(event: String) -> EventRequest {
2021-06-26 23:52:03 +08:00
Self {
id: uuid::Uuid::new_v4().to_string(),
2021-06-27 15:11:41 +08:00
event,
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
}
2021-06-27 15:11:41 +08:00
pub fn get_event(&self) -> &str { &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<String, 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("".to_string())) }
2021-06-24 23:37:45 +08:00
}