pactum-annotations 0.6.1

Declarative field normalization and validation for pactum contracts
Documentation
use crate::error::FieldIssue;

/// Dial-plan entry for a locale (ISO 3166-1 alpha-2).
#[derive(Debug, Clone, Copy)]
pub struct DialPlan {
    pub locale: &'static str,
    pub country_code: &'static str,
    pub national_min: usize,
    pub national_max: usize,
}

pub const DIAL_PLANS: &[DialPlan] = &[
    DialPlan {
        locale: "US",
        country_code: "1",
        national_min: 10,
        national_max: 10,
    },
    DialPlan {
        locale: "CA",
        country_code: "1",
        national_min: 10,
        national_max: 10,
    },
    DialPlan {
        locale: "MX",
        country_code: "52",
        national_min: 10,
        national_max: 10,
    },
    DialPlan {
        locale: "GB",
        country_code: "44",
        national_min: 9,
        national_max: 10,
    },
    DialPlan {
        locale: "ES",
        country_code: "34",
        national_min: 9,
        national_max: 9,
    },
    DialPlan {
        locale: "BR",
        country_code: "55",
        national_min: 10,
        national_max: 11,
    },
    DialPlan {
        locale: "FR",
        country_code: "33",
        national_min: 9,
        national_max: 9,
    },
    DialPlan {
        locale: "DE",
        country_code: "49",
        national_min: 10,
        national_max: 11,
    },
    DialPlan {
        locale: "AU",
        country_code: "61",
        national_min: 9,
        national_max: 9,
    },
    DialPlan {
        locale: "IN",
        country_code: "91",
        national_min: 10,
        national_max: 10,
    },
    DialPlan {
        locale: "JP",
        country_code: "81",
        national_min: 9,
        national_max: 10,
    },
];

pub fn dial_plan(locale: &str) -> Option<&'static DialPlan> {
    let locale = locale.trim().to_uppercase();
    DIAL_PLANS.iter().find(|plan| plan.locale == locale)
}

/// Strict E.164: leading `+`, 7..=15 digits after the plus.
pub fn check_phone_e164(field: &str, value: &str) -> Result<(), FieldIssue> {
    let Some(digits) = value.strip_prefix('+') else {
        return Err(FieldIssue::new(
            field,
            "INVALID_PHONE",
            "phone must be E.164 with a leading +",
        ));
    };
    if !digits.bytes().all(|b| b.is_ascii_digit()) {
        return Err(FieldIssue::new(
            field,
            "INVALID_PHONE",
            "phone may only contain + and digits",
        ));
    }
    if !(7..=15).contains(&digits.len()) {
        return Err(FieldIssue::new(
            field,
            "INVALID_PHONE",
            "phone digit count must be between 7 and 15",
        ));
    }
    Ok(())
}

/// Locale-aware phone: E.164 whose country code and national length match the dial plan.
pub fn check_phone_locale(field: &str, value: &str, locale: &str) -> Result<(), FieldIssue> {
    check_phone_e164(field, value)?;
    let Some(plan) = dial_plan(locale) else {
        return Err(FieldIssue::new(
            field,
            "INVALID_PHONE_LOCALE",
            format!("unknown phone locale {locale}"),
        ));
    };
    let digits = &value[1..];
    if !digits.starts_with(plan.country_code) {
        return Err(FieldIssue::new(
            field,
            "INVALID_PHONE",
            format!("phone must start with +{}", plan.country_code),
        ));
    }
    let national = digits.len() - plan.country_code.len();
    if national < plan.national_min || national > plan.national_max {
        return Err(FieldIssue::new(
            field,
            "INVALID_PHONE",
            format!(
                "national number length must be {}..={}",
                plan.national_min, plan.national_max
            ),
        ));
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn accepts_us_number() {
        assert!(check_phone_locale("phone", "+14155552671", "US").is_ok());
    }

    #[test]
    fn rejects_short_e164() {
        assert!(check_phone_e164("phone", "+123").is_err());
    }

    #[test]
    fn rejects_untrimmed_e164() {
        assert!(check_phone_e164("phone", "+14155552671 ").is_err());
        assert!(check_phone_e164("phone", " +14155552671").is_err());
    }
}