1use serde::{
2 Deserialize, Deserializer, Serialize, Serializer,
3 de::{self, Visitor},
4};
5use std::fmt;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum EventKey {
9 Account(AccountEventKey),
10 Sync(SyncEventKey),
11}
12
13impl EventKey {
14 pub fn as_str(&self) -> &'static str {
15 match self {
16 EventKey::Account(key) => key.as_str(),
17 EventKey::Sync(key) => key.as_str(),
18 }
19 }
20
21 pub fn to_subject(&self) -> String {
22 match self {
23 EventKey::Account(account_key) => match account_key {
24 AccountEventKey::Welcome => "Welcome to Safe App".to_string(),
25 AccountEventKey::TierUpgrade => "Your Account Tier Has Been Upgraded".to_string(),
26 },
27 EventKey::Sync(sync_key) => match sync_key {
28 SyncEventKey::Connection(connection_key) => match connection_key {
29 ConnectionEventKey::Finished => "Connection Syncing Completed".to_string(),
30 ConnectionEventKey::Added => "New Connection Added".to_string(),
31 },
32 SyncEventKey::Disputer(disputer_key) => match disputer_key {
33 DisputerEventKey::PendingCharges(pending_key) => match pending_key {
34 PendingChargeEventKey::Payment => "Pending Charge Payment Processed".to_string(),
35 PendingChargeEventKey::LimitReached => "Pending Charges Limit Reached".to_string(),
36 PendingChargeEventKey::Added => "New Pending Charge Added".to_string(),
37 },
38 DisputerEventKey::Disputes(dispute_key) => match dispute_key {
39 DisputeEventKey::Created => "New Dispute Created".to_string(),
40 DisputeEventKey::Won => "Dispute Won".to_string(),
41 DisputeEventKey::Lost => "Dispute Lost".to_string(),
42 },
43 DisputerEventKey::Evidences(evidence_key) => match evidence_key {
44 EvidencesEventKey::Submitted => "Evidence Submitted for Dispute".to_string(),
45 },
46 },
47 },
48 }
49 }
50}
51
52impl Serialize for EventKey {
54 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
55 where
56 S: Serializer,
57 {
58 serializer.serialize_str(self.as_str())
59 }
60}
61
62impl<'de> Deserialize<'de> for EventKey {
64 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
65 where
66 D: Deserializer<'de>,
67 {
68 struct EventKeyVisitor;
69
70 impl<'de> Visitor<'de> for EventKeyVisitor {
71 type Value = EventKey;
72
73 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
74 formatter.write_str("a valid event key string")
75 }
76
77 fn visit_str<E>(self, value: &str) -> Result<EventKey, E>
78 where
79 E: de::Error,
80 {
81 match value {
82 "account.welcome" => Ok(keys::ACCOUNT_WELCOME),
83 "account.tier_upgrade" => Ok(keys::ACCOUNT_TIER_UPGRADE),
84 "sync.connection.finished" => Ok(keys::SYNC_CONNECTION_FINISHED),
85 "sync.connection.added" => Ok(keys::SYNC_CONNECTION_ADDED),
86 "sync.disputer.pending_charges.payment" => Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_PAYMENT),
87 "sync.disputer.pending_charges.limit_reached" => {
88 Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_LIMIT_REACHED)
89 }
90 "sync.disputer.pending_charges.added" => Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_ADDED),
91 "sync.disputer.disputes.created" => Ok(keys::SYNC_DISPUTER_DISPUTES_CREATED),
92 "sync.disputer.disputes.won" => Ok(keys::SYNC_DISPUTER_DISPUTES_WON),
93 "sync.disputer.disputes.lost" => Ok(keys::SYNC_DISPUTER_DISPUTES_LOST),
94 "sync.disputer.evidences.submitted" => Ok(keys::SYNC_DISPUTER_EVIDENCES_SUBMITTED),
95 unknown => {
96 eprintln!("Unknown event key '{}', expected a known key", unknown);
98 Err(de::Error::custom(format!("Unknown event key: {}", unknown)))
99 }
100 }
101 }
102 }
103
104 deserializer.deserialize_str(EventKeyVisitor)
105 }
106}
107
108#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
109pub enum AccountEventKey {
110 Welcome,
111 TierUpgrade,
112}
113
114impl AccountEventKey {
115 pub fn as_str(&self) -> &'static str {
116 match self {
117 AccountEventKey::Welcome => "account.welcome",
118 AccountEventKey::TierUpgrade => "account.tier_upgrade",
119 }
120 }
121}
122
123#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
124pub enum SyncEventKey {
125 Connection(ConnectionEventKey),
126 Disputer(DisputerEventKey),
127}
128
129impl SyncEventKey {
130 pub fn as_str(&self) -> &'static str {
131 match self {
132 SyncEventKey::Connection(key) => key.as_str(),
133 SyncEventKey::Disputer(key) => key.as_str(),
134 }
135 }
136}
137
138#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
139pub enum ConnectionEventKey {
140 Finished,
141 Added,
142}
143
144impl ConnectionEventKey {
145 pub fn as_str(&self) -> &'static str {
146 match self {
147 ConnectionEventKey::Finished => "sync.connection.finished",
148 ConnectionEventKey::Added => "sync.connection.added",
149 }
150 }
151}
152
153#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
154pub enum DisputerEventKey {
155 PendingCharges(PendingChargeEventKey),
156 Disputes(DisputeEventKey),
157 Evidences(EvidencesEventKey),
158}
159
160impl DisputerEventKey {
161 pub fn as_str(&self) -> &'static str {
162 match self {
163 DisputerEventKey::PendingCharges(key) => key.as_str(),
164 DisputerEventKey::Disputes(key) => key.as_str(),
165 DisputerEventKey::Evidences(key) => key.as_str(),
166 }
167 }
168}
169
170#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
171pub enum DisputeEventKey {
172 Created,
173 Won,
174 Lost,
175}
176
177impl DisputeEventKey {
178 pub fn as_str(&self) -> &'static str {
179 match self {
180 DisputeEventKey::Created => "sync.disputer.disputes.created",
181 DisputeEventKey::Won => "sync.disputer.disputes.won",
182 DisputeEventKey::Lost => "sync.disputer.disputes.lost",
183 }
184 }
185}
186
187#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
188pub enum PendingChargeEventKey {
189 Payment,
190 LimitReached,
191 Added,
192}
193
194impl PendingChargeEventKey {
195 pub fn as_str(&self) -> &'static str {
196 match self {
197 PendingChargeEventKey::Payment => "sync.disputer.pending_charges.payment",
198 PendingChargeEventKey::LimitReached => "sync.disputer.pending_charges.limit_reached",
199 PendingChargeEventKey::Added => "sync.disputer.pending_charges.added",
200 }
201 }
202}
203
204#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
205pub enum EvidencesEventKey {
206 Submitted,
207}
208
209impl EvidencesEventKey {
210 pub fn as_str(&self) -> &'static str {
211 match self {
212 EvidencesEventKey::Submitted => "sync.disputer.evidences.submitted",
213 }
214 }
215}
216
217pub mod keys {
218 use super::*;
219
220 pub const ACCOUNT_WELCOME: EventKey = EventKey::Account(AccountEventKey::Welcome);
221 pub const ACCOUNT_TIER_UPGRADE: EventKey = EventKey::Account(AccountEventKey::TierUpgrade);
222
223 pub const SYNC_CONNECTION_FINISHED: EventKey =
224 EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Finished));
225 pub const SYNC_CONNECTION_ADDED: EventKey = EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Added));
226
227 pub const SYNC_DISPUTER_PENDING_CHARGES_PAYMENT: EventKey = EventKey::Sync(SyncEventKey::Disputer(
228 DisputerEventKey::PendingCharges(PendingChargeEventKey::Payment),
229 ));
230 pub const SYNC_DISPUTER_PENDING_CHARGES_LIMIT_REACHED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
231 DisputerEventKey::PendingCharges(PendingChargeEventKey::LimitReached),
232 ));
233 pub const SYNC_DISPUTER_PENDING_CHARGES_ADDED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
234 DisputerEventKey::PendingCharges(PendingChargeEventKey::Added),
235 ));
236 pub const SYNC_DISPUTER_DISPUTES_CREATED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
237 DisputerEventKey::Disputes(DisputeEventKey::Created),
238 ));
239 pub const SYNC_DISPUTER_DISPUTES_WON: EventKey =
240 EventKey::Sync(SyncEventKey::Disputer(DisputerEventKey::Disputes(DisputeEventKey::Won)));
241 pub const SYNC_DISPUTER_DISPUTES_LOST: EventKey = EventKey::Sync(SyncEventKey::Disputer(
242 DisputerEventKey::Disputes(DisputeEventKey::Lost),
243 ));
244 pub const SYNC_DISPUTER_EVIDENCES_SUBMITTED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
245 DisputerEventKey::Evidences(EvidencesEventKey::Submitted),
246 ));
247}