use core::fmt;
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[non_exhaustive]
pub enum ErrorKind {
NotANumber,
MissingRegion,
UnsupportedRegion,
UnsupportedCallingCode,
TooShort,
TooLong,
NotCanonical,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Error {
kind: ErrorKind,
}
impl Error {
pub(crate) const fn new(kind: ErrorKind) -> Self {
Self { kind }
}
pub const fn kind(self) -> ErrorKind {
self.kind
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let message = match self.kind {
ErrorKind::NotANumber => "input is not a phone number",
ErrorKind::MissingRegion => "a default region is required",
ErrorKind::UnsupportedRegion => "unsupported region",
ErrorKind::UnsupportedCallingCode => "unsupported calling code",
ErrorKind::TooShort => "phone number is too short",
ErrorKind::TooLong => "phone number is too long",
ErrorKind::NotCanonical => "phone number is not in canonical E.164 or RFC 3966 form",
};
f.write_str(message)
}
}
impl std::error::Error for Error {}