haematite 0.6.2

Content-addressed, branchable, actor-native storage engine
Documentation
use std::fmt;

use crate::tree::TreeOperationError;

use super::handle::BranchError;

#[derive(Debug)]
pub enum BranchGetError<E> {
    Branch(BranchError),
    Tree(TreeOperationError<E>),
}

impl<E: fmt::Display> fmt::Display for BranchGetError<E> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Branch(error) => error.fmt(formatter),
            Self::Tree(error) => error.fmt(formatter),
        }
    }
}

impl<E: std::error::Error + 'static> std::error::Error for BranchGetError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Branch(error) => Some(error),
            Self::Tree(error) => Some(error),
        }
    }
}

#[derive(Debug)]
pub enum FenceOperationError<E> {
    NodePublication(E),
    RecordEntry(E),
}

impl<E: fmt::Display> fmt::Display for FenceOperationError<E> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NodePublication(source) => {
                write!(formatter, "node publication fence failed: {source}")
            }
            Self::RecordEntry(source) => write!(formatter, "record entry fence failed: {source}"),
        }
    }
}

impl<E: std::error::Error + 'static> std::error::Error for FenceOperationError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::NodePublication(source) | Self::RecordEntry(source) => Some(source),
        }
    }
}

pub fn preserve_node_publication_fence<T, E>(
    result: Result<T, E>,
) -> Result<T, FenceOperationError<E>> {
    result.map_err(FenceOperationError::NodePublication)
}

pub fn preserve_record_entry_fence<T, E>(
    result: Result<T, E>,
) -> Result<T, FenceOperationError<E>> {
    result.map_err(FenceOperationError::RecordEntry)
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use super::commit_error::BranchCommitError;
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use super::refstore::BranchRefError;

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[derive(Debug)]
pub enum CommitOperationError<E> {
    Public(BranchCommitError),
    Tree(TreeOperationError<E>),
    Fence(FenceOperationError<E>),
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<E: fmt::Display> CommitOperationError<E> {
    pub(crate) fn into_public(self) -> BranchCommitError {
        match self {
            Self::Public(error) => error,
            Self::Tree(error) => BranchCommitError::Tree(error.into_tree_error()),
            Self::Fence(FenceOperationError::NodePublication(source)) => {
                BranchCommitError::Barrier(source.to_string())
            }
            Self::Fence(FenceOperationError::RecordEntry(_)) => {
                unreachable!("record entry fences are not node commit operations")
            }
        }
    }
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<E: fmt::Display> fmt::Display for CommitOperationError<E> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Public(error) => error.fmt(formatter),
            Self::Tree(error) => error.fmt(formatter),
            Self::Fence(error) => error.fmt(formatter),
        }
    }
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<E: std::error::Error + 'static> std::error::Error for CommitOperationError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Public(error) => Some(error),
            Self::Tree(error) => Some(error),
            Self::Fence(error) => Some(error),
        }
    }
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<E> From<BranchCommitError> for CommitOperationError<E> {
    fn from(error: BranchCommitError) -> Self {
        Self::Public(error)
    }
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<E> From<BranchError> for CommitOperationError<E> {
    fn from(error: BranchError) -> Self {
        Self::Public(BranchCommitError::Branch(error))
    }
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<E> From<BranchRefError> for CommitOperationError<E> {
    fn from(error: BranchRefError) -> Self {
        Self::Public(BranchCommitError::Ref(error))
    }
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[derive(Debug)]
pub enum RecordOperationError<E> {
    Public(E),
    Fence(FenceOperationError<E>),
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<E> RecordOperationError<E> {
    pub(crate) fn into_public(self) -> E {
        match self {
            Self::Public(error) => error,
            Self::Fence(FenceOperationError::RecordEntry(source)) => source,
            Self::Fence(FenceOperationError::NodePublication(_)) => {
                unreachable!("node publication fences are not record operations")
            }
        }
    }
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<E: fmt::Display> fmt::Display for RecordOperationError<E> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Public(error) => error.fmt(formatter),
            Self::Fence(error) => error.fmt(formatter),
        }
    }
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<E: std::error::Error + 'static> std::error::Error for RecordOperationError<E> {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Public(error) => Some(error),
            Self::Fence(error) => Some(error),
        }
    }
}

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl<E> From<E> for RecordOperationError<E> {
    fn from(error: E) -> Self {
        Self::Public(error)
    }
}