1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

use std::fmt::{Debug};
use std::time::{SystemTime};
use serde_derive::{Deserialize, Serialize};
use chrono::offset::Local;
use chrono::DateTime;

pub fn get_local_timestamp() -> u64 {
    let now = SystemTime::now();
    let date:DateTime<Local> = now.into();
    date.timestamp_millis() as u64
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct  ApiResult <T> {
    pub status: i32,
    pub message: String,
    pub data: Option<T>,
    pub timestamp: Option<u64>,
}

impl<T> ApiResult<T> {

    pub fn ok (dt: T) -> Self {
        ApiResult {
            status: 200,
            message: "OK".to_string(),
            data: Option::Some(dt),
            timestamp: Some(get_local_timestamp())
        }
    }

    pub fn error (code: i32, msg: &String) -> Self {
        ApiResult {
            status: code,
            message: msg.to_owned(),
            data: None,
            timestamp: Some(get_local_timestamp())
        }
    }

    pub fn new (code: i32, msg: &String, data: T, ts: u64) -> Self {
        ApiResult {
            status: code,
            message: msg.to_owned(),
            data: Some(data),
            timestamp: Some(ts)
        }
    }
}