use std::fmt;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Syntax(#[from] spargebra::SparqlSyntaxError),
#[error(transparent)]
Parse(#[from] oxrdfio::RdfParseError),
#[error("SQL backend error: {0}")]
Backend(String),
#[error("unsupported: {0}")]
Unsupported(String),
#[error("term hash collision: {0}")]
Collision(String),
#[error("corrupted store: {0}")]
Corrupted(String),
#[error(transparent)]
Evaluation(#[from] spareval::QueryEvaluationError),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("{0}")]
Other(String),
}
impl Error {
pub fn backend(e: impl fmt::Display) -> Self {
let msg = e.to_string();
if msg.contains("oxilite: term hash collision") {
Self::Collision(msg)
} else if msg.contains("graph_does_not_exist") {
Self::Other("the graph does not exist".into())
} else if msg.contains("graph_already_exists") {
Self::Other("the graph already exists".into())
} else if msg.contains("vector_dimensions_mismatch") {
Self::Other(
"a value of the indexed property is not a JSON array with the index's dimensions"
.into(),
)
} else if msg.contains("computed_value_not_storable") {
Self::Unsupported(
"an update template stores a computed non-integer value, which SQL cannot hash"
.into(),
)
} else {
Self::Backend(msg)
}
}
pub fn unsupported(msg: impl Into<String>) -> Self {
Self::Unsupported(msg.into())
}
pub fn corrupted(msg: impl Into<String>) -> Self {
Self::Corrupted(msg.into())
}
pub fn is_unsupported(&self) -> bool {
matches!(self, Self::Unsupported(_))
}
}