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)]
|
2022-01-23 12:14:00 +08:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2021-07-01 15:40:26 +08:00
|
|
|
impl ResponseBuilder {
|
2021-06-24 16:32:36 +08:00
|
|
|
pub fn new(status: StatusCode) -> Self {
|
2021-07-01 15:40:26 +08:00
|
|
|
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);
|
2021-08-31 11:32:51 +08:00
|
|
|
static_response!(Internal, StatusCode::Internal);
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|