Adds other useful simple constructors

pull/10/head
Tpt 7 years ago
parent e13b432ccf
commit 6423ee8d05
  1. 88
      src/model/data.rs
  2. 2
      src/rio/ntriples/ntriples_grammar.rustpeg
  3. 6
      src/rio/turtle/turtle_grammar.rustpeg

@ -15,6 +15,13 @@ pub struct NamedNode {
} }
impl NamedNode { impl NamedNode {
/// Builds a RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
pub fn new(iri: impl Into<Url>) -> Self {
Self {
iri: Arc::new(iri.into()),
}
}
pub fn value(&self) -> &str { pub fn value(&self) -> &str {
self.iri.as_str() self.iri.as_str()
} }
@ -34,9 +41,7 @@ impl FromStr for NamedNode {
type Err = ParseError; type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(NamedNode { Ok(NamedNode::new(Url::parse(s)?))
iri: Arc::new(Url::parse(s)?),
})
} }
} }
@ -47,6 +52,11 @@ pub struct BlankNode {
} }
impl 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<String>) -> Self {
Self { id: id.into() }
}
pub fn value(&self) -> &str { pub fn value(&self) -> &str {
&self.id &self.id
} }
@ -149,7 +159,7 @@ impl FromStr for Literal {
type Err = (); type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(s.clone().into()) Ok(s.into())
} }
} }
@ -313,6 +323,21 @@ pub struct Triple {
object: Term, object: Term,
} }
impl Triple {
/// Builds a RDF [triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple)
pub fn new(
subject: impl Into<NamedOrBlankNode>,
predicate: impl Into<NamedNode>,
object: impl Into<Term>,
) -> Self {
Self {
subject: subject.into(),
predicate: predicate.into(),
object: object.into(),
}
}
}
impl fmt::Display for Triple { impl fmt::Display for Triple {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {} {} .", self.subject, self.predicate, self.object) write!(f, "{} {} {} .", self.subject, self.predicate, self.object)
@ -363,6 +388,23 @@ pub struct Quad {
graph_name: Option<NamedOrBlankNode>, graph_name: Option<NamedOrBlankNode>,
} }
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<NamedOrBlankNode>,
predicate: impl Into<NamedNode>,
object: impl Into<Term>,
graph_name: impl Into<Option<NamedOrBlankNode>>,
) -> Self {
Self {
subject: subject.into(),
predicate: predicate.into(),
object: object.into(),
graph_name: graph_name.into(),
}
}
}
impl fmt::Display for Quad { impl fmt::Display for Quad {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.graph_name { match self.graph_name {
@ -451,19 +493,17 @@ impl Default for DataFactory {
impl DataFactory { impl DataFactory {
/// Builds a RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) /// Builds a RDF [IRI](https://www.w3.org/TR/rdf11-concepts/#dfn-iri)
pub fn named_node(&self, iri: impl Into<Url>) -> NamedNode { pub fn named_node(&self, iri: impl Into<Url>) -> NamedNode {
NamedNode { NamedNode::new(iri)
iri: Arc::new(iri.into()),
}
} }
/// Builds a RDF [blank node](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node) with a known id /// 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<String>) -> BlankNode { pub fn blank_node(&self, id: impl Into<String>) -> 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 /// 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 { 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) /// Builds a RDF [simple literal](https://www.w3.org/TR/rdf11-concepts/#dfn-simple-literal)
@ -495,34 +535,4 @@ impl DataFactory {
language: language.into(), language: language.into(),
} }
} }
/// Builds a RDF [triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple)
pub fn triple(
&self,
subject: impl Into<NamedOrBlankNode>,
predicate: impl Into<NamedNode>,
object: impl Into<Term>,
) -> 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<NamedOrBlankNode>,
predicate: impl Into<NamedNode>,
object: impl Into<Term>,
graph_name: impl Into<Option<NamedOrBlankNode>>,
) -> Quad {
Quad {
subject: subject.into(),
predicate: predicate.into(),
object: object.into(),
graph_name: graph_name.into(),
}
}
} }

@ -10,7 +10,7 @@ use model::data::*;
//[2] //[2]
#[pub] #[pub]
triple -> Option<Triple> = triple -> Option<Triple> =
_ 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 } _ comment? { None }
//[3] //[3]

@ -87,7 +87,7 @@ object -> () = o:object_value {?
match state.cur_subject.last() { match state.cur_subject.last() {
Some(s) => match state.cur_predicate.last() { Some(s) => match state.cur_predicate.last() {
Some(p) => { Some(p) => {
buffer.push(data_factory.triple(s.clone(), p.clone(), o)); buffer.push(Triple::new(s.clone(), p.clone(), o));
Ok(()) Ok(())
} }
None => Err("Predicate not found") 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()); 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() { for obj in o.into_iter().rev() {
let new_blank_node = NamedOrBlankNode::from(data_factory.new_blank_node()); 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(Triple::new(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(), rest.clone(), current_list_node));
current_list_node = new_blank_node; current_list_node = new_blank_node;
} }
current_list_node current_list_node

Loading…
Cancel
Save