49 lines
1.1 KiB
Rust
Raw Normal View History

2021-06-25 23:53:13 +08:00
use crate::{
error::SystemError,
2021-06-27 15:11:41 +08:00
response::{data::ResponseData, EventResponse, StatusCode},
2021-06-25 23:53:13 +08:00
};
2021-06-24 16:32:36 +08:00
macro_rules! static_response {
($name:ident, $status:expr) => {
#[allow(non_snake_case, missing_docs)]
2021-06-27 15:11:41 +08:00
pub fn $name() -> EventResponseBuilder { EventResponseBuilder::new($status) }
2021-06-24 16:32:36 +08:00
};
}
2021-06-27 15:11:41 +08:00
pub struct EventResponseBuilder<T = ResponseData> {
2021-06-24 16:32:36 +08:00
pub data: T,
pub status: 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 EventResponseBuilder {
2021-06-24 16:32:36 +08:00
pub fn new(status: StatusCode) -> Self {
2021-06-27 15:11:41 +08:00
EventResponseBuilder {
2021-06-24 16:32:36 +08:00
data: ResponseData::None,
status,
error: None,
}
}
pub fn data<D: std::convert::Into<ResponseData>>(mut self, data: D) -> Self {
self.data = data.into();
self
}
2021-06-30 23:11:27 +08:00
pub fn error(mut self, error: SystemError) -> Self {
self.error = Some(error);
2021-06-24 16:32:36 +08:00
self
}
2021-06-27 15:11:41 +08:00
pub fn build(self) -> EventResponse {
EventResponse {
2021-06-24 16:32:36 +08:00
data: self.data,
status: self.status,
error: self.error,
}
}
2021-06-27 15:11:41 +08:00
static_response!(Ok, StatusCode::Ok);
static_response!(Err, StatusCode::Err);
2021-06-24 16:32:36 +08:00
}