Avoids using std::io::Error where possible

pull/190/head
Tpt 3 years ago
parent 01a33192eb
commit 72a17c4f72
  1. 10
      lib/src/error.rs
  2. 10
      lib/src/io/read.rs
  3. 11
      lib/src/io/write.rs
  4. 1
      lib/src/lib.rs
  5. 9
      lib/src/sparql/http/dummy.rs
  6. 11
      lib/src/sparql/http/simple.rs
  7. 8
      lib/src/sparql/model.rs
  8. 3
      lib/src/sparql/plan_builder.rs
  9. 4
      lib/src/sparql/update.rs
  10. 75
      lib/src/storage/backend/fallback.rs
  11. 16
      lib/src/storage/backend/rocksdb.rs

@ -1,10 +0,0 @@
use std::error::Error;
use std::io;
pub fn invalid_data_error(error: impl Into<Box<dyn Error + Send + Sync>>) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, error)
}
pub fn invalid_input_error(error: impl Into<Box<dyn Error + Send + Sync>>) -> io::Error {
io::Error::new(io::ErrorKind::InvalidInput, error)
}

@ -388,7 +388,7 @@ pub enum ParserError {
impl ParserError {
pub(crate) fn invalid_base_iri(iri: &str, error: IriParseError) -> Self {
Self::Syntax(SyntaxError {
inner: SyntaxErrorKind::BaseIri {
inner: SyntaxErrorKind::InvalidBaseIri {
iri: iri.to_owned(),
error,
},
@ -492,7 +492,7 @@ pub struct SyntaxError {
pub(crate) enum SyntaxErrorKind {
Turtle(TurtleError),
RdfXml(RdfXmlError),
BaseIri { iri: String, error: IriParseError },
InvalidBaseIri { iri: String, error: IriParseError },
Xml(quick_xml::Error),
Term(TermParseError),
Msg { msg: String },
@ -512,7 +512,7 @@ impl fmt::Display for SyntaxError {
match &self.inner {
SyntaxErrorKind::Turtle(e) => e.fmt(f),
SyntaxErrorKind::RdfXml(e) => e.fmt(f),
SyntaxErrorKind::BaseIri { iri, error } => {
SyntaxErrorKind::InvalidBaseIri { iri, error } => {
write!(f, "Invalid base IRI '{}': {}", iri, error)
}
SyntaxErrorKind::Xml(e) => e.fmt(f),
@ -529,7 +529,7 @@ impl Error for SyntaxError {
SyntaxErrorKind::RdfXml(e) => Some(e),
SyntaxErrorKind::Xml(e) => Some(e),
SyntaxErrorKind::Term(e) => Some(e),
SyntaxErrorKind::BaseIri { .. } | SyntaxErrorKind::Msg { .. } => None,
SyntaxErrorKind::InvalidBaseIri { .. } | SyntaxErrorKind::Msg { .. } => None,
}
}
}
@ -539,7 +539,7 @@ impl From<SyntaxError> for io::Error {
match error.inner {
SyntaxErrorKind::Turtle(error) => error.into(),
SyntaxErrorKind::RdfXml(error) => error.into(),
SyntaxErrorKind::BaseIri { iri, error } => Self::new(
SyntaxErrorKind::InvalidBaseIri { iri, error } => Self::new(
io::ErrorKind::InvalidInput,
format!("Invalid IRI '{}': {}", iri, error),
),

@ -1,6 +1,5 @@
//! Utilities to write RDF graphs and datasets.
use crate::error::invalid_input_error;
use crate::io::{DatasetFormat, GraphFormat};
use crate::model::*;
use rio_api::formatter::TriplesFormatter;
@ -97,7 +96,10 @@ impl<W: Write> TripleWriter<W> {
SubjectRef::NamedNode(node) => rio::NamedNode { iri: node.as_str() }.into(),
SubjectRef::BlankNode(node) => rio::BlankNode { id: node.as_str() }.into(),
SubjectRef::Triple(_) => {
return Err(invalid_input_error("RDF/XML does not support RDF-star yet"))
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"RDF/XML does not support RDF-star yet",
))
}
},
predicate: rio::NamedNode {
@ -127,7 +129,10 @@ impl<W: Write> TripleWriter<W> {
}
.into(),
TermRef::Triple(_) => {
return Err(invalid_input_error("RDF/XML does not support RDF-star yet"))
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"RDF/XML does not support RDF-star yet",
))
}
},
})?,

@ -171,7 +171,6 @@
)]
#![doc(test(attr(deny(warnings))))]
mod error;
pub mod io;
pub mod model;
#[cfg(feature = "sophia")]

@ -1,7 +1,6 @@
//! Simple HTTP client
use crate::error::invalid_input_error;
use std::io::{Empty, Result};
use std::io::{Empty, Error, ErrorKind, Result};
use std::time::Duration;
pub struct Client {}
@ -12,7 +11,8 @@ impl Client {
}
pub fn get(&self, _url: &str, _accept: &str) -> Result<(String, Empty)> {
Err(invalid_input_error(
Err(Error::new(
ErrorKind::Unsupported,
"HTTP client is not available. Enable the feature 'http_client'",
))
}
@ -24,7 +24,8 @@ impl Client {
_content_type: &str,
_accept: &str,
) -> Result<(String, Empty)> {
Err(invalid_input_error(
Err(Error::new(
ErrorKind::Unsupported,
"HTTP client is not available. Enable the feature 'http_client'",
))
}

@ -1,6 +1,5 @@
use crate::error::{invalid_data_error, invalid_input_error};
use oxhttp::model::{Body, HeaderName, Method, Request};
use std::io::Result;
use std::io::{Error, ErrorKind, Result};
use std::time::Duration;
pub struct Client {
@ -57,3 +56,11 @@ impl Client {
Ok((content_type, response.into_body()))
}
}
fn invalid_data_error(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Error {
Error::new(ErrorKind::InvalidData, error)
}
fn invalid_input_error(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Error {
Error::new(ErrorKind::InvalidInput, error)
}

@ -1,4 +1,3 @@
use crate::error::invalid_input_error;
use crate::io::GraphFormat;
use crate::io::GraphSerializer;
use crate::model::*;
@ -123,10 +122,9 @@ impl QueryResults {
writer.finish()?;
Ok(())
} else {
Err(
invalid_input_error("Bindings or booleans could not be formatted as an RDF graph")
.into(),
)
Err(EvaluationError::msg(
"Bindings or booleans could not be formatted as an RDF graph",
))
}
}
}

@ -1,4 +1,3 @@
use crate::error::invalid_data_error;
use crate::model::{LiteralRef, NamedNode as OxNamedNode, NamedNodeRef, Term as OxTerm};
use crate::sparql::dataset::DatasetView;
use crate::sparql::error::EvaluationError;
@ -640,7 +639,7 @@ impl<'a> PlanBuilder<'a> {
self.build_for_expression(&parameters[0], variables, graph_name)?,
)),
Function::Custom(name) => {
let ox_name = OxNamedNode::new(&name.iri).map_err(invalid_data_error)?;
let ox_name = OxNamedNode::new(&name.iri).map_err(EvaluationError::wrap)?;
if self.custom_functions.contains_key(&ox_name) {
PlanExpression::CustomFunction(
ox_name,

@ -1,4 +1,4 @@
use crate::error::invalid_input_error;
use crate::io::read::ParserError;
use crate::io::{GraphFormat, GraphParser};
use crate::model::{
BlankNode as OxBlankNode, GraphName as OxGraphName, GraphNameRef, Literal as OxLiteral,
@ -176,7 +176,7 @@ impl SimpleUpdateEvaluator<'_> {
if let Some(base_iri) = &self.base_iri {
parser = parser
.with_base_iri(base_iri.as_str())
.map_err(invalid_input_error)?;
.map_err(|e| ParserError::invalid_base_iri(base_iri, e))?;
}
for t in parser.read_triples(BufReader::new(body))? {
self.transaction

@ -1,9 +1,9 @@
//! TODO: This storage is dramatically naive.
use crate::error::invalid_input_error;
use crate::storage::StorageError;
use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap};
use std::io::Result;
use std::error::Error;
use std::mem::transmute;
use std::rc::{Rc, Weak};
use std::sync::{Arc, RwLock, RwLockWriteGuard};
@ -18,7 +18,7 @@ pub struct ColumnFamilyDefinition {
pub struct Db(Arc<RwLock<HashMap<ColumnFamily, BTreeMap<Vec<u8>, Vec<u8>>>>>);
impl Db {
pub fn new(column_families: Vec<ColumnFamilyDefinition>) -> Result<Self> {
pub fn new(column_families: Vec<ColumnFamilyDefinition>) -> Result<Self, StorageError> {
let mut trees = HashMap::new();
for cf in column_families {
trees.insert(ColumnFamily(cf.name), BTreeMap::default());
@ -41,10 +41,10 @@ impl Db {
Reader(InnerReader::Simple(self.0.clone()))
}
pub fn transaction<'a, 'b: 'a, T>(
pub fn transaction<'a, 'b: 'a, T, E: Error + 'static + From<StorageError>>(
&'b self,
f: impl Fn(Transaction<'a>) -> Result<T>,
) -> Result<T> {
f: impl Fn(Transaction<'a>) -> Result<T, E>,
) -> Result<T, E> {
f(Transaction(Rc::new(RefCell::new(self.0.write().unwrap()))))
}
}
@ -62,7 +62,11 @@ enum InnerReader {
}
impl Reader {
pub fn get(&self, column_family: &ColumnFamily, key: &[u8]) -> Result<Option<Vec<u8>>> {
pub fn get(
&self,
column_family: &ColumnFamily,
key: &[u8],
) -> Result<Option<Vec<u8>>, StorageError> {
match &self.0 {
InnerReader::Simple(reader) => Ok(reader
.read()
@ -76,13 +80,19 @@ impl Reader {
.get(column_family)
.and_then(|cf| cf.get(key).cloned()))
} else {
Err(invalid_input_error("The transaction is already ended"))
Err(StorageError::Other(
"The transaction is already ended".into(),
))
}
}
}
}
pub fn contains_key(&self, column_family: &ColumnFamily, key: &[u8]) -> Result<bool> {
pub fn contains_key(
&self,
column_family: &ColumnFamily,
key: &[u8],
) -> Result<bool, StorageError> {
match &self.0 {
InnerReader::Simple(reader) => Ok(reader
.read()
@ -96,17 +106,23 @@ impl Reader {
.get(column_family)
.map_or(false, |cf| cf.contains_key(key)))
} else {
Err(invalid_input_error("The transaction is already ended"))
Err(StorageError::Other(
"The transaction is already ended".into(),
))
}
}
}
}
pub fn iter(&self, column_family: &ColumnFamily) -> Result<Iter> {
pub fn iter(&self, column_family: &ColumnFamily) -> Result<Iter, StorageError> {
self.scan_prefix(column_family, &[])
}
pub fn scan_prefix(&self, column_family: &ColumnFamily, prefix: &[u8]) -> Result<Iter> {
pub fn scan_prefix(
&self,
column_family: &ColumnFamily,
prefix: &[u8],
) -> Result<Iter, StorageError> {
let data: Vec<_> = match &self.0 {
InnerReader::Simple(reader) => {
let trees = reader.read().unwrap();
@ -147,7 +163,9 @@ impl Reader {
.collect()
}
} else {
return Err(invalid_input_error("The transaction is already ended"));
return Err(StorageError::Other(
"The transaction is already ended".into(),
));
}
}
};
@ -156,7 +174,7 @@ impl Reader {
Ok(Iter { iter, current })
}
pub fn len(&self, column_family: &ColumnFamily) -> Result<usize> {
pub fn len(&self, column_family: &ColumnFamily) -> Result<usize, StorageError> {
match &self.0 {
InnerReader::Simple(reader) => Ok(reader
.read()
@ -170,13 +188,15 @@ impl Reader {
.get(column_family)
.map_or(0, |tree| tree.len()))
} else {
Err(invalid_input_error("The transaction is already ended"))
Err(StorageError::Other(
"The transaction is already ended".into(),
))
}
}
}
}
pub fn is_empty(&self, column_family: &ColumnFamily) -> Result<bool> {
pub fn is_empty(&self, column_family: &ColumnFamily) -> Result<bool, StorageError> {
match &self.0 {
InnerReader::Simple(reader) => Ok(reader
.read()
@ -190,7 +210,9 @@ impl Reader {
.get(column_family)
.map_or(true, |tree| tree.is_empty()))
} else {
Err(invalid_input_error("The transaction is already ended"))
Err(StorageError::Other(
"The transaction is already ended".into(),
))
}
}
}
@ -214,14 +236,19 @@ impl Transaction<'_> {
&self,
column_family: &ColumnFamily,
key: &[u8],
) -> Result<bool> {
) -> Result<bool, StorageError> {
Ok((*self.0)
.borrow()
.get(column_family)
.map_or(false, |cf| cf.contains_key(key)))
}
pub fn insert(&mut self, column_family: &ColumnFamily, key: &[u8], value: &[u8]) -> Result<()> {
pub fn insert(
&mut self,
column_family: &ColumnFamily,
key: &[u8],
value: &[u8],
) -> Result<(), StorageError> {
self.0
.borrow_mut()
.get_mut(column_family)
@ -230,11 +257,15 @@ impl Transaction<'_> {
Ok(())
}
pub fn insert_empty(&mut self, column_family: &ColumnFamily, key: &[u8]) -> Result<()> {
pub fn insert_empty(
&mut self,
column_family: &ColumnFamily,
key: &[u8],
) -> Result<(), StorageError> {
self.insert(column_family, key, &[])
}
pub fn remove(&mut self, column_family: &ColumnFamily, key: &[u8]) -> Result<()> {
pub fn remove(&mut self, column_family: &ColumnFamily, key: &[u8]) -> Result<(), StorageError> {
self.0
.borrow_mut()
.get_mut(column_family)
@ -262,7 +293,7 @@ impl Iter {
self.current = self.iter.next();
}
pub fn status(&self) -> Result<()> {
pub fn status(&self) -> Result<(), StorageError> {
Ok(())
}
}

@ -4,7 +4,6 @@
#![allow(unsafe_code)]
use crate::error::invalid_input_error;
use crate::storage::error::StorageError;
use crate::store::CorruptionError;
use lazy_static::lazy_static;
@ -976,9 +975,16 @@ struct UnsafeEnv(*mut rocksdb_env_t);
unsafe impl Sync for UnsafeEnv {}
fn path_to_cstring(path: &Path) -> Result<CString, StorageError> {
Ok(CString::new(
path.to_str()
.ok_or_else(|| invalid_input_error("The DB path is not valid UTF-8"))?,
Ok(CString::new(path.to_str().ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
"The DB path is not valid UTF-8",
)
.map_err(|e| invalid_input_error(format!("The DB path contains null bytes: {}", e)))?)
})?)
.map_err(|e| {
io::Error::new(
io::ErrorKind::InvalidInput,
format!("The DB path contains null bytes: {}", e),
)
})?)
}

Loading…
Cancel
Save