utopia_emails/error.rs
1//! The errors of parsing an address and making it canonical.
2
3/// Why an address could not be parsed or made canonical.
4#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
5pub enum Error {
6 /// [`Email::new`](crate::Email::new) was given an empty address, or one
7 /// without exactly one `@` and something on both sides of it. The message
8 /// quotes the input, which can be any bytes.
9 ///
10 /// PHP: `Exception`.
11 #[error("{}", String::from_utf8_lossy(.0))]
12 Parse(Vec<u8>),
13 /// A provider's rules left the local part empty.
14 ///
15 /// PHP: `InvalidArgumentException`.
16 #[error("Email local part cannot be empty after normalization")]
17 EmptyLocal,
18}
19
20impl Error {
21 /// The class of the exception PHP throws.
22 pub fn php_class(&self) -> &'static str {
23 match self {
24 Error::Parse(_) => "Exception",
25 Error::EmptyLocal => "InvalidArgumentException",
26 }
27 }
28
29 /// The message, byte for byte.
30 pub fn message(&self) -> &[u8] {
31 match self {
32 Error::Parse(message) => message,
33 Error::EmptyLocal => b"Email local part cannot be empty after normalization",
34 }
35 }
36}