Migrates from RioError to standard error system

pull/10/head
Tpt 6 years ago
parent 4c87cef171
commit 39116bf2c9
  1. 3
      src/errors.rs
  2. 2
      src/model/named_node.rs
  3. 44
      src/rio/mod.rs
  4. 12
      src/rio/ntriples/mod.rs
  5. 10
      src/rio/turtle/mod.rs
  6. 9
      src/sparql/parser.rs
  7. 75
      tests/rdf_test_cases.rs

@ -5,5 +5,8 @@ error_chain! {
RocksDB(::rocksdb::Error);
Utf8(::std::str::Utf8Error);
Io(::std::io::Error);
NTriples(::rio::ntriples::ParseError);
Turtle(::rio::turtle::ParseError);
SparqlParser(::sparql::parser::ParseError);
}
}

@ -1,9 +1,9 @@
use errors::*;
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;
use std::sync::Arc;
use url::Url;
use errors::*;
/// A RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]

@ -1,46 +1,2 @@
use std::error::Error;
use std::fmt;
use std::io;
pub mod ntriples;
pub mod turtle;
pub type RioResult<T> = Result<T, RioError>;
#[derive(Debug)]
pub struct RioError {
error: Box<Error + Send + Sync>,
}
impl RioError {
pub fn new<E>(error: E) -> RioError
where
E: Into<Box<Error + Send + Sync>>,
{
RioError {
error: error.into(),
}
}
}
impl From<io::Error> for RioError {
fn from(error: io::Error) -> Self {
RioError::new(error)
}
}
impl fmt::Display for RioError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.error.fmt(f)
}
}
impl Error for RioError {
fn description(&self) -> &str {
self.error.description()
}
fn cause(&self) -> Option<&Error> {
Some(&*self.error)
}
}

@ -4,13 +4,15 @@ mod grammar {
include!(concat!(env!("OUT_DIR"), "/ntriples_grammar.rs"));
}
use errors::*;
use model::*;
use rio::*;
use std::collections::BTreeMap;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Read;
pub(crate) type ParseError = self::grammar::ParseError;
struct NTriplesIterator<R: Read> {
buffer: String,
reader: BufReader<R>,
@ -18,9 +20,9 @@ struct NTriplesIterator<R: Read> {
}
impl<R: Read> Iterator for NTriplesIterator<R> {
type Item = RioResult<Triple>;
type Item = Result<Triple>;
fn next(&mut self) -> Option<RioResult<Triple>> {
fn next(&mut self) -> Option<Result<Triple>> {
match self.reader.read_line(&mut self.buffer) {
Ok(line_count) => if line_count == 0 {
None
@ -30,7 +32,7 @@ impl<R: Read> Iterator for NTriplesIterator<R> {
match result {
Ok(Some(triple)) => Some(Ok(triple)),
Ok(None) => self.next(),
Err(error) => Some(Err(RioError::new(error))),
Err(error) => Some(Err(error.into())),
}
},
Err(error) => Some(Err(error.into())),
@ -38,7 +40,7 @@ impl<R: Read> Iterator for NTriplesIterator<R> {
}
}
pub fn read_ntriples<'a, R: Read + 'a>(source: R) -> impl Iterator<Item = RioResult<Triple>> {
pub fn read_ntriples<'a, R: Read + 'a>(source: R) -> impl Iterator<Item = Result<Triple>> {
NTriplesIterator {
buffer: String::default(),
reader: BufReader::new(source),

@ -1,10 +1,7 @@
/// Implements https://www.w3.org/TR/turtle/
mod grammar {
include!(concat!(env!("OUT_DIR"), "/turtle_grammar.rs"));
use model::*;
use rio::*;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::io::BufReader;
@ -12,6 +9,8 @@ mod grammar {
use url::ParseOptions;
use url::Url;
include!(concat!(env!("OUT_DIR"), "/turtle_grammar.rs"));
pub struct ParserState {
base_uri: Option<Url>,
namespaces: HashMap<String, String>,
@ -29,7 +28,7 @@ mod grammar {
pub fn read_turtle<'a, R: Read + 'a>(
source: R,
base_uri: impl Into<Option<Url>>,
) -> RioResult<impl Iterator<Item = Triple>> {
) -> super::super::super::errors::Result<impl Iterator<Item = Triple>> {
let mut state = ParserState {
base_uri: base_uri.into(),
namespaces: HashMap::default(),
@ -44,9 +43,10 @@ mod grammar {
match turtleDoc(&string_buffer, &mut state, &mut triple_buffer) {
Ok(_) => Ok(triple_buffer.into_iter()),
Err(error) => Err(RioError::new(error)),
Err(error) => Err(error.into()),
}
}
}
pub(crate) type ParseError = self::grammar::ParseError;
pub use self::grammar::read_turtle;

@ -4,8 +4,6 @@ use std::str::Chars;
mod grammar {
use model::*;
use rio::RioError;
use rio::RioResult;
use sparql::algebra::*;
use sparql::model::*;
use sparql::parser::unescape_unicode_codepoints;
@ -289,7 +287,7 @@ mod grammar {
pub fn read_sparql_query<'a, R: Read + 'a>(
source: R,
base_uri: impl Into<Option<Url>>,
) -> RioResult<Query> {
) -> super::super::super::errors::Result<Query> {
let mut state = ParserState {
base_uri: base_uri.into(),
namespaces: HashMap::default(),
@ -305,12 +303,13 @@ mod grammar {
&mut state,
) {
Ok(query) => Ok(query),
Err(error) => Err(RioError::new(error)),
Err(error) => Err(error.into()),
}
}
}
pub use sparql::parser::grammar::read_sparql_query;
pub(crate) type ParseError = self::grammar::ParseError;
pub use self::grammar::read_sparql_query;
fn needs_unescape_unicode_codepoints(input: &str) -> bool {
let bytes = input.as_bytes();

@ -8,13 +8,12 @@ extern crate url;
use reqwest::Client;
use reqwest::Response;
use rudf::errors::*;
use rudf::model::vocab::rdf;
use rudf::model::vocab::rdfs;
use rudf::model::*;
use rudf::rio::ntriples::read_ntriples;
use rudf::rio::turtle::read_turtle;
use rudf::rio::RioError;
use rudf::rio::RioResult;
use rudf::sparql::algebra::Query;
use rudf::sparql::parser::read_sparql_query;
use rudf::store::isomorphism::GraphIsomorphism;
@ -178,25 +177,25 @@ impl Default for RDFClient {
}
impl RDFClient {
pub fn load_turtle(&self, url: Url) -> RioResult<MemoryGraph> {
pub fn load_turtle(&self, url: Url) -> Result<MemoryGraph> {
Ok(read_turtle(self.get(&url)?, Some(url))?.collect())
}
pub fn load_ntriples(&self, url: Url) -> RioResult<MemoryGraph> {
pub fn load_ntriples(&self, url: Url) -> Result<MemoryGraph> {
read_ntriples(self.get(&url)?).collect()
}
pub fn load_sparql_query(&self, url: Url) -> RioResult<Query> {
pub fn load_sparql_query(&self, url: Url) -> Result<Query> {
read_sparql_query(self.get(&url)?, Some(url))
}
fn get(&self, url: &Url) -> RioResult<Response> {
fn get(&self, url: &Url) -> Result<Response> {
match self.client.get(url.clone()).send() {
Ok(response) => Ok(response),
Err(error) => if error.description() == "message is incomplete" {
self.get(url)
} else {
Err(RioError::new(error))
Err("HTTP request error".into()) //TODO: improve
},
}
}
@ -267,9 +266,9 @@ pub mod mf {
}
impl<'a> Iterator for TestManifest<'a> {
type Item = Result<Test, ManifestError>;
type Item = Result<Test>;
fn next(&mut self) -> Option<Result<Test, ManifestError>> {
fn next(&mut self) -> Option<Result<Test>> {
match self.tests_to_do.pop() {
Some(Term::NamedNode(test_node)) => {
let test_subject = NamedOrBlankNode::from(test_node.clone());
@ -278,9 +277,9 @@ impl<'a> Iterator for TestManifest<'a> {
{
Some(Term::NamedNode(c)) => match c.value().split("#").last() {
Some(k) => k.to_string(),
None => return Some(Err(ManifestError::NoType)),
None => return Some(Err("no type".into())),
},
_ => return Some(Err(ManifestError::NoType)),
_ => return Some(Err("no type".into())),
};
let name = match self.graph
.object_for_subject_predicate(&test_subject, &mf::NAME)
@ -298,14 +297,14 @@ impl<'a> Iterator for TestManifest<'a> {
.object_for_subject_predicate(&test_subject, &*mf::ACTION)
{
Some(Term::NamedNode(n)) => n.url().clone(),
Some(_) => return Some(Err(ManifestError::InvalidAction)),
None => return Some(Err(ManifestError::ActionNotFound)),
Some(_) => return Some(Err("invalid action".into())),
None => return Some(Err("action not found".into())),
};
let result = match self.graph
.object_for_subject_predicate(&test_subject, &*mf::RESULT)
{
Some(Term::NamedNode(n)) => Some(n.url().clone()),
Some(_) => return Some(Err(ManifestError::InvalidResult)),
Some(_) => return Some(Err("invalid result".into())),
None => None,
};
Some(Ok(Test {
@ -317,7 +316,7 @@ impl<'a> Iterator for TestManifest<'a> {
result,
}))
}
Some(_) => Some(Err(ManifestError::InvalidTestsList)),
Some(_) => Some(Err("invalid test list".into())),
None => {
match self.manifests_to_do.pop() {
Some(url) => {
@ -341,7 +340,7 @@ impl<'a> Iterator for TestManifest<'a> {
}),
);
}
Some(_) => return Some(Err(ManifestError::InvalidTestsList)),
Some(_) => return Some(Err("invalid tests list".into())),
None => (),
}
@ -353,7 +352,7 @@ impl<'a> Iterator for TestManifest<'a> {
self.tests_to_do
.extend(self.graph.values_for_list(list.clone().into()));
}
Some(_) => return Some(Err(ManifestError::InvalidTestsList)),
Some(_) => return Some(Err("invalid tests list".into())),
None => (),
}
}
@ -364,45 +363,3 @@ impl<'a> Iterator for TestManifest<'a> {
}
}
}
#[derive(Debug)]
pub enum ManifestError {
NoType,
ActionNotFound,
InvalidAction,
InvalidResult,
InvalidTestsList,
RioError(RioError),
}
impl Error for ManifestError {
fn description(&self) -> &str {
match self {
ManifestError::NoType => "no type found on the test case",
ManifestError::ActionNotFound => "action not found",
ManifestError::InvalidAction => "invalid action",
ManifestError::InvalidResult => "invalid result",
ManifestError::InvalidTestsList => "invalid tests list",
ManifestError::RioError(e) => e.description(),
}
}
fn cause(&self) -> Option<&Error> {
match self {
ManifestError::RioError(e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for ManifestError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl From<RioError> for ManifestError {
fn from(e: RioError) -> Self {
ManifestError::RioError(e)
}
}

Loading…
Cancel
Save