daaki-smtp 0.2.0

An async SMTP client library
Documentation
//! DELIVERBY parameter validation helpers.
//!
//! RFC 2852 Section 4 defines the `BY=` MAIL FROM parameter. The `by-time`
//! production allows at most 9 decimal digits and a by-mode of `R` forbids
//! zero or negative values.

use crate::error::Error;
use crate::types::{DeliverBy, DeliverByMode};

/// RFC 2852 Section 4: `by-time = ["-" / "+"]1*9DIGIT`.
pub(crate) const MAX_DELIVER_BY_SECONDS: i128 = 999_999_999;

/// RFC 2852 Section 4: validate DELIVERBY syntax constraints.
pub(crate) fn validate_deliver_by_value(deliver_by: &DeliverBy) -> Result<(), Error> {
    let magnitude = i128::from(deliver_by.seconds).abs();
    if magnitude > MAX_DELIVER_BY_SECONDS {
        return Err(Error::Protocol(format!(
            "DELIVERBY exceeds the RFC 2852 Section 4 9-digit limit: {}",
            deliver_by.seconds
        )));
    }

    if matches!(deliver_by.mode, DeliverByMode::Return) && deliver_by.seconds <= 0 {
        return Err(Error::Protocol(format!(
            "DELIVERBY by-time must be positive with Return mode: {} \
             (RFC 2852 Section 4)",
            deliver_by.seconds
        )));
    }

    Ok(())
}