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 Session {
72 #[serde(rename(serialize = "id", deserialize = "$id"))]
73 pub id: String,
74 pub userId: String,
75 pub expire: i64,
76 pub provider: String,
77 pub providerUid: String,
78 pub providerAccessToken: String,
79 pub providerAccessTokenExpiry: i64,
80 pub providerRefreshToken: String,
81 pub ip: String,
82 pub osCode: String,
83 pub osName: String,
84 pub osVersion: String,
85 pub clientType: String,
86 pub clientCode: String,
87 pub clientName: String,
88 pub clientVersion: String,
89 pub clientEngine: String,
90 pub clientEngineVersion: String,
91 pub deviceName: String,
92 pub deviceBrand: String,
93 pub deviceModel: String,
94 pub countryCode: String,
95 pub countryName: String,
96 pub current: bool,
97}
98
99impl Display for Session {
100 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101 let mut formatBuffer = String::new();
102 formatBuffer.push_str(&format!("{:?}", self.id));
103 formatBuffer.push_str(&format!("{:?}", self.userId));
104 formatBuffer.push_str(&format!("{:?}", self.expire));
105 formatBuffer.push_str(&format!("{:?}", self.provider));
106 formatBuffer.push_str(&format!("{:?}", self.providerUid));
107 formatBuffer.push_str(&format!("{:?}", self.providerAccessToken));
108 formatBuffer.push_str(&format!("{:?}", self.providerAccessTokenExpiry));
109 formatBuffer.push_str(&format!("{:?}", self.providerRefreshToken));
110 formatBuffer.push_str(&format!("{:?}", self.ip));
111 formatBuffer.push_str(&format!("{:?}", self.osCode));
112 formatBuffer.push_str(&format!("{:?}", self.osName));
113 formatBuffer.push_str(&format!("{:?}", self.osVersion));
114 formatBuffer.push_str(&format!("{:?}", self.clientType));
115 formatBuffer.push_str(&format!("{:?}", self.clientCode));
116 formatBuffer.push_str(&format!("{:?}", self.clientName));
117 formatBuffer.push_str(&format!("{:?}", self.clientVersion));
118 formatBuffer.push_str(&format!("{:?}", self.clientEngine));
119 formatBuffer.push_str(&format!("{:?}", self.clientEngineVersion));
120 formatBuffer.push_str(&format!("{:?}", self.deviceName));
121 formatBuffer.push_str(&format!("{:?}", self.deviceBrand));
122 formatBuffer.push_str(&format!("{:?}", self.deviceModel));
123 formatBuffer.push_str(&format!("{:?}", self.countryCode));
124 formatBuffer.push_str(&format!("{:?}", self.countryName));
125 formatBuffer.push_str(&format!("{:?}", self.current));
126
127 write!(f, "{}", formatBuffer)
128 }
129}
130
131impl Session {
132 pub fn new(id: String, userId: String, expire: i64, provider: String, providerUid: String, providerAccessToken: String, providerAccessTokenExpiry: i64, providerRefreshToken: String, ip: String, 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, current: bool, ) -> Self {
133 Self {
134 id: id,
135 userId: userId,
136 expire: expire,
137 provider: provider,
138 providerUid: providerUid,
139 providerAccessToken: providerAccessToken,
140 providerAccessTokenExpiry: providerAccessTokenExpiry,
141 providerRefreshToken: providerRefreshToken,
142 ip: ip,
143 osCode: osCode,
144 osName: osName,
145 osVersion: osVersion,
146 clientType: clientType,
147 clientCode: clientCode,
148 clientName: clientName,
149 clientVersion: clientVersion,
150 clientEngine: clientEngine,
151 clientEngineVersion: clientEngineVersion,
152 deviceName: deviceName,
153 deviceBrand: deviceBrand,
154 deviceModel: deviceModel,
155 countryCode: countryCode,
156 countryName: countryName,
157 current: current,
158 }
159 }
160}