use std::io;
use std::num::TryFromIntError;
use thiserror::Error;
use crate::Group;
use crate::Id;
use crate::Vertex;
#[derive(Debug, Error)]
pub enum DagError {
#[error("{0:?} cannot be found")]
VertexNotFound(Vertex),
#[error("{0:?} cannot be found")]
IdNotFound(Id),
#[error("NeedSlowPath: {0}")]
NeedSlowPath(String),
#[error("ProgrammingError: {0}")]
Programming(String),
#[error("bug: {0}")]
Bug(String),
#[error(transparent)]
Backend(Box<BackendError>),
#[error("out of space for group {0:?}")]
IdOverflow(Group),
#[error(transparent)]
IntOverflow(#[from] TryFromIntError),
}
#[derive(Debug, Error)]
pub enum BackendError {
#[error("{0}")]
Generic(String),
#[error(transparent)]
Io(#[from] std::io::Error),
#[cfg(any(test, feature = "indexedlog-backend"))]
#[error(transparent)]
IndexedLog(#[from] indexedlog::Error),
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl From<BackendError> for DagError {
fn from(err: BackendError) -> DagError {
DagError::Backend(Box::new(err))
}
}
#[cfg(any(test, feature = "indexedlog-backend"))]
impl From<indexedlog::Error> for DagError {
fn from(err: indexedlog::Error) -> DagError {
DagError::Backend(Box::new(BackendError::from(err)))
}
}
impl From<io::Error> for DagError {
fn from(err: io::Error) -> DagError {
DagError::Backend(Box::new(BackendError::from(err)))
}
}
pub fn bug<T>(message: impl ToString) -> crate::Result<T> {
Err(DagError::Bug(message.to_string()))
}
pub fn programming<T>(message: impl ToString) -> crate::Result<T> {
Err(DagError::Programming(message.to_string()))
}
pub trait NotFoundError {
fn not_found_error(&self) -> DagError;
fn not_found<T>(&self) -> crate::Result<T> {
Err(self.not_found_error())
}
}
impl NotFoundError for Id {
fn not_found_error(&self) -> DagError {
::fail::fail_point!("dag-not-found-id");
DagError::IdNotFound(self.clone())
}
}
impl NotFoundError for Vertex {
fn not_found_error(&self) -> DagError {
::fail::fail_point!("dag-not-found-vertex");
DagError::VertexNotFound(self.clone())
}
}