Skip to main content

aw_test/models/
log.rs

1#![allow(unused)]
2use serde::{Deserialize, Serialize, Deserializer};
3use std::collections::HashMap;
4use serde_json::value::Value;
5use std::fmt::Display;
6use super::*;
7
8#[derive(Debug, Serialize, Clone)]
9#[serde(deny_unknown_fields)]
10#[serde(untagged)]
11pub enum EmptyOption<T> {
12    Some(T),
13    None {},
14}
15
16impl<T> Display for EmptyOption<T>
17where
18    T: Display,
19{
20    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
21        match self {
22            EmptyOption::Some(t) => write!(f, "{}", t),
23            EmptyOption::None {} => write!(f, ""),
24        }
25    }
26}
27
28impl<'de, T> Deserialize<'de> for EmptyOption<T>
29where
30    T: Deserialize<'de>,
31{
32    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
33    where
34        D: Deserializer<'de>,
35    {
36        Option::deserialize(deserializer).map(Into::into)
37    }
38}
39
40impl<T> From<EmptyOption<T>> for Option<T> {
41    fn from(empty_option: EmptyOption<T>) -> Option<T> {
42        match empty_option {
43            EmptyOption::Some(option) => Some(option),
44            EmptyOption::None {} => None,
45        }
46    }
47}
48
49impl<T> From<Option<T>> for EmptyOption<T> {
50    fn from(option: Option<T>) -> EmptyOption<T> {
51        match option {
52            Some(option) => EmptyOption::Some(option),
53            None {} => EmptyOption::None {},
54        }
55    }
56}
57
58impl<T> EmptyOption<T> {
59    fn into_option(self) -> Option<T> {
60        self.into()
61    }
62    fn as_option(&self) -> Option<&T> {
63        match self {
64            EmptyOption::Some(option) => Some(option),
65            EmptyOption::None {} => None,
66        }
67    }
68}
69
70#[derive(Serialize, Deserialize, Debug, Clone)]
71pub struct Log {
72        pub event: String,
73        pub userId: String,
74        pub userEmail: String,
75        pub userName: String,
76        pub mode: String,
77        pub ip: String,
78        pub time: i64,
79        pub osCode: String,
80        pub osName: String,
81        pub osVersion: String,
82        pub clientType: String,
83        pub clientCode: String,
84        pub clientName: String,
85        pub clientVersion: String,
86        pub clientEngine: String,
87        pub clientEngineVersion: String,
88        pub deviceName: String,
89        pub deviceBrand: String,
90        pub deviceModel: String,
91        pub countryCode: String,
92        pub countryName: String,
93}
94
95impl Display for Log {
96    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
97        let mut formatBuffer = String::new();
98        formatBuffer.push_str(&format!("{:?}", self.event));
99        formatBuffer.push_str(&format!("{:?}", self.userId));
100        formatBuffer.push_str(&format!("{:?}", self.userEmail));
101        formatBuffer.push_str(&format!("{:?}", self.userName));
102        formatBuffer.push_str(&format!("{:?}", self.mode));
103        formatBuffer.push_str(&format!("{:?}", self.ip));
104        formatBuffer.push_str(&format!("{:?}", self.time));
105        formatBuffer.push_str(&format!("{:?}", self.osCode));
106        formatBuffer.push_str(&format!("{:?}", self.osName));
107        formatBuffer.push_str(&format!("{:?}", self.osVersion));
108        formatBuffer.push_str(&format!("{:?}", self.clientType));
109        formatBuffer.push_str(&format!("{:?}", self.clientCode));
110        formatBuffer.push_str(&format!("{:?}", self.clientName));
111        formatBuffer.push_str(&format!("{:?}", self.clientVersion));
112        formatBuffer.push_str(&format!("{:?}", self.clientEngine));
113        formatBuffer.push_str(&format!("{:?}", self.clientEngineVersion));
114        formatBuffer.push_str(&format!("{:?}", self.deviceName));
115        formatBuffer.push_str(&format!("{:?}", self.deviceBrand));
116        formatBuffer.push_str(&format!("{:?}", self.deviceModel));
117        formatBuffer.push_str(&format!("{:?}", self.countryCode));
118        formatBuffer.push_str(&format!("{:?}", self.countryName));
119
120        write!(f, "{}", formatBuffer)
121    }
122}
123
124impl Log {
125    pub fn new(event: String, userId: String, userEmail: String, userName: String, mode: String, ip: String, time: i64, osCode: String, osName: String, osVersion: String, clientType: String, clientCode: String, clientName: String, clientVersion: String, clientEngine: String, clientEngineVersion: String, deviceName: String, deviceBrand: String, deviceModel: String, countryCode: String, countryName: String, ) -> Self {
126        Self {
127            event: event,
128            userId: userId,
129            userEmail: userEmail,
130            userName: userName,
131            mode: mode,
132            ip: ip,
133            time: time,
134            osCode: osCode,
135            osName: osName,
136            osVersion: osVersion,
137            clientType: clientType,
138            clientCode: clientCode,
139            clientName: clientName,
140            clientVersion: clientVersion,
141            clientEngine: clientEngine,
142            clientEngineVersion: clientEngineVersion,
143            deviceName: deviceName,
144            deviceBrand: deviceBrand,
145            deviceModel: deviceModel,
146            countryCode: countryCode,
147            countryName: countryName,
148            }
149    }
150}