diff --git a/src/errors.rs b/src/errors.rs index 24d266ce..bdce774a 100644 --- a/src/errors.rs +++ b/src/errors.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); } } diff --git a/src/model/named_node.rs b/src/model/named_node.rs index faaf0882..88b5ac90 100644 --- a/src/model/named_node.rs +++ b/src/model/named_node.rs @@ -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)] diff --git a/src/rio/mod.rs b/src/rio/mod.rs index 02210524..0969fdfd 100644 --- a/src/rio/mod.rs +++ b/src/rio/mod.rs @@ -1,46 +1,2 @@ -use std::error::Error; -use std::fmt; -use std::io; - pub mod ntriples; pub mod turtle; - -pub type RioResult = Result; - -#[derive(Debug)] -pub struct RioError { - error: Box, -} - -impl RioError { - pub fn new(error: E) -> RioError - where - E: Into>, - { - RioError { - error: error.into(), - } - } -} - -impl From 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) - } -} diff --git a/src/rio/ntriples/mod.rs b/src/rio/ntriples/mod.rs index 75cb1701..7f26164c 100644 --- a/src/rio/ntriples/mod.rs +++ b/src/rio/ntriples/mod.rs @@ -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 { buffer: String, reader: BufReader, @@ -18,9 +20,9 @@ struct NTriplesIterator { } impl Iterator for NTriplesIterator { - type Item = RioResult; + type Item = Result; - fn next(&mut self) -> Option> { + fn next(&mut self) -> Option> { match self.reader.read_line(&mut self.buffer) { Ok(line_count) => if line_count == 0 { None @@ -30,7 +32,7 @@ impl Iterator for NTriplesIterator { 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 Iterator for NTriplesIterator { } } -pub fn read_ntriples<'a, R: Read + 'a>(source: R) -> impl Iterator> { +pub fn read_ntriples<'a, R: Read + 'a>(source: R) -> impl Iterator> { NTriplesIterator { buffer: String::default(), reader: BufReader::new(source), diff --git a/src/rio/turtle/mod.rs b/src/rio/turtle/mod.rs index 9e791765..a4bc6325 100644 --- a/src/rio/turtle/mod.rs +++ b/src/rio/turtle/mod.rs @@ -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, namespaces: HashMap, @@ -29,7 +28,7 @@ mod grammar { pub fn read_turtle<'a, R: Read + 'a>( source: R, base_uri: impl Into>, - ) -> RioResult> { + ) -> super::super::super::errors::Result> { 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; diff --git a/src/sparql/parser.rs b/src/sparql/parser.rs index 831cb90f..7c9510f6 100644 --- a/src/sparql/parser.rs +++ b/src/sparql/parser.rs @@ -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>, - ) -> RioResult { + ) -> super::super::super::errors::Result { 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(); diff --git a/tests/rdf_test_cases.rs b/tests/rdf_test_cases.rs index f761e27f..6d6dfcef 100644 --- a/tests/rdf_test_cases.rs +++ b/tests/rdf_test_cases.rs @@ -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 { + pub fn load_turtle(&self, url: Url) -> Result { Ok(read_turtle(self.get(&url)?, Some(url))?.collect()) } - pub fn load_ntriples(&self, url: Url) -> RioResult { + pub fn load_ntriples(&self, url: Url) -> Result { read_ntriples(self.get(&url)?).collect() } - pub fn load_sparql_query(&self, url: Url) -> RioResult { + pub fn load_sparql_query(&self, url: Url) -> Result { read_sparql_query(self.get(&url)?, Some(url)) } - fn get(&self, url: &Url) -> RioResult { + fn get(&self, url: &Url) -> Result { 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; + type Item = Result; - fn next(&mut self) -> Option> { + fn next(&mut self) -> Option> { 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 for ManifestError { - fn from(e: RioError) -> Self { - ManifestError::RioError(e) - } -}