Use slice patterns in EmailAddress::from_str()

This commit is contained in:
Alexander Krotov
2020-03-28 03:24:36 +03:00
committed by Alexander Krotov
parent 0327000f8d
commit cb92579461

View File

@@ -494,10 +494,8 @@ impl FromStr for EmailAddress {
ensure!(!input.is_empty(), "empty string is not valid"); ensure!(!input.is_empty(), "empty string is not valid");
let parts: Vec<&str> = input.rsplitn(2, '@').collect(); let parts: Vec<&str> = input.rsplitn(2, '@').collect();
ensure!(parts.len() > 1, "missing '@' character"); match &parts[..] {
let local = parts[1]; [domain, local] => {
let domain = parts[0];
ensure!( ensure!(
!local.is_empty(), !local.is_empty(),
"empty string is not valid for local part" "empty string is not valid for local part"
@@ -509,10 +507,13 @@ impl FromStr for EmailAddress {
ensure!(dot.unwrap() < domain.len() - 2, "invalid domain"); ensure!(dot.unwrap() < domain.len() - 2, "invalid domain");
Ok(EmailAddress { Ok(EmailAddress {
local: local.to_string(), local: (*local).to_string(),
domain: domain.to_string(), domain: (*domain).to_string(),
}) })
} }
_ => bail!("missing '@' character"),
}
}
} }
/// Utility to check if a in the binary represantion of listflags /// Utility to check if a in the binary represantion of listflags