use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum RadarStandaloneResponseControl {
BotDetection,
BruteForceAttack,
CredentialStuffing,
DomainSignUpRateLimit,
IpSignUpRateLimit,
ImpossibleTravel,
RepeatSignUp,
StaleAccount,
UnrecognizedDevice,
Restriction,
Unknown(String),
}
impl RadarStandaloneResponseControl {
#[allow(deprecated)]
pub fn as_str(&self) -> &str {
match self {
Self::BotDetection => "bot_detection",
Self::BruteForceAttack => "brute_force_attack",
Self::CredentialStuffing => "credential_stuffing",
Self::DomainSignUpRateLimit => "domain_sign_up_rate_limit",
Self::IpSignUpRateLimit => "ip_sign_up_rate_limit",
Self::ImpossibleTravel => "impossible_travel",
Self::RepeatSignUp => "repeat_sign_up",
Self::StaleAccount => "stale_account",
Self::UnrecognizedDevice => "unrecognized_device",
Self::Restriction => "restriction",
Self::Unknown(s) => s.as_str(),
}
}
}
impl fmt::Display for RadarStandaloneResponseControl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl AsRef<str> for RadarStandaloneResponseControl {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl FromStr for RadarStandaloneResponseControl {
type Err = std::convert::Infallible;
#[allow(deprecated)]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"bot_detection" => Self::BotDetection,
"brute_force_attack" => Self::BruteForceAttack,
"credential_stuffing" => Self::CredentialStuffing,
"domain_sign_up_rate_limit" => Self::DomainSignUpRateLimit,
"ip_sign_up_rate_limit" => Self::IpSignUpRateLimit,
"impossible_travel" => Self::ImpossibleTravel,
"repeat_sign_up" => Self::RepeatSignUp,
"stale_account" => Self::StaleAccount,
"unrecognized_device" => Self::UnrecognizedDevice,
"restriction" => Self::Restriction,
other => Self::Unknown(other.to_string()),
})
}
}
impl From<String> for RadarStandaloneResponseControl {
fn from(s: String) -> Self {
match Self::from_str(&s) {
Ok(Self::Unknown(_)) => Self::Unknown(s),
Ok(other) => other,
}
}
}
impl From<&str> for RadarStandaloneResponseControl {
fn from(s: &str) -> Self {
Self::from_str(s).unwrap_or_else(|_| Self::Unknown(s.to_string()))
}
}
impl Serialize for RadarStandaloneResponseControl {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for RadarStandaloneResponseControl {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
Ok(Self::from(s))
}
}