(
+ &self,
+ parser: Result,
+ ) -> Result>, io::Error>
+ where
+ P::Error: Send + Sync + 'static,
+ {
+ let parser = parser.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
+ let mut mapper = RioMapper::default();
+ Ok(parser
+ .into_iter(move |q| Ok(mapper.quad(&q)))
+ .map(|e: Result<_, P::Error>| {
+ e.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
+ }))
+ }
+}
+
+#[derive(Default)]
+struct RioMapper {
+ bnode_map: HashMap,
+}
+
+impl<'a> RioMapper {
+ fn named_node(&self, node: rio::NamedNode<'a>) -> NamedNode {
+ NamedNode::new_unchecked(node.iri)
+ }
+
+ fn blank_node(&mut self, node: rio::BlankNode<'a>) -> BlankNode {
+ self.bnode_map
+ .entry(node.id.to_owned())
+ .or_insert_with(BlankNode::default)
+ .clone()
+ }
+
+ fn literal(&self, literal: rio::Literal<'a>) -> Literal {
+ match literal {
+ rio::Literal::Simple { value } => Literal::new_simple_literal(value),
+ rio::Literal::LanguageTaggedString { value, language } => {
+ Literal::new_language_tagged_literal_unchecked(value, language)
+ }
+ rio::Literal::Typed { value, datatype } => {
+ Literal::new_typed_literal(value, self.named_node(datatype))
+ }
+ }
+ }
+
+ fn named_or_blank_node(&mut self, node: rio::NamedOrBlankNode<'a>) -> NamedOrBlankNode {
+ match node {
+ rio::NamedOrBlankNode::NamedNode(node) => self.named_node(node).into(),
+ rio::NamedOrBlankNode::BlankNode(node) => self.blank_node(node).into(),
+ }
+ }
+
+ fn term(&mut self, node: rio::Term<'a>) -> Term {
+ match node {
+ rio::Term::NamedNode(node) => self.named_node(node).into(),
+ rio::Term::BlankNode(node) => self.blank_node(node).into(),
+ rio::Term::Literal(literal) => self.literal(literal).into(),
+ }
+ }
+
+ fn triple(&mut self, triple: &rio::Triple<'a>) -> Triple {
+ Triple {
+ subject: self.named_or_blank_node(triple.subject),
+ predicate: self.named_node(triple.predicate),
+ object: self.term(triple.object),
+ }
+ }
+
+ fn graph_name(&mut self, graph_name: Option>) -> GraphName {
+ match graph_name {
+ Some(rio::NamedOrBlankNode::NamedNode(node)) => self.named_node(node).into(),
+ Some(rio::NamedOrBlankNode::BlankNode(node)) => self.blank_node(node).into(),
+ None => GraphName::DefaultGraph,
+ }
+ }
+
+ fn quad(&mut self, quad: &rio::Quad<'a>) -> Quad {
+ Quad {
+ subject: self.named_or_blank_node(quad.subject),
+ predicate: self.named_node(quad.predicate),
+ object: self.term(quad.object),
+ graph_name: self.graph_name(quad.graph_name),
+ }
+ }
+}
diff --git a/lib/src/sparql/model.rs b/lib/src/sparql/model.rs
index b7e3d7fe..efb35324 100644
--- a/lib/src/sparql/model.rs
+++ b/lib/src/sparql/model.rs
@@ -112,6 +112,14 @@ impl QueryResult {
}
}
+impl, I: Iterator- > + 'static> From
+ for QueryResult
+{
+ fn from(iter: I) -> Self {
+ QueryResult::Graph(Box::new(iter.map(|e| e.map_err(|e| e.into()))))
+ }
+}
+
/// [SPARQL query](https://www.w3.org/TR/sparql11-query/) serialization formats
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
pub enum QueryResultSyntax {