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.evidences.submitted" => Ok(keys::SYNC_DISPUTER_EVIDENCES_SUBMITTED),
"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),
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,
}
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",
}
}
}
#[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,
}
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",
}
}
}
#[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,
}
impl EvidencesEventKey {
pub fn as_str(&self) -> &'static str {
match self {
EvidencesEventKey::Submitted => "sync.disputer.evidences.submitted",
}
}
}
#[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_EVIDENCES_SUBMITTED: EventKey = EventKey::Sync(SyncEventKey::Disputer(
DisputerEventKey::Evidences(EvidencesEventKey::Submitted),
));
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);
}