2021-06-24 23:37:45 +08:00
|
|
|
use crate::{
|
2021-06-25 23:53:13 +08:00
|
|
|
error::SystemError,
|
2021-06-24 23:37:45 +08:00
|
|
|
request::FlowyRequest,
|
2021-06-25 23:53:13 +08:00
|
|
|
response::{data::ResponseData, Responder},
|
2021-06-24 23:37:45 +08:00
|
|
|
};
|
2021-06-26 23:52:03 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_with::skip_serializing_none;
|
|
|
|
use std::{fmt, fmt::Formatter};
|
2021-06-24 16:32:36 +08:00
|
|
|
|
2021-06-26 23:52:03 +08:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
|
2021-06-24 16:32:36 +08:00
|
|
|
pub enum StatusCode {
|
|
|
|
Success,
|
|
|
|
Error,
|
|
|
|
}
|
|
|
|
|
2021-06-26 23:52:03 +08:00
|
|
|
#[skip_serializing_none]
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
2021-06-24 16:32:36 +08:00
|
|
|
pub struct FlowyResponse<T = ResponseData> {
|
|
|
|
pub data: T,
|
|
|
|
pub status: StatusCode,
|
2021-06-26 23:52:03 +08:00
|
|
|
#[serde(skip)]
|
2021-06-25 23:53:13 +08:00
|
|
|
pub error: Option<SystemError>,
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FlowyResponse {
|
|
|
|
pub fn new(status: StatusCode) -> Self {
|
|
|
|
FlowyResponse {
|
|
|
|
data: ResponseData::None,
|
|
|
|
status,
|
|
|
|
error: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 23:52:03 +08:00
|
|
|
impl std::fmt::Display for FlowyResponse {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
match serde_json::to_string(self) {
|
|
|
|
Ok(json) => f.write_fmt(format_args!("{:?}", json))?,
|
|
|
|
Err(e) => f.write_fmt(format_args!("{:?}", e))?,
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 16:32:36 +08:00
|
|
|
impl Responder for FlowyResponse {
|
|
|
|
#[inline]
|
2021-06-24 23:37:45 +08:00
|
|
|
fn respond_to(self, _: &FlowyRequest) -> FlowyResponse { self }
|
2021-06-24 16:32:36 +08:00
|
|
|
}
|