From 6423ee8d055124e4aacc043a5ccb5d807929433d Mon Sep 17 00:00:00 2001 From: Tpt Date: Sun, 20 May 2018 22:57:58 +0200 Subject: [PATCH] Adds other useful simple constructors --- src/model/data.rs | 88 +++++++++++++---------- src/rio/ntriples/ntriples_grammar.rustpeg | 2 +- src/rio/turtle/turtle_grammar.rustpeg | 6 +- 3 files changed, 53 insertions(+), 43 deletions(-) diff --git a/src/model/data.rs b/src/model/data.rs index 143471f1..af2ee237 100644 --- a/src/model/data.rs +++ b/src/model/data.rs @@ -15,6 +15,13 @@ pub struct NamedNode { } impl NamedNode { + /// Builds a RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) + pub fn new(iri: impl Into) -> Self { + Self { + iri: Arc::new(iri.into()), + } + } + pub fn value(&self) -> &str { self.iri.as_str() } @@ -34,9 +41,7 @@ impl FromStr for NamedNode { type Err = ParseError; fn from_str(s: &str) -> Result { - Ok(NamedNode { - iri: Arc::new(Url::parse(s)?), - }) + Ok(NamedNode::new(Url::parse(s)?)) } } @@ -47,6 +52,11 @@ pub struct BlankNode { } impl BlankNode { + /// Builds a RDF [blank node](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node) with a known id + pub fn new(id: impl Into) -> Self { + Self { id: id.into() } + } + pub fn value(&self) -> &str { &self.id } @@ -149,7 +159,7 @@ impl FromStr for Literal { type Err = (); fn from_str(s: &str) -> Result { - Ok(s.clone().into()) + Ok(s.into()) } } @@ -313,6 +323,21 @@ pub struct Triple { object: Term, } +impl Triple { + /// Builds a RDF [triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) + pub fn new( + subject: impl Into, + predicate: impl Into, + object: impl Into, + ) -> Self { + Self { + subject: subject.into(), + predicate: predicate.into(), + object: object.into(), + } + } +} + impl fmt::Display for Triple { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{} {} {} .", self.subject, self.predicate, self.object) @@ -363,6 +388,23 @@ pub struct Quad { graph_name: Option, } +impl Quad { + /// Builds a RDF [triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) in a [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) + pub fn new( + subject: impl Into, + predicate: impl Into, + object: impl Into, + graph_name: impl Into>, + ) -> Self { + Self { + subject: subject.into(), + predicate: predicate.into(), + object: object.into(), + graph_name: graph_name.into(), + } + } +} + impl fmt::Display for Quad { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.graph_name { @@ -451,19 +493,17 @@ impl Default for DataFactory { impl DataFactory { /// Builds a RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) pub fn named_node(&self, iri: impl Into) -> NamedNode { - NamedNode { - iri: Arc::new(iri.into()), - } + NamedNode::new(iri) } /// Builds a RDF [blank node](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node) with a known id pub fn blank_node(&self, id: impl Into) -> BlankNode { - BlankNode { id: id.into() } + BlankNode::new(id) } /// Builds a new RDF [blank node](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node) with a unique id pub fn new_blank_node(&self) -> BlankNode { - self.blank_node(self.blank_node_id_provider.next().to_string()) + BlankNode::new(self.blank_node_id_provider.next().to_string()) } /// Builds a RDF [simple literal](https://www.w3.org/TR/rdf11-concepts/#dfn-simple-literal) @@ -495,34 +535,4 @@ impl DataFactory { language: language.into(), } } - - /// Builds a RDF [triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) - pub fn triple( - &self, - subject: impl Into, - predicate: impl Into, - object: impl Into, - ) -> Triple { - Triple { - subject: subject.into(), - predicate: predicate.into(), - object: object.into(), - } - } - - /// Builds a RDF [triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) in a [RDF dataset](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-dataset) - pub fn quad( - &self, - subject: impl Into, - predicate: impl Into, - object: impl Into, - graph_name: impl Into>, - ) -> Quad { - Quad { - subject: subject.into(), - predicate: predicate.into(), - object: object.into(), - graph_name: graph_name.into(), - } - } } diff --git a/src/rio/ntriples/ntriples_grammar.rustpeg b/src/rio/ntriples/ntriples_grammar.rustpeg index 1868e97c..931ea9f0 100644 --- a/src/rio/ntriples/ntriples_grammar.rustpeg +++ b/src/rio/ntriples/ntriples_grammar.rustpeg @@ -10,7 +10,7 @@ use model::data::*; //[2] #[pub] triple -> Option = - _ s:subject _ p:predicate _ o:object _ "." _ comment? { Some(data_factory.triple(s, p, o)) } / + _ s:subject _ p:predicate _ o:object _ "." _ comment? { Some(Triple::new(s, p, o)) } / _ comment? { None } //[3] diff --git a/src/rio/turtle/turtle_grammar.rustpeg b/src/rio/turtle/turtle_grammar.rustpeg index 6a327939..a3b3900b 100644 --- a/src/rio/turtle/turtle_grammar.rustpeg +++ b/src/rio/turtle/turtle_grammar.rustpeg @@ -87,7 +87,7 @@ object -> () = o:object_value {? match state.cur_subject.last() { Some(s) => match state.cur_predicate.last() { Some(p) => { - buffer.push(data_factory.triple(s.clone(), p.clone(), o)); + buffer.push(Triple::new(s.clone(), p.clone(), o)); Ok(()) } None => Err("Predicate not found") @@ -121,8 +121,8 @@ collection -> NamedOrBlankNode = '(' _ o:(collection_value*) ')' { let mut current_list_node = NamedOrBlankNode::from(NamedNode::from_str("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil").unwrap()); for obj in o.into_iter().rev() { let new_blank_node = NamedOrBlankNode::from(data_factory.new_blank_node()); - buffer.push(data_factory.triple(new_blank_node.clone(), first.clone(), obj)); - buffer.push(data_factory.triple(new_blank_node.clone(), rest.clone(), current_list_node)); + buffer.push(Triple::new(new_blank_node.clone(), first.clone(), obj)); + buffer.push(Triple::new(new_blank_node.clone(), rest.clone(), current_list_node)); current_list_node = new_blank_node; } current_list_node