diff --git a/Cargo.lock b/Cargo.lock index d729862c..2eaeff63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1145,6 +1145,7 @@ dependencies = [ "oxrdf", "oxrdfxml", "oxttl", + "thiserror", "tokio", ] @@ -1156,6 +1157,7 @@ dependencies = [ "oxiri", "oxrdf", "quick-xml", + "thiserror", "tokio", ] @@ -1184,6 +1186,7 @@ dependencies = [ "oxilangtag", "oxiri", "oxrdf", + "thiserror", "tokio", ] @@ -1745,6 +1748,7 @@ dependencies = [ "memchr", "oxrdf", "quick-xml", + "thiserror", "tokio", ] diff --git a/lib/oxigraph/src/sparql/error.rs b/lib/oxigraph/src/sparql/error.rs index b3516d8e..35eba714 100644 --- a/lib/oxigraph/src/sparql/error.rs +++ b/lib/oxigraph/src/sparql/error.rs @@ -5,93 +5,54 @@ use crate::sparql::ParseError; use crate::storage::StorageError; use std::convert::Infallible; use std::error::Error; -use std::{fmt, io}; +use std::io; +use thiserror::Error; /// A SPARQL evaluation error. -#[derive(Debug)] +#[derive(Debug, Error)] #[non_exhaustive] pub enum EvaluationError { /// An error in SPARQL parsing. - Parsing(ParseError), + #[error(transparent)] + Parsing(#[from] ParseError), /// An error from the storage. - Storage(StorageError), + #[error(transparent)] + Storage(#[from] StorageError), /// An error while parsing an external RDF file. - GraphParsing(RdfParseError), + #[error(transparent)] + GraphParsing(#[from] RdfParseError), /// An error while parsing an external result file (likely from a federated query). - ResultsParsing(ResultsParseError), + #[error(transparent)] + ResultsParsing(#[from] ResultsParseError), /// An error returned during results serialization. - ResultsSerialization(io::Error), + #[error(transparent)] + ResultsSerialization(#[from] io::Error), /// Error during `SERVICE` evaluation - Service(Box), + #[error("{0}")] + Service(#[source] Box), /// Error when `CREATE` tries to create an already existing graph + #[error("The graph {0} already exists")] GraphAlreadyExists(NamedNode), /// Error when `DROP` or `CLEAR` tries to remove a not existing graph + #[error("The graph {0} does not exist")] GraphDoesNotExist(NamedNode), /// The variable storing the `SERVICE` name is unbound + #[error("The variable encoding the service name is unbound")] UnboundService, /// The given `SERVICE` is not supported + #[error("The service {0} is not supported")] UnsupportedService(NamedNode), /// The given content media type returned from an HTTP response is not supported (`SERVICE` and `LOAD`) + #[error("The content media type {0} is not supported")] UnsupportedContentType(String), /// The `SERVICE` call has not returns solutions + #[error("The service is not returning solutions but a boolean or a graph")] ServiceDoesNotReturnSolutions, /// The results are not a RDF graph + #[error("The query results are not a RDF graph")] NotAGraph, } -impl fmt::Display for EvaluationError { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Parsing(error) => error.fmt(f), - Self::Storage(error) => error.fmt(f), - Self::GraphParsing(error) => error.fmt(f), - Self::ResultsParsing(error) => error.fmt(f), - Self::ResultsSerialization(error) => error.fmt(f), - Self::Service(error) => error.fmt(f), - Self::GraphAlreadyExists(graph) => write!(f, "The graph {graph} already exists"), - Self::GraphDoesNotExist(graph) => write!(f, "The graph {graph} does not exist"), - Self::UnboundService => { - f.write_str("The variable encoding the service name is unbound") - } - Self::UnsupportedService(service) => { - write!(f, "The service {service} is not supported") - } - Self::UnsupportedContentType(content_type) => { - write!(f, "The content media type {content_type} is not supported") - } - Self::ServiceDoesNotReturnSolutions => { - f.write_str("The service is not returning solutions but a boolean or a graph") - } - Self::NotAGraph => f.write_str("The query results are not a RDF graph"), - } - } -} - -impl Error for EvaluationError { - #[inline] - fn source(&self) -> Option<&(dyn Error + 'static)> { - match self { - Self::Parsing(e) => Some(e), - Self::Storage(e) => Some(e), - Self::GraphParsing(e) => Some(e), - Self::ResultsParsing(e) => Some(e), - Self::ResultsSerialization(e) => Some(e), - Self::Service(e) => { - let e = Box::as_ref(e); - Some(e) - } - Self::GraphAlreadyExists(_) - | Self::GraphDoesNotExist(_) - | Self::UnboundService - | Self::UnsupportedService(_) - | Self::UnsupportedContentType(_) - | Self::ServiceDoesNotReturnSolutions - | Self::NotAGraph => None, - } - } -} - impl From for EvaluationError { #[inline] fn from(error: Infallible) -> Self { @@ -99,34 +60,6 @@ impl From for EvaluationError { } } -impl From for EvaluationError { - #[inline] - fn from(error: ParseError) -> Self { - Self::Parsing(error) - } -} - -impl From for EvaluationError { - #[inline] - fn from(error: StorageError) -> Self { - Self::Storage(error) - } -} - -impl From for EvaluationError { - #[inline] - fn from(error: RdfParseError) -> Self { - Self::GraphParsing(error) - } -} - -impl From for EvaluationError { - #[inline] - fn from(error: ResultsParseError) -> Self { - Self::ResultsParsing(error) - } -} - impl From for io::Error { #[inline] fn from(error: EvaluationError) -> Self { diff --git a/lib/oxigraph/src/storage/error.rs b/lib/oxigraph/src/storage/error.rs index d0a67522..72f8b5e2 100644 --- a/lib/oxigraph/src/storage/error.rs +++ b/lib/oxigraph/src/storage/error.rs @@ -5,46 +5,20 @@ use std::{fmt, io}; use thiserror::Error; /// An error related to storage operations (reads, writes...). -#[derive(Debug)] +#[derive(Debug, Error)] #[non_exhaustive] pub enum StorageError { /// Error from the OS I/O layer. - Io(io::Error), + #[error(transparent)] + Io(#[from] io::Error), /// Error related to data corruption. - Corruption(CorruptionError), + #[error(transparent)] + Corruption(#[from] CorruptionError), #[doc(hidden)] + #[error(transparent)] Other(Box), } -impl fmt::Display for StorageError { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Io(e) => e.fmt(f), - Self::Corruption(e) => e.fmt(f), - Self::Other(e) => e.fmt(f), - } - } -} - -impl Error for StorageError { - #[inline] - fn source(&self) -> Option<&(dyn Error + 'static)> { - match self { - Self::Io(e) => Some(e), - Self::Corruption(e) => Some(e), - Self::Other(e) => Some(e.as_ref()), - } - } -} - -impl From for StorageError { - #[inline] - fn from(error: io::Error) -> Self { - Self::Io(error) - } -} - impl From for io::Error { #[inline] fn from(error: StorageError) -> Self { @@ -106,13 +80,6 @@ impl Error for CorruptionError { } } -impl From for StorageError { - #[inline] - fn from(error: CorruptionError) -> Self { - Self::Corruption(error) - } -} - impl From for io::Error { #[inline] fn from(error: CorruptionError) -> Self { @@ -121,57 +88,25 @@ impl From for io::Error { } /// An error raised while loading a file into a [`Store`](crate::store::Store). -#[derive(Debug)] +#[derive(Debug, Error)] pub enum LoaderError { /// An error raised while reading the file. - Parsing(ParseError), + #[error(transparent)] + Parsing(#[from] ParseError), /// An error raised during the insertion in the store. - Storage(StorageError), + #[error(transparent)] + Storage(#[from] StorageError), /// The base IRI is invalid. + #[error("Invalid base IRI '{iri}': {error}")] InvalidBaseIri { /// The IRI itself. iri: String, /// The parsing error. + #[source] error: IriParseError, }, } -impl fmt::Display for LoaderError { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Parsing(e) => e.fmt(f), - Self::Storage(e) => e.fmt(f), - Self::InvalidBaseIri { iri, error } => write!(f, "Invalid base IRI '{iri}': {error}"), - } - } -} - -impl Error for LoaderError { - #[inline] - fn source(&self) -> Option<&(dyn Error + 'static)> { - match self { - Self::Parsing(e) => Some(e), - Self::Storage(e) => Some(e), - Self::InvalidBaseIri { error, .. } => Some(error), - } - } -} - -impl From for LoaderError { - #[inline] - fn from(error: ParseError) -> Self { - Self::Parsing(error) - } -} - -impl From for LoaderError { - #[inline] - fn from(error: StorageError) -> Self { - Self::Storage(error) - } -} - impl From for io::Error { #[inline] fn from(error: LoaderError) -> Self { diff --git a/lib/oxigraph/src/storage/small_string.rs b/lib/oxigraph/src/storage/small_string.rs index fcd9b227..0ffa3f3a 100644 --- a/lib/oxigraph/src/storage/small_string.rs +++ b/lib/oxigraph/src/storage/small_string.rs @@ -1,10 +1,10 @@ use std::borrow::Borrow; use std::cmp::Ordering; -use std::error::Error; use std::hash::{Hash, Hasher}; use std::ops::Deref; use std::str::{FromStr, Utf8Error}; use std::{fmt, str}; +use thiserror::Error; /// A small inline string #[derive(Clone, Copy, Default)] @@ -169,31 +169,10 @@ impl<'a> TryFrom<&'a str> for SmallString { } } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, Error)] pub enum BadSmallStringError { + #[error("small strings could only contain at most 15 characters, found {0}")] TooLong(usize), - BadUtf8(Utf8Error), -} - -impl fmt::Display for BadSmallStringError { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::TooLong(v) => write!( - f, - "small strings could only contain at most 15 characters, found {v}" - ), - Self::BadUtf8(e) => e.fmt(f), - } - } -} - -impl Error for BadSmallStringError { - #[inline] - fn source(&self) -> Option<&(dyn Error + 'static)> { - match self { - Self::TooLong(_) => None, - Self::BadUtf8(e) => Some(e), - } - } + #[error(transparent)] + BadUtf8(#[from] Utf8Error), } diff --git a/lib/oxrdfio/Cargo.toml b/lib/oxrdfio/Cargo.toml index 84e398f1..05069df3 100644 --- a/lib/oxrdfio/Cargo.toml +++ b/lib/oxrdfio/Cargo.toml @@ -22,6 +22,7 @@ rdf-star = ["oxrdf/rdf-star", "oxttl/rdf-star"] oxrdf.workspace = true oxrdfxml.workspace = true oxttl.workspace = true +thiserror.workspace = true tokio = { workspace = true, optional = true, features = ["io-util"] } [dev-dependencies] diff --git a/lib/oxrdfio/src/error.rs b/lib/oxrdfio/src/error.rs index 78f9b998..86534d46 100644 --- a/lib/oxrdfio/src/error.rs +++ b/lib/oxrdfio/src/error.rs @@ -1,14 +1,17 @@ use std::error::Error; use std::ops::Range; use std::{fmt, io}; +use thiserror::Error; /// Error returned during RDF format parsing. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum ParseError { /// I/O error during parsing (file not found...). - Io(io::Error), + #[error(transparent)] + Io(#[from] io::Error), /// An error in the file syntax. - Syntax(SyntaxError), + #[error(transparent)] + Syntax(#[from] SyntaxError), } impl ParseError { @@ -19,26 +22,6 @@ impl ParseError { } } -impl fmt::Display for ParseError { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Io(e) => e.fmt(f), - Self::Syntax(e) => e.fmt(f), - } - } -} - -impl Error for ParseError { - #[inline] - fn source(&self) -> Option<&(dyn Error + 'static)> { - match self { - Self::Io(e) => Some(e), - Self::Syntax(e) => Some(e), - } - } -} - impl From for SyntaxError { #[inline] fn from(error: oxttl::SyntaxError) -> Self { @@ -77,20 +60,6 @@ impl From for ParseError { } } -impl From for ParseError { - #[inline] - fn from(error: io::Error) -> Self { - Self::Io(error) - } -} - -impl From for ParseError { - #[inline] - fn from(error: SyntaxError) -> Self { - Self::Syntax(error) - } -} - impl From for io::Error { #[inline] fn from(error: ParseError) -> Self { diff --git a/lib/oxrdfxml/Cargo.toml b/lib/oxrdfxml/Cargo.toml index 2ed9b248..efc71e62 100644 --- a/lib/oxrdfxml/Cargo.toml +++ b/lib/oxrdfxml/Cargo.toml @@ -22,6 +22,7 @@ oxilangtag.workspace = true oxiri.workspace = true oxrdf.workspace = true quick-xml.workspace = true +thiserror.workspace = true tokio = { workspace = true, optional = true, features = ["io-util"] } [dev-dependencies] diff --git a/lib/oxrdfxml/src/error.rs b/lib/oxrdfxml/src/error.rs index be2e161d..798a2f5d 100644 --- a/lib/oxrdfxml/src/error.rs +++ b/lib/oxrdfxml/src/error.rs @@ -3,48 +3,17 @@ use oxiri::IriParseError; use std::error::Error; use std::sync::Arc; use std::{fmt, io}; +use thiserror::Error; /// Error returned during RDF/XML parsing. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum ParseError { /// I/O error during parsing (file not found...). - Io(io::Error), + #[error(transparent)] + Io(#[from] io::Error), /// An error in the file syntax. - Syntax(SyntaxError), -} - -impl fmt::Display for ParseError { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Io(e) => e.fmt(f), - Self::Syntax(e) => e.fmt(f), - } - } -} - -impl Error for ParseError { - #[inline] - fn source(&self) -> Option<&(dyn Error + 'static)> { - match self { - Self::Io(e) => Some(e), - Self::Syntax(e) => Some(e), - } - } -} - -impl From for ParseError { - #[inline] - fn from(error: io::Error) -> Self { - Self::Io(error) - } -} - -impl From for ParseError { - #[inline] - fn from(error: SyntaxError) -> Self { - Self::Syntax(error) - } + #[error(transparent)] + Syntax(#[from] SyntaxError), } impl From for io::Error { diff --git a/lib/oxttl/Cargo.toml b/lib/oxttl/Cargo.toml index 6f4f48ff..0a4bc3ab 100644 --- a/lib/oxttl/Cargo.toml +++ b/lib/oxttl/Cargo.toml @@ -23,6 +23,7 @@ memchr.workspace = true oxrdf.workspace = true oxiri.workspace = true oxilangtag.workspace = true +thiserror.workspace = true tokio = { workspace = true, optional = true, features = ["io-util"] } [dev-dependencies] diff --git a/lib/oxttl/src/toolkit/error.rs b/lib/oxttl/src/toolkit/error.rs index 2f632352..a90de168 100644 --- a/lib/oxttl/src/toolkit/error.rs +++ b/lib/oxttl/src/toolkit/error.rs @@ -1,6 +1,7 @@ use std::error::Error; use std::ops::Range; use std::{fmt, io}; +use thiserror::Error; /// A position in a text i.e. a `line` number starting from 0, a `column` number starting from 0 (in number of code points) and a global file `offset` starting from 0 (in number of bytes). #[derive(Eq, PartialEq, Debug, Clone, Copy)] @@ -79,46 +80,14 @@ impl From for io::Error { /// A parsing error. /// /// It is the union of [`SyntaxError`] and [`io::Error`]. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum ParseError { /// I/O error during parsing (file not found...). - Io(io::Error), + #[error(transparent)] + Io(#[from] io::Error), /// An error in the file syntax. - Syntax(SyntaxError), -} - -impl fmt::Display for ParseError { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Io(e) => e.fmt(f), - Self::Syntax(e) => e.fmt(f), - } - } -} - -impl Error for ParseError { - #[inline] - fn source(&self) -> Option<&(dyn Error + 'static)> { - Some(match self { - Self::Io(e) => e, - Self::Syntax(e) => e, - }) - } -} - -impl From for ParseError { - #[inline] - fn from(error: SyntaxError) -> Self { - Self::Syntax(error) - } -} - -impl From for ParseError { - #[inline] - fn from(error: io::Error) -> Self { - Self::Io(error) - } + #[error(transparent)] + Syntax(#[from] SyntaxError), } impl From for io::Error { diff --git a/lib/sparesults/Cargo.toml b/lib/sparesults/Cargo.toml index 75c5a0bb..5eae3746 100644 --- a/lib/sparesults/Cargo.toml +++ b/lib/sparesults/Cargo.toml @@ -23,6 +23,7 @@ json-event-parser.workspace = true memchr.workspace = true oxrdf.workspace = true quick-xml.workspace = true +thiserror.workspace = true tokio = { workspace = true, optional = true, features = ["io-util"] } [dev-dependencies] diff --git a/lib/sparesults/src/error.rs b/lib/sparesults/src/error.rs index 8ece28d0..6575a2ea 100644 --- a/lib/sparesults/src/error.rs +++ b/lib/sparesults/src/error.rs @@ -3,48 +3,17 @@ use std::error::Error; use std::ops::Range; use std::sync::Arc; use std::{fmt, io}; +use thiserror::Error; /// Error returned during SPARQL result formats format parsing. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum ParseError { /// I/O error during parsing (file not found...). - Io(io::Error), + #[error(transparent)] + Io(#[from] io::Error), /// An error in the file syntax. - Syntax(SyntaxError), -} - -impl fmt::Display for ParseError { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Io(e) => e.fmt(f), - Self::Syntax(e) => e.fmt(f), - } - } -} - -impl Error for ParseError { - #[inline] - fn source(&self) -> Option<&(dyn Error + 'static)> { - match self { - Self::Io(e) => Some(e), - Self::Syntax(e) => Some(e), - } - } -} - -impl From for ParseError { - #[inline] - fn from(error: io::Error) -> Self { - Self::Io(error) - } -} - -impl From for ParseError { - #[inline] - fn from(error: SyntaxError) -> Self { - Self::Syntax(error) - } + #[error(transparent)] + Syntax(#[from] SyntaxError), } impl From for io::Error {