From 3d07160167f9459487551e4abb10b9b6003a6e3f Mon Sep 17 00:00:00 2001 From: Tpt Date: Fri, 31 Jul 2020 16:49:07 +0200 Subject: [PATCH] Relaxes error bound from Into to Into --- lib/src/error.rs | 42 ++----------- lib/src/sparql/error.rs | 16 +++-- lib/src/sparql/plan.rs | 13 ++-- lib/src/store/memory.rs | 16 +++-- lib/src/store/mod.rs | 100 ++++++++++++++++++------------- lib/src/store/numeric_encoder.rs | 6 +- lib/src/store/rocksdb.rs | 17 +++--- lib/src/store/sled.rs | 16 +++-- 8 files changed, 111 insertions(+), 115 deletions(-) diff --git a/lib/src/error.rs b/lib/src/error.rs index c364c232..09dede99 100644 --- a/lib/src/error.rs +++ b/lib/src/error.rs @@ -1,17 +1,12 @@ +use std::convert::Infallible; use std::error::Error; -use std::{fmt, io}; +use std::io; -//TODO: convert to "!" when "never_type" is going to be stabilized -#[allow(clippy::empty_enum)] -#[derive(Eq, PartialEq, Debug, Clone, Hash)] -pub(crate) enum Infallible {} - -impl Error for Infallible {} +/// Traits that allows unwrapping only infallible results +pub(crate) trait UnwrapInfallible { + type Value; -impl fmt::Display for Infallible { - fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self {} - } + fn unwrap_infallible(self) -> Self::Value; } impl UnwrapInfallible for Result { @@ -25,31 +20,6 @@ impl UnwrapInfallible for Result { } } -/// Traits that allows unwrapping only infallible results -pub(crate) trait UnwrapInfallible { - type Value; - - fn unwrap_infallible(self) -> Self::Value; -} - -impl From for Infallible { - fn from(error: std::convert::Infallible) -> Self { - match error {} - } -} - -impl From for std::io::Error { - fn from(error: Infallible) -> Self { - match error {} - } -} - -impl From for std::convert::Infallible { - fn from(error: Infallible) -> Self { - match error {} - } -} - pub(crate) fn invalid_data_error(error: impl Into>) -> io::Error { io::Error::new(io::ErrorKind::InvalidData, error) } diff --git a/lib/src/sparql/error.rs b/lib/src/sparql/error.rs index 2e9a84e6..78228204 100644 --- a/lib/src/sparql/error.rs +++ b/lib/src/sparql/error.rs @@ -1,6 +1,7 @@ -use crate::error::Infallible; +use crate::error::invalid_data_error; use crate::sparql::ParseError; use crate::store::numeric_encoder::DecoderError; +use std::convert::Infallible; use std::error; use std::fmt; use std::io; @@ -91,12 +92,6 @@ impl From for EvaluationError { } } -impl From for EvaluationError { - fn from(error: std::convert::Infallible) -> Self { - match error {} - } -} - impl From for EvaluationError { fn from(error: ParseError) -> Self { Self::Parsing(error) @@ -109,8 +104,11 @@ impl From for EvaluationError { } } -impl> From> for EvaluationError { +impl> From> for EvaluationError { fn from(error: DecoderError) -> Self { - io::Error::from(error).into() + match error { + DecoderError::Store(error) => error.into(), + DecoderError::Decoder { msg } => invalid_data_error(msg).into(), + } } } diff --git a/lib/src/sparql/plan.rs b/lib/src/sparql/plan.rs index 9f898c09..48f89be3 100644 --- a/lib/src/sparql/plan.rs +++ b/lib/src/sparql/plan.rs @@ -9,7 +9,6 @@ use crate::store::numeric_encoder::{ use crate::store::ReadableEncodedStore; use std::cell::{RefCell, RefMut}; use std::collections::BTreeSet; -use std::io; use std::rc::Rc; #[derive(Eq, PartialEq, Debug, Clone, Hash)] @@ -584,7 +583,7 @@ impl DatasetView { ) -> Box>> { if graph_name == None { Box::new( - map_io_err( + map_iter_err( self.store .encoded_quads_for_pattern(subject, predicate, object, None), ) @@ -595,7 +594,7 @@ impl DatasetView { ) } else if graph_name == Some(EncodedTerm::DefaultGraph) && self.default_graph_as_union { Box::new( - map_io_err( + map_iter_err( self.store .encoded_quads_for_pattern(subject, predicate, object, None), ) @@ -610,7 +609,7 @@ impl DatasetView { }), ) } else { - Box::new(map_io_err(self.store.encoded_quads_for_pattern( + Box::new(map_iter_err(self.store.encoded_quads_for_pattern( subject, predicate, object, graph_name, ))) } @@ -624,10 +623,10 @@ impl DatasetView { } } -fn map_io_err<'a, T>( - iter: impl Iterator>> + 'a, +fn map_iter_err<'a, T>( + iter: impl Iterator>> + 'a, ) -> impl Iterator> + 'a { - iter.map(|e| e.map_err(|e| e.into().into())) + iter.map(|e| e.map_err(|e| e.into())) } impl WithStoreError for DatasetView { diff --git a/lib/src/store/memory.rs b/lib/src/store/memory.rs index 609ca3e2..ab10e739 100644 --- a/lib/src/store/memory.rs +++ b/lib/src/store/memory.rs @@ -1,6 +1,6 @@ //! In-memory store. -use crate::error::{Infallible, UnwrapInfallible}; +use crate::error::UnwrapInfallible; use crate::io::{DatasetFormat, GraphFormat}; use crate::model::*; use crate::sparql::{EvaluationError, Query, QueryOptions, QueryResult, SimplePreparedQuery}; @@ -10,7 +10,7 @@ use crate::store::{ }; use std::collections::hash_map::DefaultHasher; use std::collections::{HashMap, HashSet}; -use std::convert::TryInto; +use std::convert::{Infallible, TryInto}; use std::hash::{BuildHasherDefault, Hash, Hasher}; use std::io::{BufRead, Write}; use std::iter::FromIterator; @@ -276,7 +276,8 @@ impl MemoryStore { base_iri: Option<&str>, ) -> Result<(), io::Error> { let mut store = self; - load_graph(&mut store, reader, format, to_graph_name, base_iri) + load_graph(&mut store, reader, format, to_graph_name, base_iri)?; + Ok(()) } /// Loads a dataset file (i.e. quads) into the store. @@ -309,7 +310,8 @@ impl MemoryStore { base_iri: Option<&str>, ) -> Result<(), io::Error> { let mut store = self; - load_dataset(&mut store, reader, format, base_iri) + load_dataset(&mut store, reader, format, base_iri)?; + Ok(()) } /// Adds a quad to this store. @@ -977,7 +979,8 @@ impl<'a> MemoryTransaction<'a> { to_graph_name: &GraphName, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_graph(self, reader, format, to_graph_name, base_iri) + load_graph(self, reader, format, to_graph_name, base_iri)?; + Ok(()) } /// Loads a dataset file (i.e. quads) into the store during the transaction. @@ -1006,7 +1009,8 @@ impl<'a> MemoryTransaction<'a> { format: DatasetFormat, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_dataset(self, reader, format, base_iri) + load_dataset(self, reader, format, base_iri)?; + Ok(()) } /// Adds a quad to this store during the transaction. diff --git a/lib/src/store/mod.rs b/lib/src/store/mod.rs index f287c347..fb9872a2 100644 --- a/lib/src/store/mod.rs +++ b/lib/src/store/mod.rs @@ -28,7 +28,7 @@ use rio_turtle::{ }; use rio_xml::{RdfXmlError, RdfXmlFormatter, RdfXmlParser}; use std::collections::HashMap; -use std::error::Error; +use std::convert::Infallible; use std::io; use std::io::{BufRead, Write}; use std::iter::Iterator; @@ -57,7 +57,7 @@ fn load_graph( format: GraphFormat, to_graph_name: &GraphName, base_iri: Option<&str>, -) -> Result<(), io::Error> { +) -> Result<(), StoreOrParseError> { let base_iri = base_iri.unwrap_or(""); match format { GraphFormat::NTriples => { @@ -76,26 +76,30 @@ fn load_from_triple_parser( store: &mut S, parser: Result, to_graph_name: &GraphName, -) -> Result<(), io::Error> +) -> Result<(), StoreOrParseError> where - IoOrParseError: From, + StoreOrParseError: From, P::Error: Send + Sync + 'static, { let mut parser = parser.map_err(invalid_input_error)?; let mut bnode_map = HashMap::default(); let to_graph_name = store .encode_graph_name(to_graph_name) - .map_err(|e| e.into())?; - let result: Result<(), IoOrParseError<_>> = parser.parse_all(&mut move |t| { - let quad = store - .encode_rio_triple_in_graph(t, to_graph_name, &mut bnode_map) - .map_err(|e| IoOrParseError::Io(e.into()))?; - store - .insert_encoded(&quad) - .map_err(|e| IoOrParseError::Io(e.into()))?; - Ok(()) - }); - Ok(result?) + .map_err(StoreOrParseError::Store)?; + parser + .parse_all(&mut move |t| { + let quad = store + .encode_rio_triple_in_graph(t, to_graph_name, &mut bnode_map) + .map_err(StoreOrParseError::Store)?; + store + .insert_encoded(&quad) + .map_err(StoreOrParseError::Store)?; + Ok(()) + }) + .map_err(|e| match e { + StoreOrParseError::Store(e) => StoreOrParseError::Store(e), + StoreOrParseError::Parse(e) => StoreOrParseError::Parse(invalid_data_error(e)), + }) } fn dump_graph( @@ -138,7 +142,7 @@ fn load_dataset( reader: impl BufRead, format: DatasetFormat, base_iri: Option<&str>, -) -> Result<(), io::Error> { +) -> Result<(), StoreOrParseError> { let base_iri = base_iri.unwrap_or(""); match format { DatasetFormat::NQuads => load_from_quad_parser(store, NQuadsParser::new(reader)), @@ -149,23 +153,27 @@ fn load_dataset( fn load_from_quad_parser( store: &mut S, parser: Result, -) -> Result<(), io::Error> +) -> Result<(), StoreOrParseError> where - IoOrParseError: From, + StoreOrParseError: From, P::Error: Send + Sync + 'static, { let mut parser = parser.map_err(invalid_input_error)?; let mut bnode_map = HashMap::default(); - let result: Result<(), IoOrParseError<_>> = parser.parse_all(&mut move |q| { - let quad = store - .encode_rio_quad(q, &mut bnode_map) - .map_err(|e| IoOrParseError::Io(e.into()))?; - store - .insert_encoded(&quad) - .map_err(|e| IoOrParseError::Io(e.into()))?; - Ok(()) - }); - Ok(result?) + parser + .parse_all(&mut move |q| { + let quad = store + .encode_rio_quad(q, &mut bnode_map) + .map_err(StoreOrParseError::Store)?; + store + .insert_encoded(&quad) + .map_err(StoreOrParseError::Store)?; + Ok(()) + }) + .map_err(|e| match e { + StoreOrParseError::Store(e) => StoreOrParseError::Store(e), + StoreOrParseError::Parse(e) => StoreOrParseError::Parse(invalid_data_error(e)), + }) } fn dump_dataset( @@ -192,34 +200,42 @@ fn dump_dataset( Ok(()) } -enum IoOrParseError { - Io(io::Error), - Parse(E), +enum StoreOrParseError { + Store(S), + Parse(P), } -impl From for IoOrParseError { - fn from(error: io::Error) -> Self { - Self::Io(error) +impl From for StoreOrParseError { + fn from(error: TurtleError) -> Self { + Self::Parse(error) } } -impl From for IoOrParseError { - fn from(error: TurtleError) -> Self { +impl From for StoreOrParseError { + fn from(error: RdfXmlError) -> Self { Self::Parse(error) } } -impl From for IoOrParseError { - fn from(error: RdfXmlError) -> Self { +impl From for StoreOrParseError { + fn from(error: io::Error) -> Self { Self::Parse(error) } } -impl From> for io::Error { - fn from(error: IoOrParseError) -> Self { +impl> From> for io::Error { + fn from(error: StoreOrParseError) -> Self { + match error { + StoreOrParseError::Store(error) => error, + StoreOrParseError::Parse(error) => error.into(), + } + } +} +impl> From> for io::Error { + fn from(error: StoreOrParseError) -> Self { match error { - IoOrParseError::Io(error) => error, - IoOrParseError::Parse(error) => invalid_data_error(error), + StoreOrParseError::Store(error) => match error {}, + StoreOrParseError::Parse(error) => error.into(), } } } diff --git a/lib/src/store/numeric_encoder.rs b/lib/src/store/numeric_encoder.rs index 1de7c532..c954e08a 100644 --- a/lib/src/store/numeric_encoder.rs +++ b/lib/src/store/numeric_encoder.rs @@ -1,12 +1,14 @@ #![allow(clippy::unreadable_literal)] -use crate::error::{invalid_data_error, Infallible}; +use crate::error::invalid_data_error; use crate::model::xsd::*; use crate::model::*; +use crate::sparql::EvaluationError; use rand::random; use rio_api::model as rio; use siphasher::sip128::{Hasher128, SipHasher24}; use std::collections::HashMap; +use std::convert::Infallible; use std::error::Error; use std::hash::Hash; use std::hash::Hasher; @@ -763,7 +765,7 @@ pub fn write_term(sink: &mut Vec, term: EncodedTerm) { } pub(crate) trait WithStoreError { - type Error: Error + Into + 'static; + type Error: Error + Into + 'static; } pub(crate) trait StrLookup: WithStoreError { diff --git a/lib/src/store/rocksdb.rs b/lib/src/store/rocksdb.rs index 174f187a..d8dc698a 100644 --- a/lib/src/store/rocksdb.rs +++ b/lib/src/store/rocksdb.rs @@ -1,6 +1,6 @@ //! Store based on the [RocksDB](https://rocksdb.org/) key-value database. -use crate::error::{invalid_data_error, Infallible, UnwrapInfallible}; +use crate::error::{invalid_data_error, UnwrapInfallible}; use crate::io::{DatasetFormat, GraphFormat}; use crate::model::*; use crate::sparql::{EvaluationError, Query, QueryOptions, QueryResult, SimplePreparedQuery}; @@ -9,7 +9,7 @@ use crate::store::{ dump_dataset, dump_graph, load_dataset, load_graph, ReadableEncodedStore, WritableEncodedStore, }; use rocksdb::*; -use std::convert::TryInto; +use std::convert::{Infallible, TryInto}; use std::io; use std::io::{BufRead, Cursor, Write}; use std::mem::{take, transmute}; @@ -576,7 +576,8 @@ impl RocksDbTransaction<'_> { to_graph_name: &GraphName, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_graph(&mut self.inner, reader, syntax, to_graph_name, base_iri) + load_graph(&mut self.inner, reader, syntax, to_graph_name, base_iri)?; + Ok(()) } /// Loads a dataset file (i.e. quads) into the store. into the store during the transaction. @@ -595,7 +596,8 @@ impl RocksDbTransaction<'_> { format: DatasetFormat, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_dataset(&mut self.inner, reader, format, base_iri) + load_dataset(&mut self.inner, reader, format, base_iri)?; + Ok(()) } /// Adds a quad to this store during the transaction. @@ -703,18 +705,19 @@ impl WithStoreError for AutoBatchWriter<'_> { impl StrContainer for AutoBatchWriter<'_> { fn insert_str(&mut self, key: StrHash, value: &str) -> Result<(), io::Error> { - Ok(self.inner.insert_str(key, value)?) + self.inner.insert_str(key, value).unwrap_infallible(); + Ok(()) } } impl WritableEncodedStore for AutoBatchWriter<'_> { fn insert_encoded(&mut self, quad: &EncodedQuad) -> Result<(), io::Error> { - self.inner.insert_encoded(quad)?; + self.inner.insert_encoded(quad).unwrap_infallible(); self.apply_if_big() } fn remove_encoded(&mut self, quad: &EncodedQuad) -> Result<(), io::Error> { - self.inner.remove_encoded(quad)?; + self.inner.remove_encoded(quad).unwrap_infallible(); self.apply_if_big() } } diff --git a/lib/src/store/sled.rs b/lib/src/store/sled.rs index 4787be0f..a91ca7c8 100644 --- a/lib/src/store/sled.rs +++ b/lib/src/store/sled.rs @@ -1,6 +1,6 @@ //! Store based on the [Sled](https://sled.rs/) key-value database. -use crate::error::{invalid_data_error, Infallible, UnwrapInfallible}; +use crate::error::{invalid_data_error, UnwrapInfallible}; use crate::io::{DatasetFormat, GraphFormat}; use crate::model::*; use crate::sparql::{EvaluationError, Query, QueryOptions, QueryResult, SimplePreparedQuery}; @@ -9,7 +9,7 @@ use crate::store::{ dump_dataset, dump_graph, load_dataset, load_graph, ReadableEncodedStore, WritableEncodedStore, }; use sled::{Batch, Config, Iter, Tree}; -use std::convert::TryInto; +use std::convert::{Infallible, TryInto}; use std::io::{BufRead, Cursor, Write}; use std::path::Path; use std::{fmt, io, str}; @@ -184,7 +184,8 @@ impl SledStore { format, to_graph_name, base_iri, - ) + )?; + Ok(()) } /// Loads a dataset file (i.e. quads) into the store. @@ -203,7 +204,8 @@ impl SledStore { format: DatasetFormat, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_dataset(&mut DirectWriter::new(self), reader, format, base_iri) + load_dataset(&mut DirectWriter::new(self), reader, format, base_iri)?; + Ok(()) } /// Adds a quad to this store. @@ -671,7 +673,8 @@ impl SledTransaction<'_> { to_graph_name: &GraphName, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_graph(&mut self.inner, reader, format, to_graph_name, base_iri) + load_graph(&mut self.inner, reader, format, to_graph_name, base_iri)?; + Ok(()) } /// Loads a dataset file (i.e. quads) into the store. into the store during the transaction. @@ -690,7 +693,8 @@ impl SledTransaction<'_> { format: DatasetFormat, base_iri: Option<&str>, ) -> Result<(), io::Error> { - load_dataset(&mut self.inner, reader, format, base_iri) + load_dataset(&mut self.inner, reader, format, base_iri)?; + Ok(()) } /// Adds a quad to this store during the transaction.