pub const HIDDEN: &str = "•••";
pub fn local_part(email: &str) -> Option<&str> {
email
.split_once('@')
.map(|(l, _)| l)
.filter(|l| !l.is_empty())
}
pub fn email(e: &str, mask: bool) -> String {
if mask { HIDDEN.into() } else { e.to_string() }
}
pub fn login_label(label: &str, login_email: Option<&str>, mask: bool) -> String {
if mask && from_email(label, login_email) {
HIDDEN.into()
} else {
label.to_string()
}
}
fn from_email(label: &str, login_email: Option<&str>) -> bool {
if label.contains('@') {
return true;
}
let Some(lp) = login_email.and_then(local_part) else {
return false;
};
match label.strip_prefix(lp) {
Some("") => true,
Some(rest) => rest
.strip_prefix('-')
.is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit())),
None => false,
}
}
pub fn words<'a>(emails: impl IntoIterator<Item = &'a str>) -> Vec<String> {
let mut out: Vec<String> = emails
.into_iter()
.filter_map(local_part)
.filter(|l| l.chars().count() >= 3)
.map(str::to_string)
.collect();
out.sort();
out.dedup();
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn labels_made_from_emails_are_hidden() {
let e = Some("alice@example.com");
assert_eq!(login_label("alice", e, true), HIDDEN);
assert_eq!(login_label("alice-2", e, true), HIDDEN);
assert_eq!(login_label("alice", e, false), "alice");
assert_eq!(login_label("work", e, true), "work");
assert_eq!(login_label("alice-team", e, true), "alice-team");
assert_eq!(login_label("alicex", e, true), "alicex");
assert_eq!(login_label("bob@example.com", None, true), HIDDEN);
assert_eq!(login_label("login", None, true), "login");
assert_eq!(email("alice@example.com", true), HIDDEN);
assert_eq!(
words([
"alice@example.com",
"al@example.com",
"x",
"alice@other.org"
]),
["alice"]
);
}
}