use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct Meta {
#[serde(default)]
pub endpoint: String,
#[serde(default)]
pub close: i32,
}
pub trait AppResponseView: Send + Sync {
fn code(&self) -> i32;
fn data(&self) -> &Value;
fn message(&self) -> &str;
fn meta(&self) -> &Meta;
fn is_ok(&self) -> bool {
self.code() == 200
}
fn to_app_response(&self) -> AppResponse {
AppResponse::from_parts(
self.code(),
self.data().clone(),
self.message().to_string(),
self.meta().clone(),
)
}
}
pub trait AppResponseMut: AppResponseView {
fn set_code(&mut self, code: i32);
fn set_data(&mut self, data: Value);
fn set_message(&mut self, message: impl Into<String>);
fn set_meta(&mut self, meta: Meta);
fn meta_mut(&mut self) -> &mut Meta;
fn with_endpoint(mut self, endpoint: impl Into<String>) -> Self
where
Self: Sized,
{
self.meta_mut().endpoint = endpoint.into();
self
}
fn with_close(mut self, close: i32) -> Self
where
Self: Sized,
{
self.meta_mut().close = close;
self
}
fn with_data(mut self, data: Value) -> Self
where
Self: Sized,
{
self.set_data(data);
self
}
fn with_message(mut self, message: impl Into<String>) -> Self
where
Self: Sized,
{
self.set_message(message);
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AppResponse {
pub code: i32,
pub data: Value,
#[serde(rename = "msg")]
pub message: String,
pub meta: Meta,
}
impl Default for AppResponse {
fn default() -> Self {
Self::ok(Value::Null)
}
}
impl AppResponse {
pub fn ok(data: Value) -> Self {
Self::from_parts(200, data, "OK".into(), Meta::default())
}
pub fn error(code: i32, message: impl Into<String>) -> Self {
Self::from_parts(code, Value::Null, message.into(), Meta::default())
}
pub fn from_parts(code: i32, data: Value, message: String, meta: Meta) -> Self {
Self {
code,
data,
message,
meta,
}
}
pub fn into_parts(self) -> (i32, Value, String, Meta) {
(self.code, self.data, self.message, self.meta)
}
pub fn is_ok(&self) -> bool {
AppResponseView::is_ok(self)
}
pub fn with_endpoint(self, endpoint: impl Into<String>) -> Self {
AppResponseMut::with_endpoint(self, endpoint)
}
pub fn with_close(self, close: i32) -> Self {
AppResponseMut::with_close(self, close)
}
pub fn with_data(self, data: Value) -> Self {
AppResponseMut::with_data(self, data)
}
pub fn with_message(self, message: impl Into<String>) -> Self {
AppResponseMut::with_message(self, message)
}
}
impl AppResponseView for AppResponse {
fn code(&self) -> i32 {
self.code
}
fn data(&self) -> &Value {
&self.data
}
fn message(&self) -> &str {
&self.message
}
fn meta(&self) -> &Meta {
&self.meta
}
}
impl AppResponseMut for AppResponse {
fn set_code(&mut self, code: i32) {
self.code = code;
}
fn set_data(&mut self, data: Value) {
self.data = data;
}
fn set_message(&mut self, message: impl Into<String>) {
self.message = message.into();
}
fn set_meta(&mut self, meta: Meta) {
self.meta = meta;
}
fn meta_mut(&mut self) -> &mut Meta {
&mut self.meta
}
}