decon_spf/spf/
mod.rs

1//! This module allows you to deconstruct an existing SPF DNS record into its
2//! constituent parts.  
3
4#[cfg_attr(docsrs, doc(cfg(feature = "builder")))]
5#[cfg(feature = "builder")]
6pub mod builder;
7pub mod errors;
8pub mod mechanism;
9mod string;
10#[cfg(test)]
11mod tests;
12mod validate;
13
14pub use crate::spf::errors::SpfError;
15pub use mechanism::Mechanism;
16use std::fmt::{Debug, Display};
17
18#[cfg(feature = "serde")]
19use serde::{Deserialize, Serialize};
20
21/// Base struct for an Spf of any type.
22#[derive(Debug, Default, Clone, PartialEq)]
23#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
24pub struct Spf<T> {
25    source: T,
26    version: T,
27    redirect_idx: usize,
28    has_redirect: bool,
29    all_idx: usize,
30    lookup_count: u8,
31    mechanisms: Vec<Mechanism<T>>,
32}
33
34pub struct SpfIterator<'a, T> {
35    mechanism_iter: std::slice::Iter<'a, Mechanism<T>>,
36}
37
38impl<'a, T> Iterator for SpfIterator<'a, T> {
39    type Item = &'a Mechanism<T>; // Change the Item type to Mechanism<T>
40
41    fn next(&mut self) -> Option<Self::Item> {
42        self.mechanism_iter.next()
43    }
44}
45
46impl<T> Spf<T>
47where
48    T: Default,
49    T: Debug,
50    T: Display,
51{
52    /// Access the version attribute associated with the Spf record.
53    pub fn version(&self) -> &T {
54        &self.version
55    }
56    /// Access the number of DNS lookups required for this Spf record.
57    pub fn lookup_count(&self) -> u8 {
58        self.lookup_count
59    }
60    /// Iterate over the Spf Mechanisms of the Spf Record. This does not return the Spf `version`,
61    /// but iterates over the mechanisms contained within the Spf record.
62    pub fn iter(&self) -> SpfIterator<'_, T> {
63        SpfIterator {
64            mechanism_iter: self.mechanisms.iter(),
65        }
66    }
67    #[allow(dead_code)]
68    fn len(&self) -> usize {
69        self.mechanisms.len()
70    }
71}
72
73impl<T> IntoIterator for Spf<T> {
74    type Item = Mechanism<T>;
75    type IntoIter = std::vec::IntoIter<Self::Item>;
76
77    fn into_iter(self) -> Self::IntoIter {
78        self.mechanisms.into_iter()
79    }
80}