chimes_auth/
utils.rs

1
2use std::fmt::{Debug};
3use std::time::{SystemTime};
4use serde_derive::{Deserialize, Serialize};
5use chrono::offset::Local;
6use chrono::DateTime;
7
8pub fn get_local_timestamp() -> u64 {
9    let now = SystemTime::now();
10    let date:DateTime<Local> = now.into();
11    date.timestamp_millis() as u64
12}
13
14#[derive(Debug, Clone, Deserialize, Serialize)]
15pub struct  ApiResult <T> {
16    pub status: i32,
17    pub message: String,
18    pub data: Option<T>,
19    pub timestamp: Option<u64>,
20}
21
22impl<T> ApiResult<T> {
23
24    pub fn ok (dt: T) -> Self {
25        ApiResult {
26            status: 200,
27            message: "OK".to_string(),
28            data: Option::Some(dt),
29            timestamp: Some(get_local_timestamp())
30        }
31    }
32
33    pub fn error (code: i32, msg: &String) -> Self {
34        ApiResult {
35            status: code,
36            message: msg.to_owned(),
37            data: None,
38            timestamp: Some(get_local_timestamp())
39        }
40    }
41
42    pub fn new (code: i32, msg: &String, data: T, ts: u64) -> Self {
43        ApiResult {
44            status: code,
45            message: msg.to_owned(),
46            data: Some(data),
47            timestamp: Some(ts)
48        }
49    }
50}
51