From 1c3f05483643583fe244d16ee77c2d046b60827c Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Thu, 8 Feb 2024 16:41:41 -0500 Subject: [PATCH] Convert error to thiserror This converts just one `SerializerError` to use `thiserror` crate, removing some code. --- Cargo.lock | 21 +++++++++++++ Cargo.toml | 1 + lib/oxigraph/Cargo.toml | 1 + lib/oxigraph/src/storage/error.rs | 49 +++++-------------------------- 4 files changed, 30 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5245a514..d729862c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1068,6 +1068,7 @@ dependencies = [ "sparesults", "spargebra", "sparopt", + "thiserror", "zstd", ] @@ -1847,6 +1848,26 @@ dependencies = [ "term", ] +[[package]] +name = "thiserror" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.3.34" diff --git a/Cargo.toml b/Cargo.toml index 7cbc711b..1c0cc9e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,6 +60,7 @@ sha1 = "0.10" sha2 = "0.10" siphasher = ">=0.3, <2.0" text-diff = "0.4" +thiserror = "1" time = "0.3" tokio = "1.29" url = "2.4" diff --git a/lib/oxigraph/Cargo.toml b/lib/oxigraph/Cargo.toml index 13f14476..cefb5755 100644 --- a/lib/oxigraph/Cargo.toml +++ b/lib/oxigraph/Cargo.toml @@ -43,6 +43,7 @@ siphasher.workspace = true sparesults = { workspace = true, features = ["rdf-star"] } spargebra = { workspace = true, features = ["rdf-star", "sep-0002", "sep-0006"] } sparopt = { workspace = true, features = ["rdf-star", "sep-0002", "sep-0006"] } +thiserror.workspace = true [target.'cfg(not(target_family = "wasm"))'.dependencies] libc.workspace = true diff --git a/lib/oxigraph/src/storage/error.rs b/lib/oxigraph/src/storage/error.rs index 05076e6e..d0a67522 100644 --- a/lib/oxigraph/src/storage/error.rs +++ b/lib/oxigraph/src/storage/error.rs @@ -2,6 +2,7 @@ use crate::io::{ParseError, RdfFormat}; use oxiri::IriParseError; use std::error::Error; use std::{fmt, io}; +use thiserror::Error; /// An error related to storage operations (reads, writes...). #[derive(Debug)] @@ -185,55 +186,19 @@ impl From for io::Error { } /// An error raised while writing a file from a [`Store`](crate::store::Store). -#[derive(Debug)] +#[derive(Debug, Error)] pub enum SerializerError { /// An error raised while writing the content. - Io(io::Error), + #[error(transparent)] + Io(#[from] io::Error), /// An error raised during the lookup in the store. - Storage(StorageError), + #[error(transparent)] + Storage(#[from] StorageError), /// A format compatible with [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) is required. + #[error("A RDF format supporting datasets was expected, {0} found")] DatasetFormatExpected(RdfFormat), } -impl fmt::Display for SerializerError { - #[inline] - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - Self::Io(e) => e.fmt(f), - Self::Storage(e) => e.fmt(f), - Self::DatasetFormatExpected(format) => write!( - f, - "A RDF format supporting datasets was expected, {format} found" - ), - } - } -} - -impl Error for SerializerError { - #[inline] - fn source(&self) -> Option<&(dyn Error + 'static)> { - match self { - Self::Io(e) => Some(e), - Self::Storage(e) => Some(e), - Self::DatasetFormatExpected(_) => None, - } - } -} - -impl From for SerializerError { - #[inline] - fn from(error: io::Error) -> Self { - Self::Io(error) - } -} - -impl From for SerializerError { - #[inline] - fn from(error: StorageError) -> Self { - Self::Storage(error) - } -} - impl From for io::Error { #[inline] fn from(error: SerializerError) -> Self {