zero-bounce 4.0.0

Wrapper library over the ZeroBounce API v2
Documentation
use chrono::NaiveDateTime;
use serde::Deserialize;

use crate::utility::structures::custom_deserialize::deserialize_naive_date;
use crate::utility::structures::validate_enums::{ZBValidateStatus, ZBValidateSubStatus};


/// One validation result.
///
/// `#[non_exhaustive]`: the API adds response fields over time, and a field
/// added later is not a breaking change. Values come from the API (or from
/// `serde_json` in tests); there is no struct literal outside this crate.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ZBValidation {
    // These default rather than fail: a repeated greylist_processing request
    // answers with nothing but "Message" (see `message`).
    #[serde(default)]
    pub address: String,
    #[serde(default)]
    pub status: String,
    #[serde(default)]
    pub sub_status: String,
    #[serde(default)]
    pub free_email: bool,
    pub did_you_mean: Option<String>,
    pub account: Option<String>,
    pub domain: Option<String>,
    pub domain_age_days: Option<String>,
    pub active_in_days: Option<String>,
    pub active_first_seen: Option<String>,
    pub smtp_provider: Option<String>,
    pub catchall_domain: Option<bool>,
    pub mx_record: Option<String>,
    pub mx_found: Option<String>,
    pub firstname: Option<String>,
    pub lastname: Option<String>,
    pub gender: Option<String>,
    pub country: Option<String>,
    pub region: Option<String>,
    pub city: Option<String>,
    pub zipcode: Option<String>,

    /// With `domain_info`: whether the domain has a website ("True", "False" or "unchecked").
    pub domain_website_exists: Option<String>,

    /// With `domain_info`: the WHOIS registrant company, "Registrant info not found"
    /// or "unchecked".
    pub domain_registrant_company_name: Option<String>,

    /// Set instead of a result while greylist processing is still running; validate
    /// again later to pick it up. The API spells this key "Message".
    #[serde(rename = "Message")]
    pub message: Option<String>,

    /// A greylist pickup that is still running carries no timestamp, so this
    /// defaults rather than failing the whole response.
    #[serde(default, deserialize_with="deserialize_naive_date")]
    pub processed_at: NaiveDateTime,
}

impl ZBValidation {
    /// Parse `status` string into typed enum (unknown API values become `ZBValidateStatus::UnknownValue`).
    pub fn status_enum(&self) -> ZBValidateStatus {
        self.status.parse().unwrap_or_else(|_| ZBValidateStatus::UnknownValue(self.status.clone()))
    }

    /// Parse `sub_status` string into typed enum (unknown API values become `ZBValidateSubStatus::UnknownValue`).
    pub fn sub_status_enum(&self) -> ZBValidateSubStatus {
        self.sub_status.parse().unwrap_or_else(|_| ZBValidateSubStatus::UnknownValue(self.sub_status.clone()))
    }
}


#[derive(Clone, Debug, Deserialize)]
pub struct ZBBatchError {
    pub error: String,
    pub email_address: String,
}


#[derive(Clone, Debug, Deserialize)]
pub struct ZBBatchValidation {
    pub email_batch: Vec<ZBValidation>,
    pub errors: Vec<ZBBatchError>,
}

#[cfg(test)]
mod test {
    use serde_json::{Result as SerdeResult, from_str};

    use super::*;
    use crate::utility::mock_constants::VALIDATION_RESPONSE_VALID;
    use crate::utility::mock_constants::VALIDATION_RESPONSE_INVALID;
    use crate::utility::mock_constants::VALIDATION_RESPONSE_NULL_FIELDS;
    use crate::utility::mock_constants::BATCH_VALIDATION_WITH_ERROR;
    use crate::utility::mock_constants::BATCH_VALIDATION_ERROR_ONLY;
    use crate::utility::mock_constants::BATCH_VALIDATION_NO_ERROR;

    #[test]
    fn test_validation_invalid_json() {
        let validation_res: SerdeResult<ZBValidation> = from_str("");
        assert!(validation_res.is_err());
    }

    #[test]
    fn test_validation_missing_expected_fields() {
        let validation_res: SerdeResult<ZBValidation> = from_str(VALIDATION_RESPONSE_NULL_FIELDS);
        assert!(validation_res.is_ok());

        let validation = validation_res.unwrap();
        assert_eq!(validation.did_you_mean, None);
        assert_eq!(validation.domain_age_days, Some("".to_string()));
    }

    #[test]
    fn test_validation_invalid_email_status() {
        let validation_res: SerdeResult<ZBValidation> = from_str(VALIDATION_RESPONSE_INVALID);
        assert!(validation_res.is_ok());

        let validation = validation_res.unwrap();
        assert_eq!(validation.status, "invalid".to_string());
        assert_eq!(validation.sub_status, "mailbox_not_found".to_string());
        assert_eq!(validation.did_you_mean, None);
        assert_eq!(validation.smtp_provider, Some("example".to_string()));
        assert!(!validation.free_email);

        let expected_date = NaiveDateTime::new(
            chrono::NaiveDate::from_ymd_opt(2023, 3, 23).unwrap(),
            chrono::NaiveTime::from_hms_milli_opt(12, 30, 28, 3).unwrap(),
        );
        assert_eq!(validation.processed_at, expected_date);
    }

    #[test]
    fn test_validation_valid_email_status() {
        let validation_res: SerdeResult<ZBValidation> = from_str(VALIDATION_RESPONSE_VALID);
        assert!(validation_res.is_ok());

        let validation = validation_res.unwrap();
        assert_eq!(validation.status, "valid".to_string());
        assert_eq!(validation.sub_status, "".to_string());
        assert_eq!(validation.did_you_mean, None);
        assert_eq!(validation.smtp_provider, Some("example".to_string()));
        assert_eq!(validation.catchall_domain, Some(false));
        assert!(!validation.free_email);

        let expected_date = NaiveDateTime::new(
            chrono::NaiveDate::from_ymd_opt(2023, 3, 23).unwrap(),
            chrono::NaiveTime::from_hms_milli_opt(13, 30, 28, 105).unwrap(),
        );
        assert_eq!(validation.processed_at, expected_date);
    }

    #[test]
    fn test_batch_error_only_content() {
        let batch: SerdeResult<ZBBatchValidation> = from_str(BATCH_VALIDATION_ERROR_ONLY);
        assert!(batch.is_ok());

        let batch_object = batch.unwrap();
        assert_eq!(batch_object.email_batch.len(), 0);
        assert_eq!(batch_object.errors.len(), 1);
    }

    #[test]
    fn test_batch_validation_and_error_content() {
        let batch: SerdeResult<ZBBatchValidation> = from_str(BATCH_VALIDATION_WITH_ERROR);
        assert!(batch.is_ok());

        let batch_object = batch.unwrap();
        assert_eq!(batch_object.email_batch.len(), 1);
        assert_eq!(batch_object.errors.len(), 1);
    }

    #[test]
    fn test_batch_validation_only_content() {
        let batch: SerdeResult<ZBBatchValidation> = from_str(BATCH_VALIDATION_NO_ERROR);
        assert!(batch.is_ok());

        let batch_object = batch.unwrap();
        assert_eq!(batch_object.email_batch.len(), 1);
        assert_eq!(batch_object.errors.len(), 0);
    }

}