#[derive(
Debug,
Clone,
PartialEq,
Eq,
Hash,
derive_more::AsRef,
derive_more::Display,
derive_more::From,
derive_more::FromStr,
derive_more::Into,
serde::Serialize,
serde::Deserialize,
)]
pub struct EmailAddress(email_address::EmailAddress);
impl EmailAddress {
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl PartialOrd for EmailAddress {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for EmailAddress {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_str().cmp(other.as_str())
}
}
#[cfg(test)]
mod tests {
use super::EmailAddress;
use pretty_assertions::assert_eq;
#[test]
fn from_str() {
assert_eq!(
EmailAddress(
"hello@example.com"
.parse()
.expect("value must be a parseable EmailAddress")
),
"hello@example.com"
.parse()
.expect("value must be a parseable EmailAddress")
);
}
}
#[cfg(test)]
mod serde_tests {
use super::EmailAddress;
use pretty_assertions::assert_eq;
use serde_json::json;
#[test]
fn serialize() {
assert_eq!(
json!(EmailAddress(
"hello@example.com"
.parse()
.expect("value must be a parseable EmailAddress")
)),
json!("hello@example.com")
);
}
#[test]
fn deserialize_good() {
let deserialized: EmailAddress = serde_json::from_value(json!("hello@example.com"))
.expect("value must be deserializable as EmailAddress");
assert_eq!(
deserialized,
EmailAddress(
"hello@example.com"
.parse()
.expect("value must be a parseable EmailAddress")
)
);
}
#[test]
fn deserialize_bad() {
assert!(serde_json::from_value::<EmailAddress>(json!("xyzabcd")).is_err());
assert!(serde_json::from_value::<EmailAddress>(json!(true)).is_err());
assert!(serde_json::from_value::<EmailAddress>(json!(123)).is_err());
}
}