pata 0.0.1

A fast, lightweight, and evidence-driven email validation library for Rust
Documentation
//! Local address validation.

use unicode_normalization::UnicodeNormalization as _;
use unicode_security::MixedScript as _;

use super::Validator;
use crate::{Address, Certainty, Classification, Finding, FindingCode, Outcome, Report};

impl Validator {
    pub(super) fn local_report(
        &self,
        input: String,
        address: Address,
        elapsed: std::time::Duration,
    ) -> Report {
        let classification = crate::classifier::classify(&address);
        let mut findings = Vec::with_capacity(8);
        findings.push(Finding::new(
            FindingCode::SyntaxValid,
            Outcome::Pass,
            Certainty::Conclusive,
            "the address matches Pata's supported mailbox grammar",
        ));
        if address.requires_smtputf8() {
            findings.push(Finding::new(
                FindingCode::SyntaxNonAsciiLocalPart,
                Outcome::Warning,
                Certainty::Conclusive,
                "delivery requires SMTPUTF8 support",
            ));
            if !address.local().nfc().eq(address.local().chars()) {
                findings.push(Finding::new(
                    FindingCode::UnicodeLocalPartNotNormalized,
                    Outcome::Warning,
                    Certainty::Conclusive,
                    "the Unicode local part is not in NFC",
                ));
            }
        }
        if address.domain().is_literal() {
            findings.push(Finding::new(
                FindingCode::SyntaxDomainLiteral,
                Outcome::Warning,
                Certainty::Conclusive,
                "the address uses a domain literal",
            ));
        }
        if !address.domain().display().is_ascii() && !address.domain().display().is_single_script()
        {
            findings.push(Finding::new(
                FindingCode::UnicodeMixedScript,
                Outcome::Warning,
                Certainty::Indicative,
                "the Unicode domain contains mixed scripts",
            ));
        }
        let overlay = self.policy.overlay(&address);
        append_classification(&mut findings, classification);
        if let Some(overlay) = overlay {
            findings.push(overlay);
        }
        Report::parsed(
            input,
            address,
            findings,
            classification,
            self.mode,
            self.policy.kind(),
            elapsed,
        )
    }
}

fn append_classification(findings: &mut Vec<Finding>, classification: Classification) {
    if classification.is_disposable() {
        findings.push(Finding::new(
            FindingCode::ClassificationDisposable,
            Outcome::Warning,
            Certainty::Indicative,
            "the domain belongs to a disposable-mail service",
        ));
    }
    if classification.is_role() {
        findings.push(Finding::new(
            FindingCode::ClassificationRole,
            Outcome::Warning,
            Certainty::Indicative,
            "the local part conventionally identifies a role",
        ));
    }
    if classification.is_free_mail() {
        findings.push(Finding::new(
            FindingCode::ClassificationFreeMail,
            Outcome::Warning,
            Certainty::Indicative,
            "the domain belongs to a free-mail provider",
        ));
    }
    if classification.suggested_domain().is_some() {
        findings.push(Finding::new(
            FindingCode::ClassificationTypo,
            Outcome::Warning,
            Certainty::Indicative,
            "the domain resembles a common provider-domain typo",
        ));
    }
}