msg_auth_status/alloc_yes/
auth_results.rs

1//! Allocating DkimResultsHandler, SpfResultsHandler, AuthResultsHandler, IpRevResultsHandler
2
3use crate::auth::SmtpAuthResult;
4use crate::dkim::DkimResult;
5use crate::iprev::IpRevResult;
6use crate::spf::SpfResult;
7
8use crate::auth_results::*;
9
10use crate::error::AuthResultsError;
11
12/// Unknown / unsupported methods
13#[derive(Clone, Debug, Default, PartialEq)]
14pub struct UnknownResult<'hdr> {
15    /// Unparsed raw that was ignored
16    pub raw: &'hdr str,
17}
18
19/// Parsed Authentication-Results
20#[derive(Clone, Debug, Default, PartialEq)]
21pub struct AuthenticationResults<'hdr> {
22    /// Relevant host to this record
23    pub host: Option<HostVersion<'hdr>>,
24    /// Parsed auth = .. records
25    pub smtp_auth_result: Vec<SmtpAuthResult<'hdr>>,
26    /// Parsed spf = .. records
27    pub spf_result: Vec<SpfResult<'hdr>>,
28    /// Parsed dkim = .. records
29    pub dkim_result: Vec<DkimResult<'hdr>>,
30    /// Parsed iprev = .. records
31    pub iprev_result: Vec<IpRevResult<'hdr>>,
32    /// Unknown .. = .. records
33    pub unknown_result: Vec<UnknownResult<'hdr>>,
34    /// Whether none was encountered denoting no result
35    pub none_done: bool,
36    /// Unparsed raw
37    pub raw: Option<&'hdr str>,
38    /// Parsing errors if any
39    pub errors: Vec<AuthResultsError<'hdr>>,
40}
41
42/// Allocating type for parsed all Authentication-Results in email
43#[cfg(any(feature = "alloc", feature = "std"))]
44#[derive(Debug, Default)]
45pub struct MessageAuthStatus<'hdr> {
46    /// Authentication-Results
47    pub auth_results: Vec<AuthenticationResults<'hdr>>,
48}
49
50/// Currently all errors are inside results - this may change in future
51#[derive(Debug, PartialEq)]
52pub enum MessageAuthStatusError {}
53
54impl<'hdr> MessageAuthStatus<'hdr> {
55    /// Parse all Authentication-Results into allocating Vec from external mail_parser::Message
56    #[cfg(feature = "mail_parser")]
57    pub fn from_mail_parser(
58        msg: &'hdr mail_parser::Message<'hdr>,
59    ) -> Result<Self, MessageAuthStatusError> {
60        let mut new_self = Self {
61            auth_results: vec![],
62        };
63
64        new_self.auth_results = msg
65            .header_values("Authentication-Results")
66            .map(|mh| mh.into())
67            .collect();
68
69        Ok(new_self)
70    }
71}
72
73#[cfg(test)]
74#[cfg(feature = "mail_parser")]
75mod test {
76    use super::*;
77    use insta::assert_debug_snapshot;
78    use rstest::rstest;
79    use std::{fs::File, io::Read, path::PathBuf};
80
81    fn load_test_data(file_location: &str) -> Vec<u8> {
82        let mut file = File::open(file_location).unwrap();
83        let mut data: Vec<u8> = vec![];
84        file.read_to_end(&mut data).unwrap();
85        data
86    }
87
88    #[rstest]
89    fn from_mail_parser(#[files("test_data/*.eml")] file_path: PathBuf) {
90        let new_snapshot_path = file_path.with_extension("snap");
91
92        insta::with_settings!({snapshot_path => new_snapshot_path}, {
93            insta::allow_duplicates! {
94                let raw  = load_test_data(file_path.to_str().unwrap());
95                let parser = mail_parser::MessageParser::default();
96                let parsed_message = parser.parse(&raw).unwrap();
97                let status = MessageAuthStatus::from_mail_parser(&parsed_message).unwrap();
98                assert_debug_snapshot!(&status);
99            }
100        });
101    }
102}