diff --git a/src/contact.rs b/src/contact.rs index e0079d2b8..476ef7b1e 100644 --- a/src/contact.rs +++ b/src/contact.rs @@ -71,7 +71,7 @@ impl ContactAddress { /// normalizing and validating it. pub fn new(s: &str) -> Result { let addr = addr_normalize(s); - if !may_be_valid_addr(addr) { + if !may_be_valid_addr(&addr) { bail!("invalid address {:?}", s); } Ok(Self(addr.to_string())) @@ -565,7 +565,7 @@ impl Contact { let addr_normalized = addr_normalize(addr); - if context.is_self_addr(addr_normalized).await? { + if context.is_self_addr(&addr_normalized).await? { return Ok(Some(ContactId::SELF)); } @@ -1405,12 +1405,13 @@ pub fn may_be_valid_addr(addr: &str) -> bool { res.is_ok() } -/// Returns address with whitespace trimmed and `mailto:` prefix removed. -pub fn addr_normalize(addr: &str) -> &str { - let norm = addr.trim(); +/// Returns address lowercased, +/// with whitespace trimmed and `mailto:` prefix removed. +pub fn addr_normalize(addr: &str) -> String { + let norm = addr.trim().to_lowercase(); if norm.starts_with("mailto:") { - norm.get(7..).unwrap_or(norm) + norm.get(7..).unwrap_or(&norm).to_string() } else { norm } @@ -1665,8 +1666,8 @@ fn cat_fingerprint( /// Compares two email addresses, normalizing them beforehand. pub fn addr_cmp(addr1: &str, addr2: &str) -> bool { - let norm1 = addr_normalize(addr1).to_lowercase(); - let norm2 = addr_normalize(addr2).to_lowercase(); + let norm1 = addr_normalize(addr1); + let norm2 = addr_normalize(addr2); norm1 == norm2 } @@ -1864,10 +1865,7 @@ mod tests { fn test_normalize_addr() { assert_eq!(addr_normalize("mailto:john@doe.com"), "john@doe.com"); assert_eq!(addr_normalize(" hello@world.com "), "hello@world.com"); - - // normalisation preserves case to allow user-defined spelling. - // however, case is ignored on addr_cmp() - assert_ne!(addr_normalize("John@Doe.com"), "john@doe.com"); + assert_eq!(addr_normalize("John@Doe.com"), "john@doe.com"); } #[test] diff --git a/src/qr.rs b/src/qr.rs index 43c65ce39..02b339600 100644 --- a/src/qr.rs +++ b/src/qr.rs @@ -789,7 +789,7 @@ fn normalize_address(addr: &str) -> Result { let new_addr = percent_decode_str(addr).decode_utf8()?; let new_addr = addr_normalize(&new_addr); - ensure!(may_be_valid_addr(new_addr), "Bad e-mail address"); + ensure!(may_be_valid_addr(&new_addr), "Bad e-mail address"); Ok(new_addr.to_string()) }