use std::fmt;
#[derive(Debug, Clone)]
pub struct InfraError {
message: String,
}
impl InfraError {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for InfraError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.message)
}
}
impl std::error::Error for InfraError {}
#[derive(Debug, Clone)]
pub enum TxError<E> {
Domain(E),
Infra(InfraError),
}
impl<E> TxError<E> {
pub fn into_domain<F>(self, map_infra: F) -> E
where
F: FnOnce(InfraError) -> E,
{
match self {
TxError::Domain(e) => e,
TxError::Infra(infra) => map_infra(infra),
}
}
}
impl<E: fmt::Display> fmt::Display for TxError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TxError::Domain(e) => write!(f, "{e}"),
TxError::Infra(e) => write!(f, "infrastructure error: {e}"),
}
}
}
impl<E: fmt::Debug + fmt::Display> std::error::Error for TxError<E> {}