#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Kind {
Redirect,
#[default]
A,
MX,
Include,
IpV4,
IpV6,
Ptr,
Exists,
All,
}
impl Kind {
pub fn is_redirect(&self) -> bool {
matches!(self, Self::Redirect)
}
pub fn is_a(&self) -> bool {
matches!(self, Self::A)
}
pub fn is_mx(&self) -> bool {
matches!(self, Self::MX)
}
pub fn is_include(&self) -> bool {
matches!(self, Self::Include)
}
pub fn is_ip(&self) -> bool {
matches!(self, Self::IpV4) || matches!(self, Self::IpV6)
}
pub fn is_ip_v4(&self) -> bool {
matches!(self, Self::IpV4)
}
pub fn is_ip_v6(&self) -> bool {
matches!(self, Self::IpV6)
}
pub fn is_ptr(&self) -> bool {
matches!(self, Self::Ptr)
}
pub fn is_exists(&self) -> bool {
matches!(self, Self::Exists)
}
pub fn is_all(&self) -> bool {
matches!(self, Self::All)
}
pub fn as_str(&self) -> &str {
match self {
Kind::Redirect => "redirect=",
Kind::Include => "include:",
Kind::A => "a",
Kind::MX => "mx",
Kind::IpV4 => "ip4:",
Kind::IpV6 => "ip6:",
Kind::Ptr => "ptr",
Kind::Exists => "exists:",
Kind::All => "all",
}
}
}
impl std::fmt::Display for Kind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Kind::Redirect => write!(f, "redirect="),
Kind::Include => write!(f, "include:"),
Kind::A => write!(f, "a"),
Kind::MX => write!(f, "mx"),
Kind::IpV4 => write!(f, "ip4:"),
Kind::IpV6 => write!(f, "ip6:"),
Kind::Ptr => write!(f, "ptr"),
Kind::Exists => write!(f, "exists:"),
Kind::All => write!(f, "all"),
}
}
}
#[test]
fn a() {
let a = Kind::A;
assert_eq!(a.to_string(), "a");
}
#[test]
fn mx() {
let a = Kind::MX;
assert_eq!(a.to_string(), "mx");
}
#[test]
fn redirect() {
let a = Kind::Redirect;
assert_eq!(a.to_string(), "redirect=");
}
#[test]
fn include() {
let a = Kind::Include;
assert_eq!(a.to_string(), "include:");
}
#[test]
fn ip4() {
let a = Kind::IpV4;
assert_eq!(a.to_string(), "ip4:");
}
#[test]
fn ip6() {
let a = Kind::IpV6;
assert_eq!(a.to_string(), "ip6:");
}
#[test]
fn ptr() {
let a = Kind::Ptr;
assert_eq!(a.to_string(), "ptr");
}
#[test]
fn exists() {
let a = Kind::Exists;
assert_eq!(a.to_string(), "exists:");
}
#[test]
fn all() {
let a = Kind::All;
assert_eq!(a.to_string(), "all");
}
#[cfg(test)]
#[cfg(feature = "serde")]
mod serde_tests {
use crate::spf::mechanism::Kind;
use serde_json;
#[test]
fn default() {
let a = Kind::default();
let json = serde_json::to_string(&a).unwrap();
assert_eq!(json, "\"A\"");
let deserialized: Kind = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized, a);
}
#[test]
fn redirect() {
let redirect = Kind::Redirect;
let json = serde_json::to_string(&redirect).unwrap();
assert_eq!(json, "\"Redirect\"");
let deserialized: Kind = serde_json::from_str(&json).unwrap();
assert_eq!(deserialized, redirect);
}
}