63 lines
1.5 KiB
Rust
Raw Normal View History

2021-06-24 23:37:45 +08:00
use crate::{
2021-07-06 14:14:47 +08:00
data::Data,
2021-06-25 23:53:13 +08:00
error::SystemError,
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-06-26 23:52:03 +08:00
use std::{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-03 22:24:02 +08:00
#[derive(Debug, Clone, serde::Serialize)]
2021-06-27 15:11:41 +08:00
pub struct EventResponse {
2021-07-03 22:24:02 +08:00
pub payload: Payload,
pub status_code: StatusCode,
2021-06-25 23:53:13 +08:00
pub error: Option<SystemError>,
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
error: None,
}
}
}
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-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
}
match &self.error {
Some(e) => f.write_fmt(format_args!("Error: {:?}", e))?,
None => {},
}
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-06 14:14:47 +08:00
pub type ResponseResult<T, E> = std::result::Result<Data<T>, E>;
pub fn response_ok<T, E>(data: T) -> Result<Data<T>, E>
where
E: Into<SystemError>,
{
Ok(Data(data))
}