#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(tag = "status", rename_all = "lowercase"))]
pub enum JSendResponse<T> {
Success {
data: Option<T>,
},
Fail {
data: T,
},
Error {
message: String,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
code: Option<i64>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
data: Option<T>,
},
}
impl<T> JSendResponse<T> {
pub fn success(data: Option<T>) -> JSendResponse<T> {
JSendResponse::Success { data }
}
pub fn fail(data: T) -> JSendResponse<T> {
JSendResponse::Fail { data }
}
pub fn error(message: String, code: Option<i64>, data: Option<T>) -> JSendResponse<T> {
JSendResponse::Error {
message,
code,
data,
}
}
pub fn data(&self) -> Option<&T> {
match self {
JSendResponse::Success { data } => data.as_ref(),
JSendResponse::Fail { data } => Some(data),
JSendResponse::Error { data, .. } => data.as_ref(),
}
}
pub fn message(&self) -> Option<&String> {
match self {
JSendResponse::Error { message, .. } => Some(message),
_ => None,
}
}
pub fn code(&self) -> Option<&i64> {
match self {
JSendResponse::Error { code, .. } => code.as_ref(),
_ => None,
}
}
}
#[cfg(test)]
mod test {
use std::collections::HashMap;
use super::*;
#[test]
fn test_success_variant() {
let data = HashMap::from([("key", "value")]);
let response = JSendResponse::success(Some(data.clone()));
assert_eq!(Some(data).as_ref(), response.data());
assert_eq!(None, response.code());
assert_eq!(None, response.message());
}
#[test]
fn test_success_variant_no_data() {
let response: JSendResponse<HashMap<&str, &str>> = JSendResponse::success(None);
assert_eq!(None, response.data());
assert_eq!(None, response.code());
assert_eq!(None, response.message());
}
#[test]
fn test_fail_variant() {
let data = HashMap::from([("key", "value")]);
let response = JSendResponse::fail(data.clone());
assert_eq!(Some(data).as_ref(), response.data());
assert_eq!(None, response.code());
assert_eq!(None, response.message());
}
#[test]
fn test_fail_variant_no_data() {
let data: Option<String> = None;
let response = JSendResponse::fail(data.clone());
assert_eq!(Some(data).as_ref(), response.data());
assert_eq!(None, response.code());
assert_eq!(None, response.message());
}
#[test]
fn test_error_variant() {
let message = "error message".to_string();
let code = 123;
let data = HashMap::from([("key", "value")]);
let response = JSendResponse::error(message.clone(), Some(code), Some(data.clone()));
assert_eq!(Some(message).as_ref(), response.message());
assert_eq!(Some(code).as_ref(), response.code());
assert_eq!(Some(data).as_ref(), response.data());
}
#[test]
fn test_error_variant_only_message() {
let message = "error message".to_string();
let response: JSendResponse<String> = JSendResponse::error(message.clone(), None, None);
assert_eq!(Some(message).as_ref(), response.message());
assert_eq!(None, response.code());
assert_eq!(None, response.data());
}
}