From 0aa536998e491c87ea9a42c92cea7249a2b9f5b1 Mon Sep 17 00:00:00 2001 From: Tpt Date: Wed, 1 Jul 2020 12:12:48 +0200 Subject: [PATCH] Fixes BlankNode encoding --- lib/src/store/numeric_encoder.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/src/store/numeric_encoder.rs b/lib/src/store/numeric_encoder.rs index 8e484e99..bb2686ad 100644 --- a/lib/src/store/numeric_encoder.rs +++ b/lib/src/store/numeric_encoder.rs @@ -907,9 +907,7 @@ pub trait Encoder { self.encode_rio_named_node(named_node.into()) } - fn encode_blank_node(&self, blank_node: &BlankNode) -> Result { - Ok(blank_node.into()) - } + fn encode_blank_node(&mut self, blank_node: &BlankNode) -> Result; fn encode_literal(&mut self, literal: &Literal) -> Result { self.encode_rio_literal(literal.into()) @@ -1028,6 +1026,17 @@ impl Encoder for S { Ok(EncodedTerm::NamedNode { iri_id }) } + fn encode_blank_node(&mut self, blank_node: &BlankNode) -> Result { + if let Some(id) = blank_node.id() { + Ok(EncodedTerm::InlineBlankNode { id }) + } else { + let id = blank_node.as_str(); + let id_id = StrHash::new(id); + self.insert_str(id_id, id)?; + Ok(EncodedTerm::NamedBlankNode { id_id }) + } + } + fn encode_rio_blank_node( &mut self, blank_node: rio::BlankNode<'_>, @@ -1263,15 +1272,16 @@ fn test_encoding() { NamedNode::new_unchecked("http://bar.com").into(), NamedNode::new_unchecked("http://foo.com").into(), BlankNode::default().into(), - Literal::new_simple_literal("foo").into(), + BlankNode::new_unchecked("foo-bnode").into(), + Literal::new_simple_literal("foo-literal").into(), Literal::from(true).into(), Literal::from(1.2).into(), Literal::from(1).into(), - Literal::from("foo").into(), - Literal::new_language_tagged_literal("foo", "fr") + Literal::from("foo-string").into(), + Literal::new_language_tagged_literal("foo-fr", "fr") .unwrap() .into(), - Literal::new_language_tagged_literal("foo", "FR") + Literal::new_language_tagged_literal("foo-FR", "FR") .unwrap() .into(), Literal::new_typed_literal("-1.32", xsd::DECIMAL.clone()).into(),