48 lines
1.1 KiB
Rust
Raw Normal View History

2021-06-25 23:53:13 +08:00
use crate::{
error::SystemError,
response::{data::ResponseData, FlowyResponse, StatusCode},
};
2021-06-24 16:32:36 +08:00
macro_rules! static_response {
($name:ident, $status:expr) => {
#[allow(non_snake_case, missing_docs)]
2021-06-25 23:53:13 +08:00
pub fn $name() -> FlowyResponseBuilder { FlowyResponseBuilder::new($status) }
2021-06-24 16:32:36 +08:00
};
}
pub struct FlowyResponseBuilder<T = ResponseData> {
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
}
impl FlowyResponseBuilder {
pub fn new(status: StatusCode) -> Self {
FlowyResponseBuilder {
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-25 23:53:13 +08:00
pub fn error(mut self, error: Option<SystemError>) -> Self {
2021-06-24 16:32:36 +08:00
self.error = error;
self
}
pub fn build(self) -> FlowyResponse {
FlowyResponse {
data: self.data,
status: self.status,
error: self.error,
}
}
static_response!(Ok, StatusCode::Success);
}