use std::borrow::Cow;
use std::ops::Deref;
use utopia_domains::Domain;
use crate::{Email, Error, Lists};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Format {
#[default]
Full,
Local,
Domain,
Provider,
Subdomain,
}
impl Format {
pub fn from_name(name: &[u8]) -> Self {
match name {
b"local" => Format::Local,
b"domain" => Format::Domain,
b"provider" => Format::Provider,
b"subdomain" => Format::Subdomain,
_ => Format::Full,
}
}
pub fn name(self) -> &'static str {
match self {
Format::Full => "full",
Format::Local => "local",
Format::Domain => "domain",
Format::Provider => "provider",
Format::Subdomain => "subdomain",
}
}
}
fn empty(s: &[u8]) -> bool {
s.is_empty() || s == b"0"
}
#[derive(Debug, Clone)]
pub struct Address {
email: Email,
domain: Domain<'static>,
}
impl Address {
pub fn new(input: impl AsRef<[u8]>) -> Result<Self, Error> {
let email = Email::new(input)?;
let domain = Domain::new(email.domain()).map_err(|e| Error::Parse(e.to_string().into_bytes()))?;
Ok(Self { email, domain })
}
pub fn email(&self) -> &Email {
&self.email
}
pub fn domain_instance(&self) -> &Domain<'static> {
&self.domain
}
pub fn has_valid_domain(&self) -> bool {
self.email.has_valid_domain_syntax() && (self.domain.is_known() || self.domain.is_test())
}
pub fn is_disposable(&self) -> bool {
Lists::global().is_disposable(self.email.domain(), self.domain.registerable().as_bytes())
}
pub fn is_free(&self) -> bool {
Lists::global().is_on_free_list(self.email.domain()) && !self.is_disposable()
}
pub fn is_corporate(&self) -> bool {
!self.is_free() && !self.is_disposable()
}
pub fn provider(&self) -> Cow<'_, [u8]> {
let registerable = self.domain.registerable();
if empty(registerable.as_bytes()) {
Cow::Borrowed(self.email.domain())
} else {
Cow::Owned(registerable.into_bytes())
}
}
pub fn subdomain(&self) -> &[u8] {
self.domain.sub().as_bytes()
}
pub fn has_subdomain(&self) -> bool {
!empty(self.subdomain())
}
pub fn formatted(&self, format: Format) -> Cow<'_, [u8]> {
match format {
Format::Full => Cow::Borrowed(self.email.get()),
Format::Local => Cow::Borrowed(self.email.local()),
Format::Domain => Cow::Borrowed(self.email.domain()),
Format::Provider => self.provider(),
Format::Subdomain => Cow::Borrowed(self.subdomain()),
}
}
}
impl Deref for Address {
type Target = Email;
fn deref(&self) -> &Email {
&self.email
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parts() {
let address = Address::new(" User.Name+Tag@Mail.Company.CO.UK \n").unwrap();
assert_eq!(address.get(), b"user.name+tag@mail.company.co.uk");
assert!(address.has_valid_domain());
assert_eq!((&*address.provider(), address.subdomain()), (&b"company.co.uk"[..], &b"mail"[..]));
assert!(address.has_subdomain());
assert_eq!(&*address.formatted(Format::from_name(b"provider")), b"company.co.uk");
assert_eq!(&*address.formatted(Format::from_name(b"nonsense")), address.get());
assert!(!Address::new("a@example.invalidtld").unwrap().has_valid_domain());
}
#[test]
fn errors() {
let url = Address::new("a@HTTP://x.com").unwrap_err();
assert_eq!(
(url.php_class(), url.to_string().as_str()),
("Exception", "'http://x.com' must be a valid domain or hostname")
);
assert_eq!(Address::new("a@b@c").unwrap_err().message(), b"'a@b@c' must be a valid email address");
}
#[test]
fn quirks() {
let address = Address::new("a@0.example.com").unwrap();
assert_eq!((address.subdomain(), address.has_subdomain()), (&b"0"[..], false));
}
#[test]
fn classification() {
let class = |s: &str| {
let address = Address::new(s).unwrap();
(address.is_free(), address.is_disposable(), address.is_corporate())
};
assert_eq!(class("a@gmail.com"), (true, false, false));
assert_eq!(class("a@10minutemail.com"), (false, true, false));
assert_eq!(class("a@x.mailinator.com"), (false, true, false));
assert_eq!(class("a@company.org"), (false, false, true));
}
}