1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use crate::member::error::EMailAddressParseError;
use std::fmt::{Display, Formatter};
#[derive(Clone, Debug, Eq)]
pub struct EMailAddress {
account: String,
domain: String,
}
impl std::hash::Hash for EMailAddress {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.account.hash(state);
self.domain.hash(state);
}
}
impl PartialEq for EMailAddress {
fn eq(&self, other: &Self) -> bool {
self.account == other.account && self.domain == other.domain
}
}
impl Display for EMailAddress {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}@{}", self.account, self.domain)
}
}
impl TryFrom<&str> for EMailAddress {
type Error = EMailAddressParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
let trimmed_value = value.trim();
let parts: Vec<&str> = trimmed_value.split('@').collect();
if parts.len() != 2 {
return Err(EMailAddressParseError::InvalidFormat);
}
let account = parts[0].trim().to_string();
let domain = parts[1].trim().to_string();
if account.is_empty() || domain.is_empty() {
return Err(EMailAddressParseError::EmptyPart);
}
Ok(Self { account, domain })
}
}
impl TryFrom<String> for EMailAddress {
type Error = EMailAddressParseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::try_from(value.as_str())
}
}
impl Serialize for EMailAddress {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl<'de> Deserialize<'de> for EMailAddress {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
EMailAddress::try_from(s).map_err(serde::de::Error::custom)
}
}
|