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.evidences.submitted" => Ok(keys::SYNC_DISPUTER_EVIDENCES_SUBMITTED),
138 "sync.connection.not_created.nudge_day_1" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY1),
139 "sync.connection.not_created.nudge_day_2" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY2),
141 "sync.connection.not_created.nudge_day_4" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY4),
142 "sync.connection.not_created.nudge_day_7" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY7),
143 "sync.connection.not_created.nudge_day_30" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY30),
144
145 "sync.response.deadline.approaching" => Ok(keys::SYNC_RESPONSE_DEADLINE_APPROACHING),
148
149 "account.existing_chargebacks" => Ok(keys::ACCOUNT_EXISTING_CHARGEBACKS),
159 "account.high_chargeback_ratio_warning"=> Ok(keys::ACCOUNT_HIGH_CHARGEBACK_RATIO_WARNING),
160 "account.provider_disconnected" => Ok(keys::ACCOUNT_PROVIDER_DISCONNECTED),
161
162 "billing.payment_failed" => Ok(keys::BILLING_PAYMENT_FAILED),
164 "billing.invoice_ready" => Ok(keys::BILLING_INVOICE_READY),
165
166 unknown => {
167 eprintln!("Unknown event key '{}', expected a known key", unknown);
169 Err(de::Error::custom(format!("Unknown event key: {}", unknown)))
170 }
171 }
172 }
173 }
174
175 deserializer.deserialize_str(EventKeyVisitor)
176 }
177}
178
179#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
180pub enum AccountEventKey {
181 Welcome,
182 TierUpgrade,
183
184 ExistingChargebacks,
186 HighChargebackRatioWarning,
187 ProviderDisconnected,
188
189 BillingPaymentFailed,
191 BillingInvoiceReady,
192}
193
194impl AccountEventKey {
195 pub fn as_str(&self) -> &'static str {
196 match self {
197 AccountEventKey::Welcome => "account.welcome",
198 AccountEventKey::TierUpgrade => "account.tier_upgrade",
199 AccountEventKey::ExistingChargebacks => "account.existing_chargebacks",
200 AccountEventKey::HighChargebackRatioWarning => "account.high_chargeback_ratio_warning",
201 AccountEventKey::ProviderDisconnected => "account.provider_disconnected",
202 AccountEventKey::BillingPaymentFailed => "billing.payment_failed",
203 AccountEventKey::BillingInvoiceReady => "billing.invoice_ready",
204 }
205 }
206}
207
208#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
209pub enum SyncEventKey {
210 Connection(ConnectionEventKey),
211 Disputer(DisputerEventKey),
212
213 ConnectionNudge(ConnectionNudgeKey),
215
216 Response(ResponseEventKey),
219 }
222
223impl SyncEventKey {
224 pub fn as_str(&self) -> &'static str {
225 match self {
226 SyncEventKey::Connection(key) => key.as_str(),
227 SyncEventKey::Disputer(key) => key.as_str(),
228 SyncEventKey::ConnectionNudge(key) => key.as_str(),
229 SyncEventKey::Response(key) => key.as_str(),
231 }
234 }
235}
236
237#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
238pub enum ConnectionEventKey {
239 Finished,
240 Added,
241}
242
243impl ConnectionEventKey {
244 pub fn as_str(&self) -> &'static str {
245 match self {
246 ConnectionEventKey::Finished => "sync.connection.finished",
247 ConnectionEventKey::Added => "sync.connection.added",
248 }
249 }
250}
251
252#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
253pub enum DisputerEventKey {
254 PendingCharges(PendingChargeEventKey),
255 Disputes(DisputeEventKey),
256 Evidences(EvidencesEventKey),
257}
258
259impl DisputerEventKey {
260 pub fn as_str(&self) -> &'static str {
261 match self {
262 DisputerEventKey::PendingCharges(key) => key.as_str(),
263 DisputerEventKey::Disputes(key) => key.as_str(),
264 DisputerEventKey::Evidences(key) => key.as_str(),
265 }
266 }
267}
268
269#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
270pub enum DisputeEventKey {
271 Created,
272 Won,
273 Lost,
274}
275
276impl DisputeEventKey {
277 pub fn as_str(&self) -> &'static str {
278 match self {
279 DisputeEventKey::Created => "sync.disputer.disputes.created",
280 DisputeEventKey::Won => "sync.disputer.disputes.won",
281 DisputeEventKey::Lost => "sync.disputer.disputes.lost",
282 }
283 }
284}
285
286#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
287pub enum PendingChargeEventKey {
288 Payment,
289 LimitReached,
290 Added,
291}
292
293impl PendingChargeEventKey {
294 pub fn as_str(&self) -> &'static str {
295 match self {
296 PendingChargeEventKey::Payment => "sync.disputer.pending_charges.payment",
297 PendingChargeEventKey::LimitReached => "sync.disputer.pending_charges.limit_reached",
298 PendingChargeEventKey::Added => "sync.disputer.pending_charges.added",
299 }
300 }
301}
302
303#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
304pub enum EvidencesEventKey {
305 Submitted,
306}
307
308impl EvidencesEventKey {
309 pub fn as_str(&self) -> &'static str {
310 match self {
311 EvidencesEventKey::Submitted => "sync.disputer.evidences.submitted",
312 }
313 }
314}
315
316#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
318pub enum ConnectionNudgeKey {
319 Day1,
320 Day2,
321 Day4,
322 Day7,
323 Day30,
324}
325
326impl ConnectionNudgeKey {
327 pub fn as_str(&self) -> &'static str {
328 match self {
329 ConnectionNudgeKey::Day1 => "sync.connection.not_created.nudge_day_1",
330 ConnectionNudgeKey::Day2 => "sync.connection.not_created.nudge_day_2",
331 ConnectionNudgeKey::Day4 => "sync.connection.not_created.nudge_day_4",
332 ConnectionNudgeKey::Day7 => "sync.connection.not_created.nudge_day_7",
333 ConnectionNudgeKey::Day30 => "sync.connection.not_created.nudge_day_30",
334 }
335 }
336}
337
338#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
353pub enum ResponseEventKey {
354 DeadlineApproaching,
355}
356impl ResponseEventKey {
357 pub fn as_str(&self) -> &'static str {
358 match self {
359 ResponseEventKey::DeadlineApproaching => "sync.response.deadline.approaching",
360 }
361 }
362}
363
364pub mod keys {
395 use super::*;
396
397 pub const ACCOUNT_WELCOME: EventKey = EventKey::Account(AccountEventKey::Welcome);
398 pub const ACCOUNT_TIER_UPGRADE: EventKey = EventKey::Account(AccountEventKey::TierUpgrade);
399
400 pub const SYNC_CONNECTION_FINISHED: EventKey =
401 EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Finished));
402
403 pub const SYNC_CONNECTION_ADDED: EventKey = EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Added));
404
405 pub const SYNC_DISPUTER_PENDING_CHARGES_PAYMENT: EventKey = EventKey::Sync(SyncEventKey::Disputer(
406 DisputerEventKey::PendingCharges(PendingChargeEventKey::Payment),
407 ));
408 pub const SYNC_DISPUTER_PENDING_CHARGES_LIMIT_REACHED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
409 DisputerEventKey::PendingCharges(PendingChargeEventKey::LimitReached),
410 ));
411 pub const SYNC_DISPUTER_PENDING_CHARGES_ADDED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
412 DisputerEventKey::PendingCharges(PendingChargeEventKey::Added),
413 ));
414 pub const SYNC_DISPUTER_DISPUTES_CREATED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
415 DisputerEventKey::Disputes(DisputeEventKey::Created),
416 ));
417 pub const SYNC_DISPUTER_DISPUTES_WON: EventKey =
418 EventKey::Sync(SyncEventKey::Disputer(DisputerEventKey::Disputes(DisputeEventKey::Won)));
419 pub const SYNC_DISPUTER_DISPUTES_LOST: EventKey = EventKey::Sync(SyncEventKey::Disputer(
420 DisputerEventKey::Disputes(DisputeEventKey::Lost),
421 ));
422 pub const SYNC_DISPUTER_EVIDENCES_SUBMITTED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
423 DisputerEventKey::Evidences(EvidencesEventKey::Submitted),
424 ));
425
426 pub const SYNC_CONNECTION_NUDGE_DAY1: EventKey = EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day1));
427
428 pub const SYNC_CONNECTION_NUDGE_DAY2: EventKey =
429 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day2));
430 pub const SYNC_CONNECTION_NUDGE_DAY4: EventKey =
431 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day4));
432 pub const SYNC_CONNECTION_NUDGE_DAY7: EventKey =
433 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day7));
434 pub const SYNC_CONNECTION_NUDGE_DAY30: EventKey =
435 EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day30));
436
437 pub const SYNC_RESPONSE_DEADLINE_APPROACHING: EventKey =
441 EventKey::Sync(SyncEventKey::Response(ResponseEventKey::DeadlineApproaching));
442
443 pub const ACCOUNT_EXISTING_CHARGEBACKS: EventKey =
455 EventKey::Account(AccountEventKey::ExistingChargebacks);
456 pub const ACCOUNT_HIGH_CHARGEBACK_RATIO_WARNING: EventKey =
457 EventKey::Account(AccountEventKey::HighChargebackRatioWarning);
458 pub const ACCOUNT_PROVIDER_DISCONNECTED: EventKey =
459 EventKey::Account(AccountEventKey::ProviderDisconnected);
460
461 pub const BILLING_PAYMENT_FAILED: EventKey =
463 EventKey::Account(AccountEventKey::BillingPaymentFailed);
464 pub const BILLING_INVOICE_READY: EventKey =
465 EventKey::Account(AccountEventKey::BillingInvoiceReady);
466
467}