use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Visitor};
use crate::models;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum GatewayType {
Stripe,
Braintree,
AppStore,
PlayStore,
BitPay,
PayPal,
Bank,
__Unknown(i64),
}
impl GatewayType {
pub fn as_i64(&self) -> i64 {
match self {
Self::Stripe => 0,
Self::Braintree => 1,
Self::AppStore => 2,
Self::PlayStore => 3,
Self::BitPay => 4,
Self::PayPal => 5,
Self::Bank => 6,
Self::__Unknown(v) => *v,
}
}
pub fn from_i64(value: i64) -> Self {
match value {
0 => Self::Stripe,
1 => Self::Braintree,
2 => Self::AppStore,
3 => Self::PlayStore,
4 => Self::BitPay,
5 => Self::PayPal,
6 => Self::Bank,
v => Self::__Unknown(v),
}
}
}
impl serde::Serialize for GatewayType {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_i64(self.as_i64())
}
}
impl<'de> serde::Deserialize<'de> for GatewayType {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct GatewayTypeVisitor;
impl Visitor<'_> for GatewayTypeVisitor {
type Value = GatewayType;
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str("an integer")
}
fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<Self::Value, E> {
Ok(GatewayType::from_i64(v))
}
fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<Self::Value, E> {
Ok(GatewayType::from_i64(v as i64))
}
}
deserializer.deserialize_i64(GatewayTypeVisitor)
}
}
impl std::fmt::Display for GatewayType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_i64())
}
}
impl Default for GatewayType {
fn default() -> GatewayType {
Self::Stripe
}
}