42 lines
974 B
Rust
Raw Normal View History

2021-06-25 23:53:13 +08:00
use crate::{
2021-07-03 22:24:02 +08:00
request::Payload,
response::{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)]
pub fn $name() -> ResponseBuilder { ResponseBuilder::new($status) }
2021-06-24 16:32:36 +08:00
};
}
2021-07-03 22:24:02 +08:00
pub struct ResponseBuilder<T = Payload> {
pub payload: T,
2021-06-24 16:32:36 +08:00
pub status: StatusCode,
}
impl ResponseBuilder {
2021-06-24 16:32:36 +08:00
pub fn new(status: StatusCode) -> Self {
ResponseBuilder {
2021-07-03 22:24:02 +08:00
payload: Payload::None,
2021-06-24 16:32:36 +08:00
status,
}
}
2021-07-03 22:24:02 +08:00
pub fn data<D: std::convert::Into<Payload>>(mut self, data: D) -> Self {
self.payload = data.into();
2021-06-24 16:32:36 +08:00
self
}
2021-06-27 15:11:41 +08:00
pub fn build(self) -> EventResponse {
EventResponse {
2021-07-03 22:24:02 +08:00
payload: self.payload,
status_code: self.status,
2021-06-24 16:32:36 +08:00
}
}
2021-06-27 15:11:41 +08:00
static_response!(Ok, StatusCode::Ok);
static_response!(Err, StatusCode::Err);
static_response!(Internal, StatusCode::Internal);
2021-06-24 16:32:36 +08:00
}