1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use ipnetwork::IpNetworkError;
#[derive(Debug, PartialEq)]
pub enum SpfError {
InvalidSource,
SourceLengthExceeded,
LookupLimitExceeded,
HasNotBeenParsed,
WhiteSpaceSyntaxError,
InvalidSPF,
RedirectWithAllMechanism,
InvalidIPAddr(IpNetworkError),
}
impl std::fmt::Display for SpfError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SpfError::InvalidSource => write!(f, "Source string not valid."),
SpfError::SourceLengthExceeded => write!(f, "Spf record exceeds 255 characters."),
SpfError::LookupLimitExceeded => write!(f, "Too many DNS lookups."),
SpfError::HasNotBeenParsed => write!(f, "Source string has not been parsed."),
SpfError::WhiteSpaceSyntaxError => {
write!(
f,
"Spf contains two or more consecutive whitespace characters."
)
}
SpfError::InvalidSPF => write!(f, "Spf record is invalid."),
SpfError::RedirectWithAllMechanism => {
write!(f, "Redirect with unexpected 'All' Mechanism")
}
SpfError::InvalidIPAddr(err) => write!(f, "{}", err.to_string()),
}
}
}
impl From<IpNetworkError> for SpfError {
fn from(err: IpNetworkError) -> Self {
SpfError::InvalidIPAddr(err)
}
}
impl std::error::Error for SpfError {}
impl SpfError {
pub fn is_spf_error(&self) -> bool {
matches!(self, Self::InvalidSource)
|| matches!(self, Self::SourceLengthExceeded)
|| matches!(self, Self::LookupLimitExceeded)
|| matches!(self, Self::HasNotBeenParsed)
|| matches!(self, Self::InvalidSPF)
|| matches!(self, Self::RedirectWithAllMechanism)
|| matches!(self, Self::InvalidIPAddr(_))
}
pub fn is_invalid_source(&self) -> bool {
matches!(self, Self::InvalidSource)
}
pub fn is_source_length_exceeded(&self) -> bool {
matches!(self, Self::SourceLengthExceeded)
}
pub fn is_lookup_limit_exceeded(&self) -> bool {
matches!(self, Self::LookupLimitExceeded)
}
pub fn is_has_not_been_parsed(&self) -> bool {
matches!(self, Self::HasNotBeenParsed)
}
pub fn is_invalid_spf(&self) -> bool {
matches!(self, Self::InvalidSPF)
}
pub fn is_redirect_with_all_mechanism(&self) -> bool {
matches!(self, Self::RedirectWithAllMechanism)
}
pub fn is_invalid_ip_addr(&self) -> bool {
matches!(self, Self::InvalidIPAddr(_))
}
}
#[test]
fn is_any_spf_error() {
let err = SpfError::InvalidSource;
assert_eq!(err.is_spf_error(), true);
}
#[test]
fn is_invalid_source() {
let err = SpfError::InvalidSource;
assert_eq!(err.is_invalid_source(), true);
}
#[test]
fn is_source_length_exceeded() {
let err = SpfError::SourceLengthExceeded;
assert_eq!(err.is_source_length_exceeded(), true);
}
#[test]
fn is_lookup_limit_exceeded() {
let err = SpfError::LookupLimitExceeded;
assert_eq!(err.is_lookup_limit_exceeded(), true)
}
#[test]
fn is_has_not_been_parsed() {
let err = SpfError::HasNotBeenParsed;
assert_eq!(err.is_has_not_been_parsed(), true)
}
#[test]
fn is_invalid_spf() {
let err = SpfError::InvalidSPF;
assert_eq!(err.is_invalid_spf(), true)
}
#[test]
fn is_redirect_with_all_mechanism() {
let err = SpfError::RedirectWithAllMechanism;
assert_eq!(err.is_redirect_with_all_mechanism(), true)
}
#[test]
fn is_invalid_ip_addr() {
let bad_ip = "203.32.160.0/33"
.parse::<ipnetwork::IpNetwork>()
.unwrap_err();
let err = SpfError::InvalidIPAddr(bad_ip);
assert_eq!(err.is_invalid_ip_addr(), true)
}