use serde::{
Deserialize, Deserializer, Serialize, Serializer,
de::{self, Visitor},
};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EventKey {
Account(AccountEventKey),
Sync(SyncEventKey),
}
impl EventKey {
pub fn as_str(&self) -> &'static str {
match self {
EventKey::Account(key) => key.as_str(),
EventKey::Sync(key) => key.as_str(),
}
}
}
impl Serialize for EventKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for EventKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct EventKeyVisitor;
impl<'de> Visitor<'de> for EventKeyVisitor {
type Value = EventKey;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a valid event key string")
}
fn visit_str<E>(self, value: &str) -> Result<EventKey, E>
where
E: de::Error,
{
match value {
"account.welcome" => Ok(keys::ACCOUNT_WELCOME),
"account.tier_upgrade" => Ok(keys::ACCOUNT_TIER_UPGRADE),
"sync.connection.finished" => Ok(keys::SYNC_CONNECTION_FINISHED),
"sync.connection.added" => Ok(keys::SYNC_CONNECTION_ADDED),
"sync.disputer.pending_charges.payment" => Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_PAYMENT),
"sync.disputer.pending_charges.limit_reached" => {
Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_LIMIT_REACHED)
}
"sync.disputer.pending_charges.added" => Ok(keys::SYNC_DISPUTER_PENDING_CHARGES_ADDED),
"sync.disputer.disputes.created" => Ok(keys::SYNC_DISPUTER_DISPUTES_CREATED),
"sync.disputer.disputes.won" => Ok(keys::SYNC_DISPUTER_DISPUTES_WON),
"sync.disputer.disputes.lost" => Ok(keys::SYNC_DISPUTER_DISPUTES_LOST),
"sync.disputer.disputes.warning_closed" => Ok(keys::SYNC_DISPUTER_DISPUTES_WARNING_CLOSED),
"sync.disputer.disputes.missing_evidence_reminder" => Ok(keys::SYNC_DISPUTER_DISPUTES_MISSING_EVIDENCE_REMINDER),
"sync.disputer.evidences.submitted" => Ok(keys::SYNC_DISPUTER_EVIDENCES_SUBMITTED),
"sync.disputer.evidences.received" => Ok(keys::SYNC_DISPUTER_EVIDENCES_RECEIVED),
"sync.connection.not_created.nudge_day_1" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY1),
"sync.connection.not_created.nudge_day_2" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY2),
"sync.connection.not_created.nudge_day_4" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY4),
"sync.connection.not_created.nudge_day_7" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY7),
"sync.connection.not_created.nudge_day_30" => Ok(keys::SYNC_CONNECTION_NUDGE_DAY30),
"sync.response.deadline.approaching" => Ok(keys::SYNC_RESPONSE_DEADLINE_APPROACHING),
"account.existing_chargebacks" => Ok(keys::ACCOUNT_EXISTING_CHARGEBACKS),
"account.high_chargeback_ratio_warning"=> Ok(keys::ACCOUNT_HIGH_CHARGEBACK_RATIO_WARNING),
"account.provider_disconnected" => Ok(keys::ACCOUNT_PROVIDER_DISCONNECTED),
"billing.payment_failed" => Ok(keys::BILLING_PAYMENT_FAILED),
"billing.invoice_ready" => Ok(keys::BILLING_INVOICE_READY),
"welcome" => Ok(keys::BILLING_WELCOME),
"payment_succeeded" => Ok(keys::BILLING_PAYMENT_SUCCEEDED),
"payment_failed" => Ok(keys::BILLING_PAYMENT_FAILED_2),
"overage_invoice_created" => Ok(keys::BILLING_OVERAGE_INVOICE_CREATED),
"upcoming_invoice" => Ok(keys::BILLING_UPCOMING_INVOICE),
"trial_expired" => Ok(keys::BILLING_TRIAL_EXPIRED),
"trial_ending" => Ok(keys::BILLING_TRIAL_ENDING),
"trial_started" => Ok(keys::BILLING_TRIAL_STARTED),
"plan_downgraded" => Ok(keys::BILLING_PLAN_DOWNGRADED),
"plan_upgraded" => Ok(keys::BILLING_PLAN_UPGRADED),
"plan_renewed" => Ok(keys::BILLING_PLAN_RENEWED),
"plan_cancelled" => Ok(keys::BILLING_PLAN_CANCELLED),
"plan_purchased" => Ok(keys::BILLING_PLAN_PURCHASED),
unknown => {
eprintln!("Unknown event key '{}', expected a known key", unknown);
Err(de::Error::custom(format!("Unknown event key: {}", unknown)))
}
}
}
}
deserializer.deserialize_str(EventKeyVisitor)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AccountEventKey {
Welcome,
TierUpgrade,
ExistingChargebacks,
HighChargebackRatioWarning,
ProviderDisconnected,
BillingPaymentFailed,
BillingInvoiceReady,
BillingWelcome,
BillingPaymentSucceeded,
BillingPaymentFailed2,
BillingOverageInvoiceCreated,
BillingUpcomingInvoice,
BillingTrialExpired,
BillingTrialEnding,
BillingTrialStarted,
BillingPlanDowngraded,
BillingPlanUpgraded,
BillingPlanRenewed,
BillingPlanCancelled,
BillingPlanPurchased,
}
impl AccountEventKey {
pub fn as_str(&self) -> &'static str {
match self {
AccountEventKey::Welcome => "account.welcome",
AccountEventKey::TierUpgrade => "account.tier_upgrade",
AccountEventKey::ExistingChargebacks => "account.existing_chargebacks",
AccountEventKey::HighChargebackRatioWarning => "account.high_chargeback_ratio_warning",
AccountEventKey::ProviderDisconnected => "account.provider_disconnected",
AccountEventKey::BillingPaymentFailed => "billing.payment_failed",
AccountEventKey::BillingInvoiceReady => "billing.invoice_ready",
AccountEventKey::BillingWelcome => "welcome",
AccountEventKey::BillingPaymentSucceeded => "payment_succeeded",
AccountEventKey::BillingPaymentFailed2 => "payment_failed",
AccountEventKey::BillingOverageInvoiceCreated => "overage_invoice_created",
AccountEventKey::BillingUpcomingInvoice => "upcoming_invoice",
AccountEventKey::BillingTrialExpired => "trial_expired",
AccountEventKey::BillingTrialEnding => "trial_ending",
AccountEventKey::BillingTrialStarted => "trial_started",
AccountEventKey::BillingPlanDowngraded => "plan_downgraded",
AccountEventKey::BillingPlanUpgraded => "plan_upgraded",
AccountEventKey::BillingPlanRenewed => "plan_renewed",
AccountEventKey::BillingPlanCancelled => "plan_cancelled",
AccountEventKey::BillingPlanPurchased => "plan_purchased",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SyncEventKey {
Connection(ConnectionEventKey),
Disputer(DisputerEventKey),
ConnectionNudge(ConnectionNudgeKey),
Response(ResponseEventKey),
}
impl SyncEventKey {
pub fn as_str(&self) -> &'static str {
match self {
SyncEventKey::Connection(key) => key.as_str(),
SyncEventKey::Disputer(key) => key.as_str(),
SyncEventKey::ConnectionNudge(key) => key.as_str(),
SyncEventKey::Response(key) => key.as_str(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConnectionEventKey {
Finished,
Added,
}
impl ConnectionEventKey {
pub fn as_str(&self) -> &'static str {
match self {
ConnectionEventKey::Finished => "sync.connection.finished",
ConnectionEventKey::Added => "sync.connection.added",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DisputerEventKey {
PendingCharges(PendingChargeEventKey),
Disputes(DisputeEventKey),
Evidences(EvidencesEventKey),
}
impl DisputerEventKey {
pub fn as_str(&self) -> &'static str {
match self {
DisputerEventKey::PendingCharges(key) => key.as_str(),
DisputerEventKey::Disputes(key) => key.as_str(),
DisputerEventKey::Evidences(key) => key.as_str(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DisputeEventKey {
Created,
Won,
Lost,
WarningClosed,
MissingEvidenceReminder
}
impl DisputeEventKey {
pub fn as_str(&self) -> &'static str {
match self {
DisputeEventKey::Created => "sync.disputer.disputes.created",
DisputeEventKey::Won => "sync.disputer.disputes.won",
DisputeEventKey::Lost => "sync.disputer.disputes.lost",
DisputeEventKey::WarningClosed => "sync.disputer.disputes.warning_closed",
DisputeEventKey::MissingEvidenceReminder => "sync.disputer.disputes.missing_evidence_reminder",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PendingChargeEventKey {
Payment,
LimitReached,
Added,
}
impl PendingChargeEventKey {
pub fn as_str(&self) -> &'static str {
match self {
PendingChargeEventKey::Payment => "sync.disputer.pending_charges.payment",
PendingChargeEventKey::LimitReached => "sync.disputer.pending_charges.limit_reached",
PendingChargeEventKey::Added => "sync.disputer.pending_charges.added",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum EvidencesEventKey {
Submitted,
Received,
}
impl EvidencesEventKey {
pub fn as_str(&self) -> &'static str {
match self {
EvidencesEventKey::Submitted => "sync.disputer.evidences.submitted",
EvidencesEventKey::Received => "sync.disputer.evidences.received",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConnectionNudgeKey {
Day1,
Day2,
Day4,
Day7,
Day30,
}
impl ConnectionNudgeKey {
pub fn as_str(&self) -> &'static str {
match self {
ConnectionNudgeKey::Day1 => "sync.connection.not_created.nudge_day_1",
ConnectionNudgeKey::Day2 => "sync.connection.not_created.nudge_day_2",
ConnectionNudgeKey::Day4 => "sync.connection.not_created.nudge_day_4",
ConnectionNudgeKey::Day7 => "sync.connection.not_created.nudge_day_7",
ConnectionNudgeKey::Day30 => "sync.connection.not_created.nudge_day_30",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ResponseEventKey {
DeadlineApproaching,
}
impl ResponseEventKey {
pub fn as_str(&self) -> &'static str {
match self {
ResponseEventKey::DeadlineApproaching => "sync.response.deadline.approaching",
}
}
}
pub mod keys {
use super::*;
pub const ACCOUNT_WELCOME: EventKey = EventKey::Account(AccountEventKey::Welcome);
pub const ACCOUNT_TIER_UPGRADE: EventKey = EventKey::Account(AccountEventKey::TierUpgrade);
pub const SYNC_CONNECTION_FINISHED: EventKey =
EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Finished));
pub const SYNC_CONNECTION_ADDED: EventKey = EventKey::Sync(SyncEventKey::Connection(ConnectionEventKey::Added));
pub const SYNC_DISPUTER_PENDING_CHARGES_PAYMENT: EventKey = EventKey::Sync(SyncEventKey::Disputer(
DisputerEventKey::PendingCharges(PendingChargeEventKey::Payment),
));
pub const SYNC_DISPUTER_PENDING_CHARGES_LIMIT_REACHED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
DisputerEventKey::PendingCharges(PendingChargeEventKey::LimitReached),
));
pub const SYNC_DISPUTER_PENDING_CHARGES_ADDED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
DisputerEventKey::PendingCharges(PendingChargeEventKey::Added),
));
pub const SYNC_DISPUTER_DISPUTES_CREATED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
DisputerEventKey::Disputes(DisputeEventKey::Created),
));
pub const SYNC_DISPUTER_DISPUTES_WON: EventKey =
EventKey::Sync(SyncEventKey::Disputer(DisputerEventKey::Disputes(DisputeEventKey::Won)));
pub const SYNC_DISPUTER_DISPUTES_LOST: EventKey = EventKey::Sync(SyncEventKey::Disputer(
DisputerEventKey::Disputes(DisputeEventKey::Lost),
));
pub const SYNC_DISPUTER_DISPUTES_WARNING_CLOSED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
DisputerEventKey::Disputes(DisputeEventKey::WarningClosed),
));
pub const SYNC_DISPUTER_DISPUTES_MISSING_EVIDENCE_REMINDER: EventKey = EventKey::Sync(SyncEventKey::Disputer(
DisputerEventKey::Disputes(DisputeEventKey::MissingEvidenceReminder),
));
pub const SYNC_DISPUTER_EVIDENCES_SUBMITTED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
DisputerEventKey::Evidences(EvidencesEventKey::Submitted),
));
pub const SYNC_DISPUTER_EVIDENCES_RECEIVED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
DisputerEventKey::Evidences(EvidencesEventKey::Received),
));
pub const SYNC_CONNECTION_NUDGE_DAY1: EventKey = EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day1));
pub const SYNC_CONNECTION_NUDGE_DAY2: EventKey =
EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day2));
pub const SYNC_CONNECTION_NUDGE_DAY4: EventKey =
EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day4));
pub const SYNC_CONNECTION_NUDGE_DAY7: EventKey =
EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day7));
pub const SYNC_CONNECTION_NUDGE_DAY30: EventKey =
EventKey::Sync(SyncEventKey::ConnectionNudge(ConnectionNudgeKey::Day30));
pub const SYNC_RESPONSE_DEADLINE_APPROACHING: EventKey =
EventKey::Sync(SyncEventKey::Response(ResponseEventKey::DeadlineApproaching));
pub const ACCOUNT_EXISTING_CHARGEBACKS: EventKey =
EventKey::Account(AccountEventKey::ExistingChargebacks);
pub const ACCOUNT_HIGH_CHARGEBACK_RATIO_WARNING: EventKey =
EventKey::Account(AccountEventKey::HighChargebackRatioWarning);
pub const ACCOUNT_PROVIDER_DISCONNECTED: EventKey =
EventKey::Account(AccountEventKey::ProviderDisconnected);
pub const BILLING_PAYMENT_FAILED: EventKey =
EventKey::Account(AccountEventKey::BillingPaymentFailed);
pub const BILLING_INVOICE_READY: EventKey =
EventKey::Account(AccountEventKey::BillingInvoiceReady);
pub const BILLING_WELCOME: EventKey =
EventKey::Account(AccountEventKey::BillingWelcome);
pub const BILLING_PAYMENT_SUCCEEDED: EventKey =
EventKey::Account(AccountEventKey::BillingPaymentSucceeded);
pub const BILLING_PAYMENT_FAILED_2: EventKey =
EventKey::Account(AccountEventKey::BillingPaymentFailed2);
pub const BILLING_OVERAGE_INVOICE_CREATED: EventKey =
EventKey::Account(AccountEventKey::BillingOverageInvoiceCreated);
pub const BILLING_UPCOMING_INVOICE: EventKey =
EventKey::Account(AccountEventKey::BillingUpcomingInvoice);
pub const BILLING_TRIAL_EXPIRED: EventKey =
EventKey::Account(AccountEventKey::BillingTrialExpired);
pub const BILLING_TRIAL_ENDING: EventKey =
EventKey::Account(AccountEventKey::BillingTrialEnding);
pub const BILLING_TRIAL_STARTED: EventKey =
EventKey::Account(AccountEventKey::BillingTrialStarted);
pub const BILLING_PLAN_DOWNGRADED: EventKey =
EventKey::Account(AccountEventKey::BillingPlanDowngraded);
pub const BILLING_PLAN_UPGRADED: EventKey =
EventKey::Account(AccountEventKey::BillingPlanUpgraded);
pub const BILLING_PLAN_RENEWED: EventKey =
EventKey::Account(AccountEventKey::BillingPlanRenewed);
pub const BILLING_PLAN_CANCELLED: EventKey =
EventKey::Account(AccountEventKey::BillingPlanCancelled);
pub const BILLING_PLAN_PURCHASED: EventKey =
EventKey::Account(AccountEventKey::BillingPlanPurchased);
}