2021-06-24 23:37:45 +08:00
|
|
|
use crate::{
|
2021-07-15 08:46:16 +08:00
|
|
|
byte_trait::FromBytes,
|
2021-07-06 14:14:47 +08:00
|
|
|
data::Data,
|
2021-07-15 08:46:16 +08:00
|
|
|
errors::{DispatchError, InternalError},
|
2021-07-06 14:14:47 +08:00
|
|
|
request::{EventRequest, Payload},
|
2021-07-03 22:24:02 +08:00
|
|
|
response::Responder,
|
2021-06-24 23:37:45 +08:00
|
|
|
};
|
2021-07-09 14:02:42 +08:00
|
|
|
use derivative::*;
|
2021-07-15 08:46:16 +08:00
|
|
|
use std::{convert::TryFrom, fmt, fmt::Formatter};
|
2021-06-24 16:32:36 +08:00
|
|
|
|
2021-07-03 22:24:02 +08:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
|
2021-06-24 16:32:36 +08:00
|
|
|
pub enum StatusCode {
|
2021-06-30 23:11:27 +08:00
|
|
|
Ok = 0,
|
|
|
|
Err = 1,
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-27 15:11:41 +08:00
|
|
|
// serde user guide: https://serde.rs/field-attrs.html
|
2021-07-09 14:02:42 +08:00
|
|
|
#[derive(Debug, Clone, serde::Serialize, Derivative)]
|
2021-06-27 15:11:41 +08:00
|
|
|
pub struct EventResponse {
|
2021-07-09 14:02:42 +08:00
|
|
|
#[derivative(Debug = "ignore")]
|
2021-07-03 22:24:02 +08:00
|
|
|
pub payload: Payload,
|
|
|
|
pub status_code: StatusCode,
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-27 15:11:41 +08:00
|
|
|
impl EventResponse {
|
2021-07-03 22:24:02 +08:00
|
|
|
pub fn new(status_code: StatusCode) -> Self {
|
2021-06-27 15:11:41 +08:00
|
|
|
EventResponse {
|
2021-07-03 22:24:02 +08:00
|
|
|
payload: Payload::None,
|
|
|
|
status_code,
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
|
|
|
}
|
2021-07-15 08:46:16 +08:00
|
|
|
|
|
|
|
pub fn parse<T, E>(self) -> Result<Result<T, E>, DispatchError>
|
|
|
|
where
|
|
|
|
T: FromBytes,
|
|
|
|
E: FromBytes,
|
|
|
|
{
|
|
|
|
if self.status_code == StatusCode::Err {
|
|
|
|
match <Data<E>>::try_from(self.payload) {
|
|
|
|
Ok(err) => Ok(Err(err.into_inner())),
|
|
|
|
Err(e) => Err(InternalError::new(e).into()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match <Data<T>>::try_from(self.payload) {
|
|
|
|
Ok(a) => Ok(Ok(a.into_inner())),
|
|
|
|
Err(e) => Err(InternalError::new(e).into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
|
|
|
|
2021-06-27 15:11:41 +08:00
|
|
|
impl std::fmt::Display for EventResponse {
|
2021-06-26 23:52:03 +08:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
2021-07-03 22:24:02 +08:00
|
|
|
f.write_fmt(format_args!("Status_Code: {:?}", self.status_code))?;
|
2021-06-29 23:21:25 +08:00
|
|
|
|
2021-07-03 22:24:02 +08:00
|
|
|
match &self.payload {
|
|
|
|
Payload::Bytes(b) => f.write_fmt(format_args!("Data: {} bytes", b.len()))?,
|
|
|
|
Payload::None => f.write_fmt(format_args!("Data: Empty"))?,
|
2021-06-26 23:52:03 +08:00
|
|
|
}
|
2021-06-29 23:21:25 +08:00
|
|
|
|
2021-06-26 23:52:03 +08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 15:11:41 +08:00
|
|
|
impl Responder for EventResponse {
|
2021-06-24 16:32:36 +08:00
|
|
|
#[inline]
|
2021-06-27 15:11:41 +08:00
|
|
|
fn respond_to(self, _: &EventRequest) -> EventResponse { self }
|
|
|
|
}
|
|
|
|
|
2021-07-31 10:50:56 +08:00
|
|
|
pub type DataResult<T, E> = std::result::Result<Data<T>, E>;
|
2021-07-06 14:14:47 +08:00
|
|
|
|
2021-07-31 10:50:56 +08:00
|
|
|
pub fn data_result<T, E>(data: T) -> Result<Data<T>, E>
|
2021-07-01 15:40:26 +08:00
|
|
|
where
|
2021-07-10 16:27:20 +08:00
|
|
|
E: Into<DispatchError>,
|
2021-07-01 15:40:26 +08:00
|
|
|
{
|
|
|
|
Ok(Data(data))
|
|
|
|
}
|