@ -1,5 +1,6 @@
//! Data structures for [RDF 1.1 Concepts](https://www.w3.org/TR/rdf11-concepts/) like IRI, literal or triples.
//! Data structures for [RDF 1.1 Concepts](https://www.w3.org/TR/rdf11-concepts/) like IRI, literal or triples.
use std ::convert ::{ TryFrom , TryInto } ;
use std ::fmt ;
use std ::fmt ;
use std ::fmt ::Write ;
use std ::fmt ::Write ;
@ -28,6 +29,18 @@ impl fmt::Display for NamedNode {
}
}
}
}
impl TryFrom < NamedNodePattern > for NamedNode {
type Error = ( ) ;
#[ inline ]
fn try_from ( pattern : NamedNodePattern ) -> Result < Self , ( ) > {
match pattern {
NamedNodePattern ::NamedNode ( t ) = > Ok ( t ) ,
NamedNodePattern ::Variable ( _ ) = > Err ( ( ) ) ,
}
}
}
/// An RDF [blank node](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node).
/// An RDF [blank node](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node).
///
///
///
///
@ -126,14 +139,20 @@ impl fmt::Display for Literal {
pub enum Subject {
pub enum Subject {
NamedNode ( NamedNode ) ,
NamedNode ( NamedNode ) ,
BlankNode ( BlankNode ) ,
BlankNode ( BlankNode ) ,
Triple ( Box < Triple > ) ,
}
}
impl fmt ::Display for Subject {
impl fmt ::Display for Subject {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
match self {
match self {
Subject ::NamedNode ( node ) = > node . fmt ( f ) ,
Self ::NamedNode ( node ) = > node . fmt ( f ) ,
Subject ::BlankNode ( node ) = > node . fmt ( f ) ,
Self ::BlankNode ( node ) = > node . fmt ( f ) ,
Self ::Triple ( triple ) = > write! (
f ,
"<<{} {} {}>>" ,
triple . subject , triple . predicate , triple . object
) ,
}
}
}
}
}
}
@ -152,6 +171,104 @@ impl From<BlankNode> for Subject {
}
}
}
}
impl From < Triple > for Subject {
#[ inline ]
fn from ( triple : Triple ) -> Self {
Self ::Triple ( Box ::new ( triple ) )
}
}
impl From < Box < Triple > > for Subject {
#[ inline ]
fn from ( triple : Box < Triple > ) -> Self {
Self ::Triple ( triple )
}
}
impl TryFrom < TermPattern > for Subject {
type Error = ( ) ;
#[ inline ]
fn try_from ( term : TermPattern ) -> Result < Self , ( ) > {
match term {
TermPattern ::NamedNode ( t ) = > Ok ( t . into ( ) ) ,
TermPattern ::BlankNode ( t ) = > Ok ( t . into ( ) ) ,
TermPattern ::Triple ( t ) = > Ok ( Triple ::try_from ( t ) ? . into ( ) ) ,
TermPattern ::Literal ( _ ) | TermPattern ::Variable ( _ ) = > Err ( ( ) ) ,
}
}
}
/// The union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) and [triples](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple).
///
/// The default string formatter is returning an N-Triples, Turtle and SPARQL compatible representation.
#[ derive(Eq, PartialEq, Debug, Clone, Hash) ]
pub enum GroundSubject {
NamedNode ( NamedNode ) ,
Triple ( Box < GroundTriple > ) ,
}
impl fmt ::Display for GroundSubject {
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
match self {
Self ::NamedNode ( node ) = > node . fmt ( f ) ,
Self ::Triple ( triple ) = > write! (
f ,
"<<{} {} {}>>" ,
triple . subject , triple . predicate , triple . object
) ,
}
}
}
impl From < NamedNode > for GroundSubject {
#[ inline ]
fn from ( node : NamedNode ) -> Self {
Self ::NamedNode ( node )
}
}
impl From < GroundTriple > for GroundSubject {
#[ inline ]
fn from ( triple : GroundTriple ) -> Self {
Self ::Triple ( Box ::new ( triple ) )
}
}
impl From < Box < GroundTriple > > for GroundSubject {
#[ inline ]
fn from ( triple : Box < GroundTriple > ) -> Self {
Self ::Triple ( triple )
}
}
impl TryFrom < Subject > for GroundSubject {
type Error = ( ) ;
#[ inline ]
fn try_from ( subject : Subject ) -> Result < Self , ( ) > {
match subject {
Subject ::NamedNode ( t ) = > Ok ( t . into ( ) ) ,
Subject ::BlankNode ( _ ) = > Err ( ( ) ) ,
Subject ::Triple ( t ) = > Ok ( GroundTriple ::try_from ( t ) ? . into ( ) ) ,
}
}
}
impl TryFrom < GroundTerm > for GroundSubject {
type Error = ( ) ;
#[ inline ]
fn try_from ( term : GroundTerm ) -> Result < Self , ( ) > {
match term {
GroundTerm ::NamedNode ( t ) = > Ok ( t . into ( ) ) ,
GroundTerm ::Literal ( _ ) = > Err ( ( ) ) ,
GroundTerm ::Triple ( t ) = > Ok ( t . into ( ) ) ,
}
}
}
/// An RDF [term](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-term).
/// An RDF [term](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-term).
///
///
/// It is the union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node) and [literals](https://www.w3.org/TR/rdf11-concepts/#dfn-literal).
/// It is the union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [blank nodes](https://www.w3.org/TR/rdf11-concepts/#dfn-blank-node) and [literals](https://www.w3.org/TR/rdf11-concepts/#dfn-literal).
@ -162,15 +279,21 @@ pub enum Term {
NamedNode ( NamedNode ) ,
NamedNode ( NamedNode ) ,
BlankNode ( BlankNode ) ,
BlankNode ( BlankNode ) ,
Literal ( Literal ) ,
Literal ( Literal ) ,
Triple ( Box < Triple > ) ,
}
}
impl fmt ::Display for Term {
impl fmt ::Display for Term {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
match self {
match self {
Term ::NamedNode ( node ) = > node . fmt ( f ) ,
Self ::NamedNode ( node ) = > node . fmt ( f ) ,
Term ::BlankNode ( node ) = > node . fmt ( f ) ,
Self ::BlankNode ( node ) = > node . fmt ( f ) ,
Term ::Literal ( literal ) = > literal . fmt ( f ) ,
Self ::Literal ( literal ) = > literal . fmt ( f ) ,
Self ::Triple ( triple ) = > write! (
f ,
"<<{} {} {}>>" ,
triple . subject , triple . predicate , triple . object
) ,
}
}
}
}
}
}
@ -196,31 +319,67 @@ impl From<Literal> for Term {
}
}
}
}
impl From < Triple > for Term {
#[ inline ]
fn from ( triple : Triple ) -> Self {
Self ::Triple ( Box ::new ( triple ) )
}
}
impl From < Box < Triple > > for Term {
#[ inline ]
fn from ( triple : Box < Triple > ) -> Self {
Self ::Triple ( triple )
}
}
impl From < Subject > for Term {
impl From < Subject > for Term {
#[ inline ]
#[ inline ]
fn from ( resource : Subject ) -> Self {
fn from ( resource : Subject ) -> Self {
match resource {
match resource {
Subject ::NamedNode ( node ) = > Self ::NamedNode ( node ) ,
Subject ::NamedNode ( node ) = > node . into ( ) ,
Subject ::BlankNode ( node ) = > Self ::BlankNode ( node ) ,
Subject ::BlankNode ( node ) = > node . into ( ) ,
Subject ::Triple ( t ) = > t . into ( ) ,
}
}
}
}
}
}
/// The union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) and [literals](https://www.w3.org/TR/rdf11-concepts/#dfn-literal).
impl TryFrom < TermPattern > for Term {
type Error = ( ) ;
#[ inline ]
fn try_from ( pattern : TermPattern ) -> Result < Self , ( ) > {
match pattern {
TermPattern ::NamedNode ( t ) = > Ok ( t . into ( ) ) ,
TermPattern ::BlankNode ( t ) = > Ok ( t . into ( ) ) ,
TermPattern ::Literal ( t ) = > Ok ( t . into ( ) ) ,
TermPattern ::Triple ( t ) = > Ok ( Triple ::try_from ( t ) ? . into ( ) ) ,
TermPattern ::Variable ( _ ) = > Err ( ( ) ) ,
}
}
}
/// The union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [literals](https://www.w3.org/TR/rdf11-concepts/#dfn-literal) and [triples](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple).
///
///
/// The default string formatter is returning an N-Triples, Turtle and SPARQL compatible representation.
/// The default string formatter is returning an N-Triples, Turtle and SPARQL compatible representation.
#[ derive(Eq, PartialEq, Debug, Clone, Hash) ]
#[ derive(Eq, PartialEq, Debug, Clone, Hash) ]
pub enum GroundTerm {
pub enum GroundTerm {
NamedNode ( NamedNode ) ,
NamedNode ( NamedNode ) ,
Literal ( Literal ) ,
Literal ( Literal ) ,
Triple ( Box < GroundTriple > ) ,
}
}
impl fmt ::Display for GroundTerm {
impl fmt ::Display for GroundTerm {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
match self {
match self {
GroundTerm ::NamedNode ( node ) = > node . fmt ( f ) ,
Self ::NamedNode ( node ) = > node . fmt ( f ) ,
GroundTerm ::Literal ( literal ) = > literal . fmt ( f ) ,
Self ::Literal ( literal ) = > literal . fmt ( f ) ,
Self ::Triple ( triple ) = > write! (
f ,
"<<{} {} {}>>" ,
triple . subject , triple . predicate , triple . object
) ,
}
}
}
}
}
}
@ -239,6 +398,148 @@ impl From<Literal> for GroundTerm {
}
}
}
}
impl From < GroundTriple > for GroundTerm {
#[ inline ]
fn from ( triple : GroundTriple ) -> Self {
Self ::Triple ( Box ::new ( triple ) )
}
}
impl From < Box < GroundTriple > > for GroundTerm {
#[ inline ]
fn from ( triple : Box < GroundTriple > ) -> Self {
Self ::Triple ( triple )
}
}
impl TryFrom < Term > for GroundTerm {
type Error = ( ) ;
#[ inline ]
fn try_from ( term : Term ) -> Result < Self , ( ) > {
match term {
Term ::NamedNode ( t ) = > Ok ( t . into ( ) ) ,
Term ::BlankNode ( _ ) = > Err ( ( ) ) ,
Term ::Literal ( t ) = > Ok ( t . into ( ) ) ,
Term ::Triple ( t ) = > Ok ( GroundTriple ::try_from ( t ) ? . into ( ) ) ,
}
}
}
/// A [RDF triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple).
///
/// The default string formatter is returning a N-Quads representation.
///
/// ```
/// use spargebra::term::NamedNode;
/// use spargebra::term::Triple;
///
/// assert_eq!(
/// "<http://example.com/foo> <http://schema.org/sameAs> <http://example.com/foo>",
/// Triple {
/// subject: NamedNode { iri: "http://example.com/foo".into() }.into(),
/// predicate: NamedNode { iri: "http://schema.org/sameAs".into() },
/// object: NamedNode { iri: "http://example.com/foo".into() }.into(),
/// }.to_string()
/// )
/// ```
#[ derive(Eq, PartialEq, Debug, Clone, Hash) ]
pub struct Triple {
pub subject : Subject ,
pub predicate : NamedNode ,
pub object : Term ,
}
impl fmt ::Display for Triple {
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
write! ( f , "{} {} {}" , self . subject , self . predicate , self . object )
}
}
impl TryFrom < TriplePattern > for Triple {
type Error = ( ) ;
#[ inline ]
fn try_from ( triple : TriplePattern ) -> Result < Self , ( ) > {
Ok ( Self {
subject : triple . subject . try_into ( ) ? ,
predicate : triple . predicate . try_into ( ) ? ,
object : triple . object . try_into ( ) ? ,
} )
}
}
impl TryFrom < Box < TriplePattern > > for Triple {
type Error = ( ) ;
#[ inline ]
fn try_from ( triple : Box < TriplePattern > ) -> Result < Self , ( ) > {
Ok ( Self {
subject : triple . subject . try_into ( ) ? ,
predicate : triple . predicate . try_into ( ) ? ,
object : triple . object . try_into ( ) ? ,
} )
}
}
/// A [RDF triple](https://www.w3.org/TR/rdf11-concepts/#dfn-rdf-triple) without blank nodes.
///
/// The default string formatter is returning a N-Quads representation.
///
/// ```
/// use spargebra::term::NamedNode;
/// use spargebra::term::GroundTriple;
///
/// assert_eq!(
/// "<http://example.com/foo> <http://schema.org/sameAs> <http://example.com/foo>",
/// GroundTriple {
/// subject: NamedNode { iri: "http://example.com/foo".into() }.into(),
/// predicate: NamedNode { iri: "http://schema.org/sameAs".into() },
/// object: NamedNode { iri: "http://example.com/foo".into() }.into(),
/// }.to_string()
/// )
/// ```
#[ derive(Eq, PartialEq, Debug, Clone, Hash) ]
pub struct GroundTriple {
pub subject : GroundSubject ,
pub predicate : NamedNode ,
pub object : GroundTerm ,
}
impl fmt ::Display for GroundTriple {
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
write! ( f , "{} {} {}" , self . subject , self . predicate , self . object )
}
}
impl TryFrom < Triple > for GroundTriple {
type Error = ( ) ;
#[ inline ]
fn try_from ( triple : Triple ) -> Result < Self , ( ) > {
Ok ( Self {
subject : triple . subject . try_into ( ) ? ,
predicate : triple . predicate ,
object : triple . object . try_into ( ) ? ,
} )
}
}
impl TryFrom < Box < Triple > > for GroundTriple {
type Error = ( ) ;
#[ inline ]
fn try_from ( triple : Box < Triple > ) -> Result < Self , ( ) > {
Ok ( Self {
subject : triple . subject . try_into ( ) ? ,
predicate : triple . predicate ,
object : triple . object . try_into ( ) ? ,
} )
}
}
/// A possible graph name.
/// A possible graph name.
///
///
/// It is the union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) and the [default graph name](https://www.w3.org/TR/rdf11-concepts/#dfn-default-graph).
/// It is the union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri) and the [default graph name](https://www.w3.org/TR/rdf11-concepts/#dfn-default-graph).
@ -252,8 +553,8 @@ impl fmt::Display for GraphName {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
match self {
match self {
GraphName ::NamedNode ( node ) = > node . fmt ( f ) ,
Self ::NamedNode ( node ) = > node . fmt ( f ) ,
GraphName ::DefaultGraph = > write! ( f , "DEFAULT" ) ,
Self ::DefaultGraph = > write! ( f , "DEFAULT" ) ,
}
}
}
}
}
}
@ -265,6 +566,19 @@ impl From<NamedNode> for GraphName {
}
}
}
}
impl TryFrom < GraphNamePattern > for GraphName {
type Error = ( ) ;
#[ inline ]
fn try_from ( pattern : GraphNamePattern ) -> Result < Self , ( ) > {
match pattern {
GraphNamePattern ::NamedNode ( t ) = > Ok ( t . into ( ) ) ,
GraphNamePattern ::DefaultGraph = > Ok ( GraphName ::DefaultGraph ) ,
GraphNamePattern ::Variable ( _ ) = > Err ( ( ) ) ,
}
}
}
/// 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).
/// 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).
///
///
/// The default string formatter is returning a N-Quads representation.
/// The default string formatter is returning a N-Quads representation.
@ -274,7 +588,7 @@ impl From<NamedNode> for GraphName {
/// use spargebra::term::Quad;
/// use spargebra::term::Quad;
///
///
/// assert_eq!(
/// assert_eq!(
/// "<http://example.com/foo> <http://schema.org/sameAs> <http://example.com/foo> <http://example.com/> . ",
/// "<http://example.com/foo> <http://schema.org/sameAs> <http://example.com/foo> <http://example.com/>",
/// Quad {
/// Quad {
/// subject: NamedNode { iri: "http://example.com/foo".into() }.into(),
/// subject: NamedNode { iri: "http://example.com/foo".into() }.into(),
/// predicate: NamedNode { iri: "http://schema.org/sameAs".into() },
/// predicate: NamedNode { iri: "http://schema.org/sameAs".into() },
@ -295,17 +609,31 @@ impl fmt::Display for Quad {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
if self . graph_name = = GraphName ::DefaultGraph {
if self . graph_name = = GraphName ::DefaultGraph {
write! ( f , "{} {} {} . " , self . subject , self . predicate , self . object )
write! ( f , "{} {} {}" , self . subject , self . predicate , self . object )
} else {
} else {
write! (
write! (
f ,
f ,
"{} {} {} {} . " ,
"{} {} {} {}" ,
self . subject , self . predicate , self . object , self . graph_name
self . subject , self . predicate , self . object , self . graph_name
)
)
}
}
}
}
}
}
impl TryFrom < QuadPattern > for Quad {
type Error = ( ) ;
#[ inline ]
fn try_from ( quad : QuadPattern ) -> Result < Self , ( ) > {
Ok ( Self {
subject : quad . subject . try_into ( ) ? ,
predicate : quad . predicate . try_into ( ) ? ,
object : quad . object . try_into ( ) ? ,
graph_name : quad . graph_name . try_into ( ) ? ,
} )
}
}
/// 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) without blank nodes.
/// 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) without blank nodes.
///
///
/// The default string formatter is returning a N-Quads representation.
/// The default string formatter is returning a N-Quads representation.
@ -315,7 +643,7 @@ impl fmt::Display for Quad {
/// use spargebra::term::GroundQuad;
/// use spargebra::term::GroundQuad;
///
///
/// assert_eq!(
/// assert_eq!(
/// "<http://example.com/foo> <http://schema.org/sameAs> <http://example.com/foo> <http://example.com/> . ",
/// "<http://example.com/foo> <http://schema.org/sameAs> <http://example.com/foo> <http://example.com/>",
/// GroundQuad {
/// GroundQuad {
/// subject: NamedNode { iri: "http://example.com/foo".into() }.into(),
/// subject: NamedNode { iri: "http://example.com/foo".into() }.into(),
/// predicate: NamedNode { iri: "http://schema.org/sameAs".into() },
/// predicate: NamedNode { iri: "http://schema.org/sameAs".into() },
@ -326,7 +654,7 @@ impl fmt::Display for Quad {
/// ```
/// ```
#[ derive(Eq, PartialEq, Debug, Clone, Hash) ]
#[ derive(Eq, PartialEq, Debug, Clone, Hash) ]
pub struct GroundQuad {
pub struct GroundQuad {
pub subject : NamedNode ,
pub subject : GroundSubject ,
pub predicate : NamedNode ,
pub predicate : NamedNode ,
pub object : GroundTerm ,
pub object : GroundTerm ,
pub graph_name : GraphName ,
pub graph_name : GraphName ,
@ -336,17 +664,31 @@ impl fmt::Display for GroundQuad {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
if self . graph_name = = GraphName ::DefaultGraph {
if self . graph_name = = GraphName ::DefaultGraph {
write! ( f , "{} {} {} . " , self . subject , self . predicate , self . object )
write! ( f , "{} {} {}" , self . subject , self . predicate , self . object )
} else {
} else {
write! (
write! (
f ,
f ,
"{} {} {} {} . " ,
"{} {} {} {}" ,
self . subject , self . predicate , self . object , self . graph_name
self . subject , self . predicate , self . object , self . graph_name
)
)
}
}
}
}
}
}
impl TryFrom < Quad > for GroundQuad {
type Error = ( ) ;
#[ inline ]
fn try_from ( quad : Quad ) -> Result < Self , ( ) > {
Ok ( Self {
subject : quad . subject . try_into ( ) ? ,
predicate : quad . predicate ,
object : quad . object . try_into ( ) ? ,
graph_name : quad . graph_name ,
} )
}
}
/// A [SPARQL query variable](https://www.w3.org/TR/sparql11-query/#sparqlQueryVariables).
/// A [SPARQL query variable](https://www.w3.org/TR/sparql11-query/#sparqlQueryVariables).
///
///
/// ```
/// ```
@ -380,8 +722,8 @@ impl fmt::Display for NamedNodePattern {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
match self {
match self {
NamedNodePattern ::NamedNode ( node ) = > node . fmt ( f ) ,
Self ::NamedNode ( node ) = > node . fmt ( f ) ,
NamedNodePattern ::Variable ( var ) = > var . fmt ( f ) ,
Self ::Variable ( var ) = > var . fmt ( f ) ,
}
}
}
}
}
}
@ -406,6 +748,7 @@ pub enum TermPattern {
NamedNode ( NamedNode ) ,
NamedNode ( NamedNode ) ,
BlankNode ( BlankNode ) ,
BlankNode ( BlankNode ) ,
Literal ( Literal ) ,
Literal ( Literal ) ,
Triple ( Box < TriplePattern > ) ,
Variable ( Variable ) ,
Variable ( Variable ) ,
}
}
@ -413,10 +756,11 @@ impl fmt::Display for TermPattern {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
match self {
match self {
TermPattern ::NamedNode ( term ) = > term . fmt ( f ) ,
Self ::NamedNode ( term ) = > term . fmt ( f ) ,
TermPattern ::BlankNode ( term ) = > term . fmt ( f ) ,
Self ::BlankNode ( term ) = > term . fmt ( f ) ,
TermPattern ::Literal ( term ) = > term . fmt ( f ) ,
Self ::Literal ( term ) = > term . fmt ( f ) ,
TermPattern ::Variable ( var ) = > var . fmt ( f ) ,
Self ::Triple ( triple ) = > write! ( f , "<<{}>>" , triple ) ,
Self ::Variable ( var ) = > var . fmt ( f ) ,
}
}
}
}
}
}
@ -442,19 +786,45 @@ impl From<Literal> for TermPattern {
}
}
}
}
impl From < TriplePattern > for TermPattern {
#[ inline ]
fn from ( triple : TriplePattern ) -> Self {
Self ::Triple ( Box ::new ( triple ) )
}
}
impl From < Box < TriplePattern > > for TermPattern {
#[ inline ]
fn from ( triple : Box < TriplePattern > ) -> Self {
Self ::Triple ( triple )
}
}
impl From < Variable > for TermPattern {
impl From < Variable > for TermPattern {
fn from ( var : Variable ) -> Self {
fn from ( var : Variable ) -> Self {
Self ::Variable ( var )
Self ::Variable ( var )
}
}
}
}
impl From < Subject > for TermPattern {
#[ inline ]
fn from ( subject : Subject ) -> Self {
match subject {
Subject ::NamedNode ( node ) = > node . into ( ) ,
Subject ::BlankNode ( node ) = > node . into ( ) ,
Subject ::Triple ( t ) = > TriplePattern ::from ( t ) . into ( ) ,
}
}
}
impl From < Term > for TermPattern {
impl From < Term > for TermPattern {
#[ inline ]
#[ inline ]
fn from ( term : Term ) -> Self {
fn from ( term : Term ) -> Self {
match term {
match term {
Term ::NamedNode ( node ) = > Self ::NamedNode ( node ) ,
Term ::NamedNode ( node ) = > node . into ( ) ,
Term ::BlankNode ( node ) = > Self ::BlankNode ( node ) ,
Term ::BlankNode ( node ) = > node . into ( ) ,
Term ::Literal ( literal ) = > Self ::Literal ( literal ) ,
Term ::Literal ( literal ) = > literal . into ( ) ,
Term ::Triple ( t ) = > TriplePattern ::from ( t ) . into ( ) ,
}
}
}
}
}
}
@ -463,8 +833,8 @@ impl From<NamedNodePattern> for TermPattern {
#[ inline ]
#[ inline ]
fn from ( element : NamedNodePattern ) -> Self {
fn from ( element : NamedNodePattern ) -> Self {
match element {
match element {
NamedNodePattern ::NamedNode ( node ) = > Self ::NamedNode ( node ) ,
NamedNodePattern ::NamedNode ( node ) = > node . into ( ) ,
NamedNodePattern ::Variable ( var ) = > Self ::Variable ( var ) ,
NamedNodePattern ::Variable ( var ) = > var . into ( ) ,
}
}
}
}
}
}
@ -475,15 +845,17 @@ pub enum GroundTermPattern {
NamedNode ( NamedNode ) ,
NamedNode ( NamedNode ) ,
Literal ( Literal ) ,
Literal ( Literal ) ,
Variable ( Variable ) ,
Variable ( Variable ) ,
Triple ( Box < GroundTriplePattern > ) ,
}
}
impl fmt ::Display for GroundTermPattern {
impl fmt ::Display for GroundTermPattern {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
match self {
match self {
GroundTermPattern ::NamedNode ( term ) = > term . fmt ( f ) ,
Self ::NamedNode ( term ) = > term . fmt ( f ) ,
GroundTermPattern ::Literal ( term ) = > term . fmt ( f ) ,
Self ::Literal ( term ) = > term . fmt ( f ) ,
GroundTermPattern ::Variable ( var ) = > var . fmt ( f ) ,
Self ::Variable ( var ) = > var . fmt ( f ) ,
Self ::Triple ( triple ) = > write! ( f , "<<{}>>" , triple ) ,
}
}
}
}
}
}
@ -502,6 +874,20 @@ impl From<Literal> for GroundTermPattern {
}
}
}
}
impl From < GroundTriplePattern > for GroundTermPattern {
#[ inline ]
fn from ( triple : GroundTriplePattern ) -> Self {
Self ::Triple ( Box ::new ( triple ) )
}
}
impl From < Box < GroundTriplePattern > > for GroundTermPattern {
#[ inline ]
fn from ( triple : Box < GroundTriplePattern > ) -> Self {
Self ::Triple ( triple )
}
}
impl From < Variable > for GroundTermPattern {
impl From < Variable > for GroundTermPattern {
#[ inline ]
#[ inline ]
fn from ( var : Variable ) -> Self {
fn from ( var : Variable ) -> Self {
@ -509,12 +895,22 @@ impl From<Variable> for GroundTermPattern {
}
}
}
}
impl From < GroundSubject > for GroundTermPattern {
#[ inline ]
fn from ( term : GroundSubject ) -> Self {
match term {
GroundSubject ::NamedNode ( node ) = > node . into ( ) ,
GroundSubject ::Triple ( triple ) = > GroundTriplePattern ::from ( triple ) . into ( ) ,
}
}
}
impl From < GroundTerm > for GroundTermPattern {
impl From < GroundTerm > for GroundTermPattern {
#[ inline ]
#[ inline ]
fn from ( term : GroundTerm ) -> Self {
fn from ( term : GroundTerm ) -> Self {
match term {
match term {
GroundTerm ::NamedNode ( node ) = > Self ::NamedNode ( node ) ,
GroundTerm ::NamedNode ( node ) = > node . into ( ) ,
GroundTerm ::Literal ( literal ) = > Self ::Literal ( literal ) ,
GroundTerm ::Literal ( literal ) = > literal . into ( ) ,
GroundTerm ::Triple ( triple ) = > GroundTriplePattern ::from ( triple ) . into ( ) ,
}
}
}
}
}
}
@ -523,12 +919,27 @@ impl From<NamedNodePattern> for GroundTermPattern {
#[ inline ]
#[ inline ]
fn from ( element : NamedNodePattern ) -> Self {
fn from ( element : NamedNodePattern ) -> Self {
match element {
match element {
NamedNodePattern ::NamedNode ( node ) = > Self ::NamedNode ( node ) ,
NamedNodePattern ::NamedNode ( node ) = > node . into ( ) ,
NamedNodePattern ::Variable ( var ) = > Self ::Variable ( var ) ,
NamedNodePattern ::Variable ( var ) = > var . into ( ) ,
}
}
}
}
}
}
impl TryFrom < TermPattern > for GroundTermPattern {
type Error = ( ) ;
#[ inline ]
fn try_from ( pattern : TermPattern ) -> Result < Self , ( ) > {
Ok ( match pattern {
TermPattern ::NamedNode ( named_node ) = > named_node . into ( ) ,
TermPattern ::BlankNode ( _ ) = > return Err ( ( ) ) ,
TermPattern ::Literal ( literal ) = > literal . into ( ) ,
TermPattern ::Triple ( triple ) = > GroundTriplePattern ::try_from ( triple ) ? . into ( ) ,
TermPattern ::Variable ( variable ) = > variable . into ( ) ,
} )
}
}
/// The union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [default graph name](https://www.w3.org/TR/rdf11-concepts/#dfn-default-graph) and [variables](https://www.w3.org/TR/sparql11-query/#sparqlQueryVariables).
/// The union of [IRIs](https://www.w3.org/TR/rdf11-concepts/#dfn-iri), [default graph name](https://www.w3.org/TR/rdf11-concepts/#dfn-default-graph) and [variables](https://www.w3.org/TR/sparql11-query/#sparqlQueryVariables).
#[ derive(Eq, PartialEq, Debug, Clone, Hash) ]
#[ derive(Eq, PartialEq, Debug, Clone, Hash) ]
pub enum GraphNamePattern {
pub enum GraphNamePattern {
@ -541,9 +952,9 @@ impl fmt::Display for GraphNamePattern {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
match self {
match self {
GraphNamePattern ::NamedNode ( node ) = > node . fmt ( f ) ,
Self ::NamedNode ( node ) = > node . fmt ( f ) ,
GraphNamePattern ::DefaultGraph = > f . write_str ( "DEFAULT" ) ,
Self ::DefaultGraph = > f . write_str ( "DEFAULT" ) ,
GraphNamePattern ::Variable ( var ) = > var . fmt ( f ) ,
Self ::Variable ( var ) = > var . fmt ( f ) ,
}
}
}
}
}
}
@ -566,7 +977,7 @@ impl From<GraphName> for GraphNamePattern {
#[ inline ]
#[ inline ]
fn from ( graph_name : GraphName ) -> Self {
fn from ( graph_name : GraphName ) -> Self {
match graph_name {
match graph_name {
GraphName ::NamedNode ( node ) = > Self ::NamedNode ( node ) ,
GraphName ::NamedNode ( node ) = > node . into ( ) ,
GraphName ::DefaultGraph = > Self ::DefaultGraph ,
GraphName ::DefaultGraph = > Self ::DefaultGraph ,
}
}
}
}
@ -576,8 +987,8 @@ impl From<NamedNodePattern> for GraphNamePattern {
#[ inline ]
#[ inline ]
fn from ( graph_name : NamedNodePattern ) -> Self {
fn from ( graph_name : NamedNodePattern ) -> Self {
match graph_name {
match graph_name {
NamedNodePattern ::NamedNode ( node ) = > Self ::NamedNode ( node ) ,
NamedNodePattern ::NamedNode ( node ) = > node . into ( ) ,
NamedNodePattern ::Variable ( var ) = > Self ::Variable ( var ) ,
NamedNodePattern ::Variable ( var ) = > var . into ( ) ,
}
}
}
}
}
}
@ -607,11 +1018,92 @@ impl TriplePattern {
impl fmt ::Display for TriplePattern {
impl fmt ::Display for TriplePattern {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
write! (
write! ( f , "{} {} {}" , self . subject , self . predicate , self . object )
f ,
}
"(triple {} {} {})" ,
}
self . subject , self . predicate , self . object
)
impl From < Triple > for TriplePattern {
#[ inline ]
fn from ( triple : Triple ) -> Self {
Self {
subject : triple . subject . into ( ) ,
predicate : triple . predicate . into ( ) ,
object : triple . object . into ( ) ,
}
}
}
impl From < Box < Triple > > for TriplePattern {
#[ inline ]
fn from ( triple : Box < Triple > ) -> Self {
Self {
subject : triple . subject . into ( ) ,
predicate : triple . predicate . into ( ) ,
object : triple . object . into ( ) ,
}
}
}
/// A [triple pattern](https://www.w3.org/TR/sparql11-query/#defn_TriplePattern) without blank nodes
#[ derive(Eq, PartialEq, Debug, Clone, Hash) ]
pub struct GroundTriplePattern {
pub subject : GroundTermPattern ,
pub predicate : NamedNodePattern ,
pub object : GroundTermPattern ,
}
impl fmt ::Display for GroundTriplePattern {
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
write! ( f , "{} {} {}" , self . subject , self . predicate , self . object )
}
}
impl From < GroundTriple > for GroundTriplePattern {
#[ inline ]
fn from ( triple : GroundTriple ) -> Self {
Self {
subject : triple . subject . into ( ) ,
predicate : triple . predicate . into ( ) ,
object : triple . object . into ( ) ,
}
}
}
impl From < Box < GroundTriple > > for GroundTriplePattern {
#[ inline ]
fn from ( triple : Box < GroundTriple > ) -> Self {
Self {
subject : triple . subject . into ( ) ,
predicate : triple . predicate . into ( ) ,
object : triple . object . into ( ) ,
}
}
}
impl TryFrom < TriplePattern > for GroundTriplePattern {
type Error = ( ) ;
#[ inline ]
fn try_from ( triple : TriplePattern ) -> Result < Self , Self ::Error > {
Ok ( Self {
subject : triple . subject . try_into ( ) ? ,
predicate : triple . predicate ,
object : triple . object . try_into ( ) ? ,
} )
}
}
impl TryFrom < Box < TriplePattern > > for GroundTriplePattern {
type Error = ( ) ;
#[ inline ]
fn try_from ( triple : Box < TriplePattern > ) -> Result < Self , Self ::Error > {
Ok ( Self {
subject : triple . subject . try_into ( ) ? ,
predicate : triple . predicate ,
object : triple . object . try_into ( ) ? ,
} )
}
}
}
}
@ -644,15 +1136,11 @@ impl fmt::Display for QuadPattern {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
if self . graph_name = = GraphNamePattern ::DefaultGraph {
if self . graph_name = = GraphNamePattern ::DefaultGraph {
write! (
write! ( f , "{} {} {}" , self . subject , self . predicate , self . object )
f ,
"(triple {} {} {})" ,
self . subject , self . predicate , self . object
)
} else {
} else {
write! (
write! (
f ,
f ,
"(graph {} (triple {} {} {})) " ,
"GRAPH {} {{ {} {} {} }}" ,
self . graph_name , self . subject , self . predicate , self . object
self . graph_name , self . subject , self . predicate , self . object
)
)
}
}
@ -672,21 +1160,31 @@ impl fmt::Display for GroundQuadPattern {
#[ inline ]
#[ inline ]
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
fn fmt ( & self , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
if self . graph_name = = GraphNamePattern ::DefaultGraph {
if self . graph_name = = GraphNamePattern ::DefaultGraph {
write! (
write! ( f , "{} {} {}" , self . subject , self . predicate , self . object )
f ,
"(triple {} {} {})" ,
self . subject , self . predicate , self . object
)
} else {
} else {
write! (
write! (
f ,
f ,
"(graph {} (triple {} {} {})) " ,
"GRAPH {} {{ {} {} {} }}" ,
self . graph_name , self . subject , self . predicate , self . object
self . graph_name , self . subject , self . predicate , self . object
)
)
}
}
}
}
}
}
impl TryFrom < QuadPattern > for GroundQuadPattern {
type Error = ( ) ;
#[ inline ]
fn try_from ( pattern : QuadPattern ) -> Result < Self , ( ) > {
Ok ( GroundQuadPattern {
subject : pattern . subject . try_into ( ) ? ,
predicate : pattern . predicate ,
object : pattern . object . try_into ( ) ? ,
graph_name : pattern . graph_name ,
} )
}
}
#[ inline ]
#[ inline ]
pub ( crate ) fn print_quoted_str ( string : & str , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
pub ( crate ) fn print_quoted_str ( string : & str , f : & mut fmt ::Formatter < ' _ > ) -> fmt ::Result {
f . write_char ( '"' ) ? ;
f . write_char ( '"' ) ? ;