43 lines
870 B
Rust
Raw Normal View History

2021-06-25 23:53:13 +08:00
use crate::{
request::Payload,
response::{AFPluginEventResponse, 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,
pub status: StatusCode,
2021-06-24 16:32:36 +08:00
}
impl ResponseBuilder {
pub fn new(status: StatusCode) -> Self {
ResponseBuilder {
payload: Payload::None,
status,
2021-06-24 16:32:36 +08:00
}
}
2021-06-24 16:32:36 +08:00
pub fn data<D: std::convert::Into<Payload>>(mut self, data: D) -> Self {
self.payload = data.into();
self
}
2021-06-24 16:32:36 +08:00
pub fn build(self) -> AFPluginEventResponse {
AFPluginEventResponse {
payload: self.payload,
status_code: self.status,
2021-06-24 16:32:36 +08:00
}
}
2021-06-24 16:32:36 +08:00
static_response!(Ok, StatusCode::Ok);
static_response!(Err, StatusCode::Err);
2021-06-24 16:32:36 +08:00
}