use std::error::Error;
use std::fmt::{self, Display};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EntityIdError {
InvalidFormat,
InvalidIdentifier,
}
impl Display for EntityIdError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
EntityIdError::InvalidFormat => f.write_str("The provided ID has an invalid format"),
EntityIdError::InvalidIdentifier => f.write_str("ID must contain a valid identifier"),
}
}
}
impl Error for EntityIdError {}
impl AsRef<str> for EntityIdError {
fn as_ref(&self) -> &str {
match self {
EntityIdError::InvalidFormat => "The provided ID has an invalid format",
EntityIdError::InvalidIdentifier => "ID must contain a valid identifier",
}
}
}
impl From<EntityIdError> for String {
fn from(err: EntityIdError) -> Self {
err.as_ref().to_string()
}
}
impl From<&EntityIdError> for String {
fn from(err: &EntityIdError) -> Self {
err.as_ref().to_string()
}
}
#[derive(Debug)]
pub enum IdentifierError {
Uuid(uuid::Error),
Ulid(ulid::DecodeError),
}
impl Display for IdentifierError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
IdentifierError::Uuid(err) => write!(f, "Invalid UUID format: {}", err),
IdentifierError::Ulid(err) => write!(f, "Invalid ULID format: {}", err),
}
}
}
impl Error for IdentifierError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
IdentifierError::Uuid(err) => Some(err),
IdentifierError::Ulid(err) => Some(err),
}
}
}
impl From<uuid::Error> for IdentifierError {
fn from(err: uuid::Error) -> Self {
IdentifierError::Uuid(err)
}
}
impl From<ulid::DecodeError> for IdentifierError {
fn from(err: ulid::DecodeError) -> Self {
IdentifierError::Ulid(err)
}
}
impl From<IdentifierError> for String {
fn from(err: IdentifierError) -> Self {
match &err {
IdentifierError::Uuid(uuid_err) => format!("Invalid UUID format: {}", uuid_err),
IdentifierError::Ulid(ulid_err) => format!("Invalid ULID format: {}", ulid_err),
}
}
}
impl From<&IdentifierError> for String {
fn from(err: &IdentifierError) -> Self {
match err {
IdentifierError::Uuid(uuid_err) => format!("Invalid UUID format: {}", uuid_err),
IdentifierError::Ulid(ulid_err) => format!("Invalid ULID format: {}", ulid_err),
}
}
}
impl IdentifierError {
pub fn uuid_error(&self) -> Option<&uuid::Error> {
match self {
IdentifierError::Uuid(err) => Some(err),
_ => None,
}
}
pub fn ulid_error(&self) -> Option<&ulid::DecodeError> {
match self {
IdentifierError::Ulid(err) => Some(err),
_ => None,
}
}
pub fn error_message(&self) -> String {
match self {
IdentifierError::Uuid(err) => err.to_string(),
IdentifierError::Ulid(err) => err.to_string(),
}
}
}