systemprompt_identifiers/
email.rs1use crate::error::IdValidationError;
7use crate::{DbValue, ToDbValue};
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10use std::fmt;
11
12#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, JsonSchema)]
13#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
14#[cfg_attr(feature = "sqlx", sqlx(transparent))]
15#[serde(transparent)]
16pub struct Email(String);
17
18impl Email {
19 pub fn try_new(value: impl Into<String>) -> Result<Self, IdValidationError> {
20 let value = value.into();
21 if value.is_empty() {
22 return Err(IdValidationError::empty("Email"));
23 }
24 let parts: Vec<&str> = value.split('@').collect();
25 if parts.len() != 2 {
26 return Err(IdValidationError::invalid(
27 "Email",
28 "must contain exactly one '@' symbol",
29 ));
30 }
31 let local = parts[0];
32 let domain = parts[1];
33 if local.is_empty() {
34 return Err(IdValidationError::invalid(
35 "Email",
36 "local part (before @) cannot be empty",
37 ));
38 }
39 if local.starts_with('.') || local.ends_with('.') {
40 return Err(IdValidationError::invalid(
41 "Email",
42 "local part cannot start or end with '.'",
43 ));
44 }
45 if local.contains("..") {
46 return Err(IdValidationError::invalid(
47 "Email",
48 "local part cannot contain consecutive dots",
49 ));
50 }
51 if local.contains('\n') || local.contains('\r') {
52 return Err(IdValidationError::invalid(
53 "Email",
54 "email cannot contain newline characters",
55 ));
56 }
57 if domain.is_empty() {
58 return Err(IdValidationError::invalid(
59 "Email",
60 "domain part (after @) cannot be empty",
61 ));
62 }
63 if !domain.contains('.') {
64 return Err(IdValidationError::invalid(
65 "Email",
66 "domain must contain at least one '.'",
67 ));
68 }
69 if domain.starts_with('.') || domain.ends_with('.') {
70 return Err(IdValidationError::invalid(
71 "Email",
72 "domain cannot start or end with '.'",
73 ));
74 }
75 if domain.contains("..") {
76 return Err(IdValidationError::invalid(
77 "Email",
78 "domain cannot contain consecutive dots",
79 ));
80 }
81 if let Some(tld) = domain.rsplit('.').next()
82 && tld.len() < 2
83 {
84 return Err(IdValidationError::invalid(
85 "Email",
86 "TLD must be at least 2 characters",
87 ));
88 }
89 Ok(Self(value))
90 }
91
92 #[must_use]
93 #[expect(
94 clippy::expect_used,
95 reason = "infallible constructor reserved for already-validated inputs; untrusted input \
96 must go through try_new"
97 )]
98 pub fn new(value: impl Into<String>) -> Self {
99 Self::try_new(value).expect("Email validation failed")
100 }
101
102 #[must_use]
103 pub fn as_str(&self) -> &str {
104 &self.0
105 }
106
107 #[must_use]
108 pub fn local_part(&self) -> &str {
109 self.0.split('@').next().unwrap_or("")
110 }
111
112 #[must_use]
113 pub fn domain(&self) -> &str {
114 self.0.split('@').nth(1).unwrap_or("")
115 }
116}
117
118impl fmt::Display for Email {
119 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120 write!(f, "{}", self.0)
121 }
122}
123
124impl TryFrom<String> for Email {
125 type Error = IdValidationError;
126
127 fn try_from(s: String) -> Result<Self, Self::Error> {
128 Self::try_new(s)
129 }
130}
131
132impl TryFrom<&str> for Email {
133 type Error = IdValidationError;
134
135 fn try_from(s: &str) -> Result<Self, Self::Error> {
136 Self::try_new(s)
137 }
138}
139
140impl std::str::FromStr for Email {
141 type Err = IdValidationError;
142
143 fn from_str(s: &str) -> Result<Self, Self::Err> {
144 Self::try_new(s)
145 }
146}
147
148impl AsRef<str> for Email {
149 fn as_ref(&self) -> &str {
150 &self.0
151 }
152}
153
154impl<'de> Deserialize<'de> for Email {
155 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
156 where
157 D: serde::Deserializer<'de>,
158 {
159 let s = String::deserialize(deserializer)?;
160 Self::try_new(s).map_err(serde::de::Error::custom)
161 }
162}
163
164impl ToDbValue for Email {
165 fn to_db_value(&self) -> DbValue {
166 DbValue::String(self.0.clone())
167 }
168}
169
170impl ToDbValue for &Email {
171 fn to_db_value(&self) -> DbValue {
172 DbValue::String(self.0.clone())
173 }
174}