tuwunel_service/threepid/
canonical.rs1use tuwunel_core::{Err, Result, err};
2
3const MAX_EMAIL_LEN: usize = 500;
6
7pub fn canonicalize_email(address: &str) -> Result<String> {
15 if address.len() > MAX_EMAIL_LEN {
16 return Err!(Request(InvalidParam("Email address is too long")));
17 }
18
19 let (local, domain) = address
20 .rsplit_once('@')
21 .ok_or_else(|| err!(Request(InvalidParam("Email address must contain a domain"))))?;
22
23 if local.is_empty() || domain.is_empty() {
24 return Err!(Request(InvalidParam("Email address is malformed")));
25 }
26
27 let local = case_fold(local);
28 let domain = case_fold(domain);
29
30 Ok(format!("{local}@{domain}"))
31}
32
33fn case_fold(input: &str) -> String {
37 input.chars().fold(String::new(), |mut out, c| {
38 match c {
39 | 'ß' => out.push_str("ss"),
40 | other => out.extend(other.to_lowercase()),
41 }
42
43 out
44 })
45}