use crate::expression::ExpressionEvaluationError;
use oxrdf::{NamedNode, Term, Variable};
use spargebra::SparqlSyntaxError;
use std::convert::Infallible;
use std::error::Error;
use std::ops::RangeInclusive;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum QueryEvaluationError {
#[error(transparent)]
Dataset(Box<dyn Error + Send + Sync>),
#[error("{0}")]
Service(#[source] Box<dyn Error + Send + Sync>),
#[error("The SPARQL query does not contains variable {0} in its SELECT projection")]
NotExistingSubstitutedVariable(Variable),
#[error("The SPARQL dataset returned the default graph even if a named graph is expected")]
UnexpectedDefaultGraph,
#[error("The custom function {0} is not supported")]
UnsupportedCustomFunction(NamedNode),
#[error("The custom function {name} requires between {} and {} arguments, but {actual} were given", .expected.start(), .expected.end())]
UnsupportedCustomFunctionArity {
name: NamedNode,
expected: RangeInclusive<usize>,
actual: usize,
},
#[error("The variable encoding the service name is unbound")]
UnboundService,
#[error("{0} is not a valid service name")]
InvalidServiceName(Term),
#[error("The service {0} is not supported")]
UnsupportedService(NamedNode),
#[cfg(feature = "sparql-12")]
#[error("The SPARQL dataset returned a triple term that is not a valid RDF 1.2 term")]
InvalidStorageTripleTerm,
#[error("The SPARQL operation has been cancelled")]
Cancelled,
#[doc(hidden)]
#[error(transparent)]
Unexpected(Box<dyn Error + Send + Sync>),
}
impl From<Infallible> for QueryEvaluationError {
#[inline]
fn from(error: Infallible) -> Self {
match error {}
}
}
#[doc(hidden)]
impl From<SparqlSyntaxError> for QueryEvaluationError {
#[inline]
fn from(error: SparqlSyntaxError) -> Self {
Self::Unexpected(Box::new(error))
}
}
impl From<ExpressionEvaluationError<Self>> for QueryEvaluationError {
#[inline]
fn from(error: ExpressionEvaluationError<Self>) -> Self {
match error {
ExpressionEvaluationError::Context(e) => e,
ExpressionEvaluationError::UnsupportedCustomFunction(name) => {
Self::UnsupportedCustomFunction(name)
}
ExpressionEvaluationError::UnsupportedCustomFunctionArity {
name,
expected,
actual,
} => Self::UnsupportedCustomFunctionArity {
name,
expected,
actual,
},
}
}
}