use core::fmt;
use oxrdf::NamedNode;
use crate::journal::Position;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum StoreError {
Unavailable(String),
Invalid(String),
Unsupported(String),
Backend(String),
}
impl StoreError {
pub fn invalid(msg: impl Into<String>) -> Self {
StoreError::Invalid(msg.into())
}
pub fn backend(err: impl fmt::Display) -> Self {
StoreError::Backend(err.to_string())
}
pub fn is_transient(&self) -> bool {
matches!(self, StoreError::Unavailable(_))
}
}
impl fmt::Display for StoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
StoreError::Unavailable(m) => write!(f, "store unavailable: {m}"),
StoreError::Invalid(m) => write!(f, "invalid request: {m}"),
StoreError::Unsupported(m) => write!(f, "unsupported: {m}"),
StoreError::Backend(m) => write!(f, "backend error: {m}"),
}
}
}
impl std::error::Error for StoreError {}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AppendError {
Conflict {
expected: Position,
actual: Position,
},
Fenced {
scope: NamedNode,
epoch: u64,
highest: u64,
},
Unsupported(String),
Invalid(String),
Storage(StoreError),
}
impl AppendError {
pub fn is_conflict(&self) -> bool {
matches!(self, AppendError::Conflict { .. })
}
pub fn is_fenced(&self) -> bool {
matches!(self, AppendError::Fenced { .. })
}
}
impl fmt::Display for AppendError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AppendError::Conflict { expected, actual } => write!(
f,
"append conflict: expected position {}, journal is at {}",
expected.get(),
actual.get()
),
AppendError::Fenced {
scope,
epoch,
highest,
} => write!(
f,
"append fenced: epoch {epoch} for {scope} is below the accepted epoch {highest}"
),
AppendError::Unsupported(m) => write!(f, "unsupported append: {m}"),
AppendError::Invalid(m) => write!(f, "invalid append: {m}"),
AppendError::Storage(e) => write!(f, "append failed: {e}"),
}
}
}
impl std::error::Error for AppendError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
AppendError::Storage(e) => Some(e),
_ => None,
}
}
}
impl From<StoreError> for AppendError {
fn from(e: StoreError) -> Self {
match e {
StoreError::Invalid(m) => AppendError::Invalid(m),
StoreError::Unsupported(m) => AppendError::Unsupported(m),
other => AppendError::Storage(other),
}
}
}