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}
94
95impl Serialize for EventKey {
97 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
98 where
99 S: Serializer,
100 {
101 serializer.serialize_str(self.as_str())
102 }
103}
104
105impl<'de> Deserialize<'de> for EventKey {
107 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
108 where
109 D: Deserializer<'de>,
110 {
111 struct EventKeyVisitor;
112
113 impl<'de> Visitor<'de> for EventKeyVisitor {
114 type Value = EventKey;
115
116 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
117 formatter.write_str("a valid event key string")
118 }
119
120 fn visit_str<E>(self, value: &str) -> Result<EventKey, E>
121 where
122 E: de::Error,
123 {
124 match value {
125 "account.welcome" => Ok(keys::ACCOUNT_WELCOME),
126 "account.tier_upgrade" => Ok(keys::ACCOUNT_TIER_UPGRADE),
127 "sync.connection.finished" => Ok(keys::SYNC_CONNECTION_FINISHED),
128 "sync.connection.added" => Ok(keys::SYNC_CONNECTION_ADDED),
129 "sync.disputer.pending_charges.payment" => Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_PAYMENT),
130 "sync.disputer.pending_charges.limit_reached" => {
131 Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_LIMIT_REACHED)
132 }
133 "sync.disputer.pending_charges.added" => Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_ADDED),
134 "sync.disputer.disputes.created" => Ok(keys::SYNC_DISPUTER_DISPUTES_CREATED),
135 "sync.disputer.disputes.won" => Ok(keys::SYNC_DISPUTER_DISPUTES_WON),
136 "sync.disputer.disputes.lost" => Ok(keys::SYNC_DISPUTER_DISPUTES_LOST),
137 "sync.disputer.disputes.warning_closed" => Ok(keys::SYNC_DISPUTER_DISPUTES_WARNING_CLOSED),
138 "sync.disputer.disputes.missing_evidence_reminder" => Ok(keys::SYNC_DISPUTER_DISPUTES_MISSING_EVIDENCE_REMINDER),
139 "sync.disputer.evidences.submitted" => Ok(keys::SYNC_DISPUTER_EVIDENCES_SUBMITTED),
140 "sync.disputer.evidences.received" => Ok(keys::SYNC_DISPUTER_EVIDENCES_RECEIVED),
141 "sync.connection.not_created.nudge_day_1" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY1),
142 "sync.connection.not_created.nudge_day_2" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY2),
144 "sync.connection.not_created.nudge_day_4" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY4),
145 "sync.connection.not_created.nudge_day_7" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY7),
146 "sync.connection.not_created.nudge_day_30" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY30),
147
148 "sync.response.deadline.approaching" => Ok(keys::SYNC_RESPONSE_DEADLINE_APPROACHING),
151
152 "account.existing_chargebacks" => Ok(keys::ACCOUNT_EXISTING_CHARGEBACKS),
162 "account.high_chargeback_ratio_warning"=> Ok(keys::ACCOUNT_HIGH_CHARGEBACK_RATIO_WARNING),
163 "account.provider_disconnected" => Ok(keys::ACCOUNT_PROVIDER_DISCONNECTED),
164
165 "billing.payment_failed" => Ok(keys::BILLING_PAYMENT_FAILED),
167 "billing.invoice_ready" => Ok(keys::BILLING_INVOICE_READY),
168
169 unknown => {
170 eprintln!("Unknown event key '{}', expected a known key", unknown);
172 Err(de::Error::custom(format!("Unknown event key: {}", unknown)))
173 }
174 }
175 }
176 }
177
178 deserializer.deserialize_str(EventKeyVisitor)
179 }
180}
181
182#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
183pub enum AccountEventKey {
184 Welcome,
185 TierUpgrade,
186
187 ExistingChargebacks,
189 HighChargebackRatioWarning,
190 ProviderDisconnected,
191
192 BillingPaymentFailed,
194 BillingInvoiceReady,
195}
196
197impl AccountEventKey {
198 pub fn as_str(&self) -> &'static str {
199 match self {
200 AccountEventKey::Welcome => "account.welcome",
201 AccountEventKey::TierUpgrade => "account.tier_upgrade",
202 AccountEventKey::ExistingChargebacks => "account.existing_chargebacks",
203 AccountEventKey::HighChargebackRatioWarning => "account.high_chargeback_ratio_warning",
204 AccountEventKey::ProviderDisconnected => "account.provider_disconnected",
205 AccountEventKey::BillingPaymentFailed => "billing.payment_failed",
206 AccountEventKey::BillingInvoiceReady => "billing.invoice_ready",
207 }
208 }
209}
210
211#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
212pub enum SyncEventKey {
213 Connection(ConnectionEventKey),
214 Disputer(DisputerEventKey),
215
216 ConnectionNudge(ConnectionNudgeKey),
218
219 Response(ResponseEventKey),
222 }
225
226impl SyncEventKey {
227 pub fn as_str(&self) -> &'static str {
228 match self {
229 SyncEventKey::Connection(key) => key.as_str(),
230 SyncEventKey::Disputer(key) => key.as_str(),
231 SyncEventKey::ConnectionNudge(key) => key.as_str(),
232 SyncEventKey::Response(key) => key.as_str(),
234 }
237 }
238}
239
240#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
241pub enum ConnectionEventKey {
242 Finished,
243 Added,
244}
245
246impl ConnectionEventKey {
247 pub fn as_str(&self) -> &'static str {
248 match self {
249 ConnectionEventKey::Finished => "sync.connection.finished",
250 ConnectionEventKey::Added => "sync.connection.added",
251 }
252 }
253}
254
255#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
256pub enum DisputerEventKey {
257 PendingCharges(PendingChargeEventKey),
258 Disputes(DisputeEventKey),
259 Evidences(EvidencesEventKey),
260}
261
262impl DisputerEventKey {
263 pub fn as_str(&self) -> &'static str {
264 match self {
265 DisputerEventKey::PendingCharges(key) => key.as_str(),
266 DisputerEventKey::Disputes(key) => key.as_str(),
267 DisputerEventKey::Evidences(key) => key.as_str(),
268 }
269 }
270}
271
272#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
273pub enum DisputeEventKey {
274 Created,
275 Won,
276 Lost,
277 WarningClosed,
278 MissingEvidenceReminder
279}
280
281impl DisputeEventKey {
282 pub fn as_str(&self) -> &'static str {
283 match self {
284 DisputeEventKey::Created => "sync.disputer.disputes.created",
285 DisputeEventKey::Won => "sync.disputer.disputes.won",
286 DisputeEventKey::Lost => "sync.disputer.disputes.lost",
287 DisputeEventKey::WarningClosed => "sync.disputer.disputes.warning_closed",
288 DisputeEventKey::MissingEvidenceReminder => "sync.disputer.disputes.missing_evidence_reminder",
289 }
290 }
291}
292
293#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
294pub enum PendingChargeEventKey {
295 Payment,
296 LimitReached,
297 Added,
298}
299
300impl PendingChargeEventKey {
301 pub fn as_str(&self) -> &'static str {
302 match self {
303 PendingChargeEventKey::Payment => "sync.disputer.pending_charges.payment",
304 PendingChargeEventKey::LimitReached => "sync.disputer.pending_charges.limit_reached",
305 PendingChargeEventKey::Added => "sync.disputer.pending_charges.added",
306 }
307 }
308}
309
310#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
311pub enum EvidencesEventKey {
312 Submitted,
313 Received,
314}
315
316impl EvidencesEventKey {
317 pub fn as_str(&self) -> &'static str {
318 match self {
319 EvidencesEventKey::Submitted => "sync.disputer.evidences.submitted",
320 EvidencesEventKey::Received => "sync.disputer.evidences.received",
321 }
322 }
323}
324
325#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
327pub enum ConnectionNudgeKey {
328 Day1,
329 Day2,
330 Day4,
331 Day7,
332 Day30,
333}
334
335impl ConnectionNudgeKey {
336 pub fn as_str(&self) -> &'static str {
337 match self {
338 ConnectionNudgeKey::Day1 => "sync.connection.not_created.nudge_day_1",
339 ConnectionNudgeKey::Day2 => "sync.connection.not_created.nudge_day_2",
340 ConnectionNudgeKey::Day4 => "sync.connection.not_created.nudge_day_4",
341 ConnectionNudgeKey::Day7 => "sync.connection.not_created.nudge_day_7",
342 ConnectionNudgeKey::Day30 => "sync.connection.not_created.nudge_day_30",
343 }
344 }
345}
346
347#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
362pub enum ResponseEventKey {
363 DeadlineApproaching,
364}
365impl ResponseEventKey {
366 pub fn as_str(&self) -> &'static str {
367 match self {
368 ResponseEventKey::DeadlineApproaching => "sync.response.deadline.approaching",
369 }
370 }
371}
372
373pub mod keys {
404 use super::*;
405
406 pub const ACCOUNT_WELCOME: EventKey = EventKey::Account(AccountEventKey::Welcome);
407 pub const ACCOUNT_TIER_UPGRADE: EventKey = EventKey::Account(AccountEventKey::TierUpgrade);
408
409 pub const SYNC_CONNECTION_FINISHED: EventKey =
410 EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Finished));
411
412 pub const SYNC_CONNECTION_ADDED: EventKey = EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Added));
413
414 pub const SYNC_DISPUTER_PENDING_CHARGES_PAYMENT: EventKey = EventKey::Sync(SyncEventKey::Disputer(
415 DisputerEventKey::PendingCharges(PendingChargeEventKey::Payment),
416 ));
417 pub const SYNC_DISPUTER_PENDING_CHARGES_LIMIT_REACHED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
418 DisputerEventKey::PendingCharges(PendingChargeEventKey::LimitReached),
419 ));
420 pub const SYNC_DISPUTER_PENDING_CHARGES_ADDED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
421 DisputerEventKey::PendingCharges(PendingChargeEventKey::Added),
422 ));
423 pub const SYNC_DISPUTER_DISPUTES_CREATED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
424 DisputerEventKey::Disputes(DisputeEventKey::Created),
425 ));
426 pub const SYNC_DISPUTER_DISPUTES_WON: EventKey =
427 EventKey::Sync(SyncEventKey::Disputer(DisputerEventKey::Disputes(DisputeEventKey::Won)));
428 pub const SYNC_DISPUTER_DISPUTES_LOST: EventKey = EventKey::Sync(SyncEventKey::Disputer(
429 DisputerEventKey::Disputes(DisputeEventKey::Lost),
430 ));
431 pub const SYNC_DISPUTER_DISPUTES_WARNING_CLOSED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
432 DisputerEventKey::Disputes(DisputeEventKey::WarningClosed),
433 ));
434 pub const SYNC_DISPUTER_DISPUTES_MISSING_EVIDENCE_REMINDER: EventKey = EventKey::Sync(SyncEventKey::Disputer(
435 DisputerEventKey::Disputes(DisputeEventKey::MissingEvidenceReminder),
436 ));
437 pub const SYNC_DISPUTER_EVIDENCES_SUBMITTED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
438 DisputerEventKey::Evidences(EvidencesEventKey::Submitted),
439 ));
440 pub const SYNC_DISPUTER_EVIDENCES_RECEIVED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
441 DisputerEventKey::Evidences(EvidencesEventKey::Received),
442 ));
443
444 pub const SYNC_CONNECTION_NUDGE_DAY1: EventKey = EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day1));
445
446 pub const SYNC_CONNECTION_NUDGE_DAY2: EventKey =
447 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day2));
448 pub const SYNC_CONNECTION_NUDGE_DAY4: EventKey =
449 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day4));
450 pub const SYNC_CONNECTION_NUDGE_DAY7: EventKey =
451 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day7));
452 pub const SYNC_CONNECTION_NUDGE_DAY30: EventKey =
453 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day30));
454
455 pub const SYNC_RESPONSE_DEADLINE_APPROACHING: EventKey =
459 EventKey::Sync(SyncEventKey::Response(ResponseEventKey::DeadlineApproaching));
460
461 pub const ACCOUNT_EXISTING_CHARGEBACKS: EventKey =
473 EventKey::Account(AccountEventKey::ExistingChargebacks);
474 pub const ACCOUNT_HIGH_CHARGEBACK_RATIO_WARNING: EventKey =
475 EventKey::Account(AccountEventKey::HighChargebackRatioWarning);
476 pub const ACCOUNT_PROVIDER_DISCONNECTED: EventKey =
477 EventKey::Account(AccountEventKey::ProviderDisconnected);
478
479 pub const BILLING_PAYMENT_FAILED: EventKey =
481 EventKey::Account(AccountEventKey::BillingPaymentFailed);
482 pub const BILLING_INVOICE_READY: EventKey =
483 EventKey::Account(AccountEventKey::BillingInvoiceReady);
484
485}