use std::{
borrow::Cow,
cell::{Ref, RefMut},
error::Error,
fmt,
rc::Rc,
sync::Arc,
};
use constant_string::constant_string;
use email_address::EmailAddress;
pub use email_address::Options as EmailAddressOptions;
constant_string!(
EmailAddressErrorCode,
EMAIL_ADDRESS_ERROR_CODE,
"emailAddress"
);
#[derive(Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(
tag = "subcode",
rename_all = "camelCase",
rename_all_fields = "camelCase"
)
)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum EmailAddressError {
InvalidCharacter {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
MissingSeparator {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
LocalPartEmpty {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
LocalPartTooLong {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
DomainEmpty {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
DomainTooLong {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
SubDomainEmpty {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
SubDomainTooLong {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
DomainTooFew {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
DomainInvalidSeparator {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
UnbalancedQuotes {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
InvalidComment {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
InvalidIPAddress {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
UnsupportedDomainLiteral {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
UnsupportedDisplayName {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
MissingDisplayName {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
MissingEndBracket {
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "utoipa", schema(inline, required))]
code: EmailAddressErrorCode,
#[cfg(feature = "message")]
message: String,
},
}
impl fmt::Display for EmailAddressError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:#?}")
}
}
impl Error for EmailAddressError {}
impl From<email_address::Error> for EmailAddressError {
fn from(value: email_address::Error) -> Self {
let code = EmailAddressErrorCode;
match value {
email_address::Error::InvalidCharacter => Self::InvalidCharacter {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::MissingSeparator => Self::MissingSeparator {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::LocalPartEmpty => Self::LocalPartEmpty {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::LocalPartTooLong => Self::LocalPartTooLong {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::DomainEmpty => Self::DomainEmpty {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::DomainTooLong => Self::DomainTooLong {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::SubDomainEmpty => Self::SubDomainEmpty {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::SubDomainTooLong => Self::SubDomainTooLong {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::DomainTooFew => Self::DomainTooFew {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::DomainInvalidSeparator => Self::DomainInvalidSeparator {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::UnbalancedQuotes => Self::UnbalancedQuotes {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::InvalidComment => Self::InvalidComment {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::InvalidIPAddress => Self::InvalidIPAddress {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::UnsupportedDomainLiteral => Self::UnsupportedDomainLiteral {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::UnsupportedDisplayName => Self::UnsupportedDisplayName {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::MissingDisplayName => Self::MissingDisplayName {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
email_address::Error::MissingEndBracket => Self::MissingEndBracket {
code,
#[cfg(feature = "message")]
message: "".to_owned(),
},
}
}
}
pub trait ValidateEmailAddress {
fn email_address(&self) -> Option<Cow<'_, str>>;
fn validate_email_address(
&self,
options: EmailAddressOptions,
) -> Result<(), EmailAddressError> {
let Some(email_address) = self.email_address() else {
return Ok(());
};
EmailAddress::parse_with_options(&email_address, options)
.map_err(EmailAddressError::from)?;
Ok(())
}
}
impl ValidateEmailAddress for str {
fn email_address(&self) -> Option<Cow<'_, str>> {
Some(self.into())
}
}
impl ValidateEmailAddress for &str {
fn email_address(&self) -> Option<Cow<'_, str>> {
Some((*self).into())
}
}
impl ValidateEmailAddress for String {
fn email_address(&self) -> Option<Cow<'_, str>> {
Some(self.into())
}
}
impl ValidateEmailAddress for Cow<'_, str> {
fn email_address(&self) -> Option<Cow<'_, str>> {
Some(self.clone())
}
}
impl ValidateEmailAddress for EmailAddress {
fn email_address(&self) -> Option<Cow<'_, str>> {
Some(self.as_str().into())
}
}
impl<T> ValidateEmailAddress for Option<T>
where
T: ValidateEmailAddress,
{
fn email_address(&self) -> Option<Cow<'_, str>> {
if let Some(s) = self {
T::email_address(s)
} else {
None
}
}
}
macro_rules! validate_with_deref {
($type:ty) => {
impl<T> ValidateEmailAddress for $type
where
T: ValidateEmailAddress,
{
fn email_address(&self) -> Option<Cow<'_, str>> {
T::email_address(self)
}
}
};
}
validate_with_deref!(&T);
validate_with_deref!(Arc<T>);
validate_with_deref!(Box<T>);
validate_with_deref!(Rc<T>);
validate_with_deref!(Ref<'_, T>);
validate_with_deref!(RefMut<'_, T>);
#[cfg(test)]
mod tests {
use std::{borrow::Cow, cell::RefCell, rc::Rc, sync::Arc};
use email_address::EmailAddress;
use super::{EmailAddressError, EmailAddressOptions, ValidateEmailAddress};
#[test]
fn ok() {
let options = EmailAddressOptions::default().without_display_text();
assert_eq!((*"admin@localhost").validate_email_address(options), Ok(()));
assert_eq!("admin@localhost".validate_email_address(options), Ok(()));
assert_eq!(
"admin@localhost".to_owned().validate_email_address(options),
Ok(())
);
assert_eq!(
Cow::<str>::Borrowed("admin@localhost").validate_email_address(options),
Ok(())
);
assert_eq!(
Cow::<str>::Owned("admin@localhost".to_owned()).validate_email_address(options),
Ok(())
);
assert_eq!(
EmailAddress::new_unchecked("admin@localhost").validate_email_address(options),
Ok(())
);
assert_eq!(None::<&str>.validate_email_address(options), Ok(()));
assert_eq!(
Some("admin@localhost").validate_email_address(options),
Ok(())
);
assert_eq!((&"admin@localhost").validate_email_address(options), Ok(()));
assert_eq!(
Box::new("admin@localhost").validate_email_address(options),
Ok(())
);
assert_eq!(
Arc::new("admin@localhost").validate_email_address(options),
Ok(())
);
assert_eq!(
Rc::new("admin@localhost").validate_email_address(options),
Ok(())
);
let cell = RefCell::new("admin@localhost");
assert_eq!(cell.borrow().validate_email_address(options), Ok(()));
assert_eq!(cell.borrow_mut().validate_email_address(options), Ok(()));
}
#[test]
fn invalid_error() {
let options = EmailAddressOptions::default().without_display_text();
assert_eq!(
(*"admin").validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
assert_eq!(
"admin".validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
assert_eq!(
"admin".to_owned().validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
assert_eq!(
Cow::<str>::Borrowed("admin").validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
assert_eq!(
Cow::<str>::Owned("admin".to_owned()).validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
assert_eq!(
EmailAddress::new_unchecked("admin").validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
assert_eq!(
Some("admin").validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
assert_eq!(
(&"admin").validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
assert_eq!(
Box::new("admin").validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
assert_eq!(
Arc::new("admin").validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
assert_eq!(
Rc::new("admin").validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
let cell = RefCell::new("admin");
assert_eq!(
cell.borrow().validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
assert_eq!(
cell.borrow_mut().validate_email_address(options),
Err(EmailAddressError::from(
email_address::Error::MissingSeparator
))
);
}
}