use crate::{parse_id, Error};
pub fn validate(s: &str) -> Result<(), Error> {
let colon_idx = parse_id(s, b'@')?;
let localpart = &s[1..colon_idx];
let _ = localpart_is_fully_conforming(localpart)?;
Ok(())
}
pub fn localpart_is_fully_conforming(localpart: &str) -> Result<bool, Error> {
let is_fully_conforming = !localpart.is_empty()
&& localpart.bytes().all(
|b| matches!(b, b'0'..=b'9' | b'a'..=b'z' | b'-' | b'.' | b'=' | b'_' | b'/' | b'+'),
);
if !is_fully_conforming {
#[cfg(not(feature = "compat-user-id"))]
let is_invalid =
localpart.is_empty() || localpart.bytes().any(|b| b < 0x21 || b == b':' || b > 0x7E);
#[cfg(feature = "compat-user-id")]
let is_invalid = localpart.as_bytes().contains(&b':');
if is_invalid {
return Err(Error::InvalidCharacters);
}
}
Ok(is_fully_conforming)
}