use std::{error::Error, fmt};
use crate::{
Categorized, Category, Failure, NotARevision, Permanent, PreconditionsError, Transient,
};
use super::failure::{Sorted, WriteSorted};
fn fmt_outcome_unknown(failure: &Failure, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "outcome unknown: {}", failure.message())
}
fn source_of_outcome_unknown(failure: &Failure) -> Option<&(dyn Error + 'static)> {
let cause: &(dyn Error + 'static) = failure.cause()?;
Some(cause)
}
#[derive(Debug)]
#[non_exhaustive]
pub enum LoadError {
Transient(Transient),
Permanent(Permanent),
}
impl From<Sorted> for LoadError {
fn from(sorted: Sorted) -> Self {
match sorted {
Sorted::Transient(failure) => Self::Transient(failure),
Sorted::Permanent(failure) => Self::Permanent(failure),
}
}
}
impl From<Permanent> for LoadError {
fn from(failure: Permanent) -> Self {
Self::Permanent(failure)
}
}
impl fmt::Display for LoadError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Transient(failure) => failure.fmt(formatter),
Self::Permanent(failure) => failure.fmt(formatter),
}
}
}
impl Error for LoadError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Transient(failure) => failure.source(),
Self::Permanent(failure) => failure.source(),
}
}
}
impl Categorized for LoadError {
fn category(&self) -> Option<Category> {
match self {
Self::Transient(failure) => failure.category(),
Self::Permanent(failure) => failure.category(),
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum WriteError {
Transient(Transient),
Permanent(Permanent),
OutcomeUnknown(Failure),
NotARevision(NotARevision),
}
impl From<WriteSorted> for WriteError {
fn from(sorted: WriteSorted) -> Self {
match sorted {
WriteSorted::Sorted(Sorted::Transient(failure)) => Self::Transient(failure),
WriteSorted::Sorted(Sorted::Permanent(failure)) => Self::Permanent(failure),
WriteSorted::OutcomeUnknown(failure) => Self::OutcomeUnknown(failure),
}
}
}
impl From<PreconditionsError> for WriteError {
fn from(refusal: PreconditionsError) -> Self {
match refusal {
PreconditionsError::Permanent(failure) => Self::Permanent(failure),
PreconditionsError::NotARevision(refusal) => Self::NotARevision(refusal),
}
}
}
impl From<Permanent> for WriteError {
fn from(failure: Permanent) -> Self {
Self::Permanent(failure)
}
}
impl fmt::Display for WriteError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Transient(failure) => failure.fmt(formatter),
Self::Permanent(failure) => failure.fmt(formatter),
Self::OutcomeUnknown(failure) => fmt_outcome_unknown(failure, formatter),
Self::NotARevision(refusal) => refusal.fmt(formatter),
}
}
}
impl Error for WriteError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Transient(failure) => failure.source(),
Self::Permanent(failure) => failure.source(),
Self::OutcomeUnknown(failure) => source_of_outcome_unknown(failure),
Self::NotARevision(refusal) => refusal.source(),
}
}
}
impl Categorized for WriteError {
fn category(&self) -> Option<Category> {
match self {
Self::Transient(failure) => failure.category(),
Self::Permanent(failure) => failure.category(),
Self::OutcomeUnknown(_) | Self::NotARevision(_) => None,
}
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum RegisterSchemasError {
Transient(Transient),
Permanent(Permanent),
}
impl From<Sorted> for RegisterSchemasError {
fn from(sorted: Sorted) -> Self {
match sorted {
Sorted::Transient(failure) => Self::Transient(failure),
Sorted::Permanent(failure) => Self::Permanent(failure),
}
}
}
impl From<Permanent> for RegisterSchemasError {
fn from(failure: Permanent) -> Self {
Self::Permanent(failure)
}
}
impl fmt::Display for RegisterSchemasError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Transient(failure) => failure.fmt(formatter),
Self::Permanent(failure) => failure.fmt(formatter),
}
}
}
impl Error for RegisterSchemasError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Transient(failure) => failure.source(),
Self::Permanent(failure) => failure.source(),
}
}
}
impl Categorized for RegisterSchemasError {
fn category(&self) -> Option<Category> {
match self {
Self::Transient(failure) => failure.category(),
Self::Permanent(failure) => failure.category(),
}
}
}