use crate::constants::*;
use crate::utils::{deserialize_option_u32_from_str_or_num, deserialize_u32_from_str_or_num};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct R<T> {
pub success: bool,
pub code: i32,
pub msg: Option<String>,
pub data: Option<T>,
#[serde(skip)]
pub def_exec: bool,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub path: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub version: String,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub base_version: String,
pub timestamp: i64,
}
impl<T> R<T> {
pub fn new(code: i32, msg: Option<String>, data: Option<T>) -> Self {
Self {
success: code == SUCCESS_CODE,
code,
msg,
data,
def_exec: false,
path: String::new(),
version: String::new(),
base_version: String::new(),
timestamp: chrono::Local::now().timestamp_millis(),
}
}
pub fn ok() -> Self {
Self::new(SUCCESS_CODE, Some(SUCCESS_MESSAGE.into()), None)
}
pub fn ok_with_data(data: T) -> Self {
Self::new(SUCCESS_CODE, Some(SUCCESS_MESSAGE.into()), Some(data))
}
pub fn fail() -> Self {
Self::new(OPERATION_EX_CODE, None, None)
}
pub fn fail_with_code(code: i32, msg: String) -> Self {
Self::new(code, Some(msg.into()), None)
}
pub fn fail_with_message(msg: String) -> Self {
Self::new(FAIL_CODE, Some(msg.into()), None)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CursorPageBaseResp<T> {
#[serde(skip_serializing_if = "Option::is_none")]
pub cursor: Option<u32>,
pub has_next: bool,
pub list: Vec<T>,
pub total: i64,
}
impl<T> CursorPageBaseResp<T> {
pub fn init(cursor: Option<u32>, has_next: bool, list: Vec<T>, total: i64) -> Self {
Self {
cursor,
has_next,
list,
total,
}
}
pub fn is_empty(&self) -> bool {
self.list.is_empty()
}
pub fn empty() -> Self {
Self {
cursor: None,
has_next: false,
list: Vec::new(),
total: 0,
}
}
}
impl<T> Default for CursorPageBaseResp<T> {
fn default() -> Self {
Self {
cursor: None,
has_next: false,
list: Vec::new(),
total: 0,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CursorPageBaseReq {
#[serde(
default = "CursorPageBaseReq::default_page_size",
deserialize_with = "deserialize_u32_from_str_or_num"
)]
pub page_size: u32,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_option_u32_from_str_or_num"
)]
pub cursor: Option<u32>,
}
impl CursorPageBaseReq {
fn default_page_size() -> u32 {
10
}
pub fn is_first_page(&self) -> bool {
self.cursor
.as_ref()
.map(|s| *s == 0 || *s < self.page_size)
.unwrap_or(true)
}
pub fn plus_page(&self) -> (u32, u32) {
(1, self.page_size)
}
}
impl Default for CursorPageBaseReq {
fn default() -> Self {
Self {
page_size: Self::default_page_size(),
cursor: None,
}
}
}