#![cfg_attr(
not(test),
deny(clippy::unwrap_used, clippy::expect_used, clippy::panic)
)]
mod config;
mod error;
mod normalize;
mod parser;
mod provider;
mod validate;
pub use config::{
CasePolicy, Config, ConfigBuilder, DomainCheck, DotPolicy, Strictness, SubaddressPolicy,
};
pub use error::{Error, ErrorKind};
pub use normalize::confusable_skeleton;
pub use provider::{ProviderRegistry, ProviderRule};
#[derive(Debug, Clone)]
pub struct EmailAddress {
original: String,
local_part: String,
tag: Option<String>,
domain: String,
domain_unicode: Option<String>,
display_name: Option<String>,
skeleton: Option<String>,
freemail: bool,
}
impl EmailAddress {
pub fn parse_with(input: &str, config: &Config) -> Result<Self, Error> {
let parsed = parser::parse(
input,
config.strictness,
config.allow_display_name,
config.allow_domain_literal,
)?;
let normalized = normalize::normalize(&parsed, config)?;
validate::validate(&parsed, &normalized, config)?;
let freemail = config
.providers
.lookup(&normalized.domain)
.is_some_and(|p| p.is_freemail());
Ok(Self {
original: parsed.input.to_string(),
local_part: normalized.local_part,
tag: normalized.tag,
domain: normalized.domain,
domain_unicode: normalized.domain_unicode,
display_name: normalized.display_name,
skeleton: normalized.skeleton,
freemail,
})
}
pub fn local_part(&self) -> &str {
&self.local_part
}
pub fn tag(&self) -> Option<&str> {
self.tag.as_deref()
}
pub fn domain(&self) -> &str {
&self.domain
}
pub fn domain_unicode(&self) -> &str {
self.domain_unicode.as_deref().unwrap_or(&self.domain)
}
pub fn display_name(&self) -> Option<&str> {
self.display_name.as_deref()
}
pub fn canonical(&self) -> String {
if needs_quoting(&self.local_part) {
let escaped = escape_local_part(&self.local_part);
format!("\"{}\"@{}", escaped, self.domain)
} else {
format!("{}@{}", self.local_part, self.domain)
}
}
pub fn original(&self) -> &str {
&self.original
}
pub fn skeleton(&self) -> Option<&str> {
self.skeleton.as_deref()
}
pub fn is_freemail(&self) -> bool {
self.freemail
}
pub fn parse_batch(inputs: &[&str], config: &Config) -> Vec<Result<Self, Error>> {
inputs
.iter()
.map(|input| Self::parse_with(input, config))
.collect()
}
#[cfg(feature = "rayon")]
pub fn parse_batch_par(inputs: &[&str], config: &Config) -> Vec<Result<Self, Error>> {
use rayon::prelude::*;
inputs
.par_iter()
.map(|input| Self::parse_with(input, config))
.collect()
}
}
impl std::fmt::Display for EmailAddress {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let local = if needs_quoting(&self.local_part) {
format!("\"{}\"", escape_local_part(&self.local_part))
} else {
self.local_part.clone()
};
match &self.display_name {
Some(name) => write!(
f,
"\"{}\" <{}@{}>",
escape_display_name(name),
local,
self.domain
),
None => write!(f, "{}@{}", local, self.domain),
}
}
}
fn needs_quoting(local: &str) -> bool {
if local.is_empty() {
return true;
}
if local.starts_with('.') || local.ends_with('.') || local.contains("..") {
return true;
}
local.chars().any(|ch| {
!ch.is_ascii_alphanumeric()
&& !matches!(
ch,
'!' | '#'
| '$'
| '%'
| '&'
| '\''
| '*'
| '+'
| '-'
| '/'
| '='
| '?'
| '^'
| '_'
| '`'
| '{'
| '|'
| '}'
| '~'
| '.'
)
&& (ch as u32) < 0x80 })
}
fn escape_local_part(local: &str) -> String {
let mut escaped = String::with_capacity(local.len());
for ch in local.chars() {
match ch {
'"' | '\\' => {
escaped.push('\\');
escaped.push(ch);
}
'\r' | '\n' => {} _ => escaped.push(ch),
}
}
escaped
}
fn escape_display_name(name: &str) -> String {
let mut escaped = String::with_capacity(name.len());
for ch in name.chars() {
match ch {
'"' => {
escaped.push('\\');
escaped.push('"');
}
'\\' => {
escaped.push('\\');
escaped.push('\\');
}
'\r' | '\n' => {} _ => escaped.push(ch),
}
}
escaped
}
impl PartialEq for EmailAddress {
fn eq(&self, other: &Self) -> bool {
self.local_part == other.local_part && self.domain == other.domain
}
}
impl Eq for EmailAddress {}
impl std::hash::Hash for EmailAddress {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.local_part.hash(state);
self.domain.hash(state);
}
}
impl std::str::FromStr for EmailAddress {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse_with(s, &Config::default())
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for EmailAddress {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.canonical().serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for EmailAddress {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = String::deserialize(deserializer)?;
s.parse().map_err(serde::de::Error::custom)
}
}
#[cfg(test)]
mod tests;