use std::fmt;
use std::io;
use std::path::PathBuf;
use crate::io::SourceLocation;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("invalid RDF value: {0}")]
InvalidRdf(String),
#[error("{0}")]
Parse(#[from] ParseError),
#[error("RDF serialize error: {0}")]
Serialize(String),
#[error("SPARQL parse error: {0}")]
SparqlParse(String),
#[error("SPARQL evaluation error: {0}")]
SparqlEvaluation(String),
#[error("storage error: {0}")]
Storage(String),
#[error("unsupported feature: {0}")]
Unsupported(String),
#[error(transparent)]
Io(#[from] io::Error),
#[error("could not open store at {}: {message}", path.display())]
OpenStore {
path: PathBuf,
message: String,
},
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParseError {
pub message: String,
pub location: Option<SourceLocation>,
}
impl ParseError {
pub(crate) fn new(message: impl Into<String>, location: Option<SourceLocation>) -> Self {
Self {
message: message.into(),
location,
}
}
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "RDF parse error")?;
if let Some(location) = &self.location {
write!(f, "{location}")?;
}
write!(f, ": {}", self.message)
}
}
impl std::error::Error for ParseError {}
impl Error {
pub(crate) fn parse(message: impl Into<String>, location: Option<SourceLocation>) -> Self {
Self::Parse(ParseError::new(message, location))
}
}
pub type Result<T> = std::result::Result<T, Error>;