Skip to content

Commit

Permalink
Merge branch 'MattesWhite-literal-as-own-type'
Browse files Browse the repository at this point in the history
  • Loading branch information
pchampin committed Mar 19, 2020
2 parents 501add0 + 40224ee commit 77976a5
Show file tree
Hide file tree
Showing 19 changed files with 1,305 additions and 418 deletions.
38 changes: 19 additions & 19 deletions sophia/src/dataset/_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub type DQuad<'a, D> = StreamedQuad<'a, <D as Dataset>::Quad>;
pub type DResult<D, T> = Result<T, <D as Dataset>::Error>;
/// Type alias for fallible quad iterators produced by a dataset.
pub type DQuadSource<'a, D> = Box<dyn Iterator<Item = DResult<D, DQuad<'a, D>>> + 'a>;
/// Type alias for fallible hashets of terms produced by a dataset.
/// Type alias for fallible hashsets of terms produced by a dataset.
///
/// See [`Dataset::quads`](./trait.Dataset.html#tymethod.quads)
/// for more information about how to use it.
Expand Down Expand Up @@ -435,17 +435,17 @@ pub trait Dataset {
for q in self.quads() {
let q = q?;
let (s, p, o) = (q.s(), q.p(), q.o());
if let Iri(_) = s {
if let Term::Iri(_) = s {
insert_if_absent(&mut res, s)
}
if let Iri(_) = p {
if let Term::Iri(_) = p {
insert_if_absent(&mut res, p)
}
if let Iri(_) = o {
if let Term::Iri(_) = o {
insert_if_absent(&mut res, o)
}
if let Some(gn) = q.g() {
if let Iri(_) = gn {
if let Term::Iri(_) = gn {
insert_if_absent(&mut res, &gn)
}
}
Expand All @@ -459,17 +459,17 @@ pub trait Dataset {
for q in self.quads() {
let q = q?;
let (s, p, o) = (q.s(), q.p(), q.o());
if let BNode(_) = s {
if let Term::BNode(_) = s {
insert_if_absent(&mut res, s)
}
if let BNode(_) = p {
if let Term::BNode(_) = p {
insert_if_absent(&mut res, p)
}
if let BNode(_) = o {
if let Term::BNode(_) = o {
insert_if_absent(&mut res, o)
}
if let Some(gn) = q.g() {
if let BNode(_) = gn {
if let Term::BNode(_) = gn {
insert_if_absent(&mut res, &gn)
}
}
Expand All @@ -483,17 +483,17 @@ pub trait Dataset {
for q in self.quads() {
let q = q?;
let (s, p, o) = (q.s(), q.p(), q.o());
if let Literal(_, _) = s {
if let Term::Literal(_) = s {
insert_if_absent(&mut res, s)
}
if let Literal(_, _) = p {
if let Term::Literal(_) = p {
insert_if_absent(&mut res, p)
}
if let Literal(_, _) = o {
if let Term::Literal(_) = o {
insert_if_absent(&mut res, o)
}
if let Some(gn) = q.g() {
if let Literal(_, _) = gn {
if let Term::Literal(_) = gn {
insert_if_absent(&mut res, &gn)
}
}
Expand All @@ -507,17 +507,17 @@ pub trait Dataset {
for q in self.quads() {
let q = q?;
let (s, p, o) = (q.s(), q.p(), q.o());
if let Variable(_) = s {
if let Term::Variable(_) = s {
insert_if_absent(&mut res, s)
}
if let Variable(_) = p {
if let Term::Variable(_) = p {
insert_if_absent(&mut res, p)
}
if let Variable(_) = o {
if let Term::Variable(_) = o {
insert_if_absent(&mut res, o)
}
if let Some(gn) = q.g() {
if let Variable(_) = gn {
if let Term::Variable(_) = gn {
insert_if_absent(&mut res, &gn)
}
}
Expand Down Expand Up @@ -583,7 +583,7 @@ pub trait MutableDataset: Dataset {
///
/// NB: unless this dataset also implements [`SetDataset`](trait.SetDataset.html),
/// a return value of `true` does *not* mean that the quad was not already in the dataset,
/// only that the dataset now has one more occurence of it.
/// only that the dataset now has one more occurrence of it.
///
fn insert<T, U, V, W>(
&mut self,
Expand All @@ -604,7 +604,7 @@ pub trait MutableDataset: Dataset {
///
/// NB: unless this dataset also implements [`SetDataset`](trait.SetDataset.html),
/// a return value of `true` does *not* mean that the quad is not still contained in the dataset,
/// only that the dataset now has one less occurence of it.
/// only that the dataset now has one less occurrence of it.
///
fn remove<T, U, V, W>(
&mut self,
Expand Down
30 changes: 15 additions & 15 deletions sophia/src/graph/_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub type GResult<G, T> = Result<T, <G as Graph>::Error>;
/// See [`Graph::triples`](./trait.Graph.html#tymethod.triples)
/// for more information about how to use it.
pub type GTripleSource<'a, G> = Box<dyn Iterator<Item = GResult<G, GTriple<'a, G>>> + 'a>;
/// Type alias for fallible hashets of terms produced by a graph.
/// Type alias for fallible hashsets of terms produced by a graph.
pub type GResultTermSet<G> = GResult<G, HashSet<GTerm<G>>>;

/// Generic trait for RDF graphs.
Expand Down Expand Up @@ -269,13 +269,13 @@ pub trait Graph {
for t in self.triples() {
let t = t?;
let (s, p, o) = (t.s(), t.p(), t.o());
if let Iri(_) = s {
if let Term::Iri(_) = s {
insert_if_absent(&mut res, s)
}
if let Iri(_) = p {
if let Term::Iri(_) = p {
insert_if_absent(&mut res, p)
}
if let Iri(_) = o {
if let Term::Iri(_) = o {
insert_if_absent(&mut res, o)
}
}
Expand All @@ -288,13 +288,13 @@ pub trait Graph {
for t in self.triples() {
let t = t?;
let (s, p, o) = (t.s(), t.p(), t.o());
if let BNode(_) = s {
if let Term::BNode(_) = s {
insert_if_absent(&mut res, s)
}
if let BNode(_) = p {
if let Term::BNode(_) = p {
insert_if_absent(&mut res, p)
}
if let BNode(_) = o {
if let Term::BNode(_) = o {
insert_if_absent(&mut res, o)
}
}
Expand All @@ -307,13 +307,13 @@ pub trait Graph {
for t in self.triples() {
let t = t?;
let (s, p, o) = (t.s(), t.p(), t.o());
if let Literal(_, _) = s {
if let Term::Literal(_) = s {
insert_if_absent(&mut res, s)
}
if let Literal(_, _) = p {
if let Term::Literal(_) = p {
insert_if_absent(&mut res, p)
}
if let Literal(_, _) = o {
if let Term::Literal(_) = o {
insert_if_absent(&mut res, o)
}
}
Expand All @@ -326,13 +326,13 @@ pub trait Graph {
for t in self.triples() {
let t = t?;
let (s, p, o) = (t.s(), t.p(), t.o());
if let Variable(_) = s {
if let Term::Variable(_) = s {
insert_if_absent(&mut res, s)
}
if let Variable(_) = p {
if let Term::Variable(_) = p {
insert_if_absent(&mut res, p)
}
if let Variable(_) = o {
if let Term::Variable(_) = o {
insert_if_absent(&mut res, o)
}
}
Expand Down Expand Up @@ -376,7 +376,7 @@ pub trait MutableGraph: Graph {
///
/// NB: unless this graph also implements [`SetGraph`](trait.SetGraph.html),
/// a return value of `true` does *not* mean that the triple was not already in the graph,
/// only that the graph now has one more occurence of it.
/// only that the graph now has one more occurrence of it.
///
fn insert<T, U, V>(&mut self, s: &Term<T>, p: &Term<U>, o: &Term<V>) -> MGResult<Self, bool>
where
Expand All @@ -390,7 +390,7 @@ pub trait MutableGraph: Graph {
///
/// NB: unless this graph also implements [`SetGraph`](trait.SetGraph.html),
/// a return value of `true` does *not* mean that the triple is not still contained in the graph,
/// only that the graph now has one less occurence of it.
/// only that the graph now has one less occurrence of it.
///
fn remove<T, U, V>(&mut self, s: &Term<T>, p: &Term<U>, o: &Term<V>) -> MGResult<Self, bool>
where
Expand Down
48 changes: 45 additions & 3 deletions sophia/src/ns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,36 @@ impl<T: TermData> Namespace<T> {
/// as it is never checked that the prefix IRI is a valid IRI reference.
#[macro_export]
macro_rules! namespace {
($iri_prefix:expr, $($suffix:ident),*; $($r_id:ident, $r_sf:expr),*) => {
pub static PREFIX:&'static str = $iri_prefix;
$(
$crate::ns_term!($iri_prefix, $suffix);
)*
$(
$crate::ns_term!($iri_prefix, $r_id, $r_sf);
)*

pub mod iri {
$(
$crate::ns_iri!($iri_prefix, $suffix);
)*
$(
$crate::ns_iri!($iri_prefix, $r_id, $r_sf);
)*
}
};
($iri_prefix:expr, $($suffix:ident),*) => {
pub static PREFIX:&'static str = $iri_prefix;
$(
$crate::ns_term!($iri_prefix, $suffix);
)*
}

pub mod iri {
$(
$crate::ns_iri!($iri_prefix, $suffix);
)*
}
};
}

/// Helper for creating a term in a "namespace module".
Expand All @@ -84,6 +108,24 @@ macro_rules! ns_term {
};
}

/// Helper for creating a term in a "namespace module".
/// In general, you should use the [`namespace!`](macro.namespace.html) macro instead.
///
/// # Safety
/// This macro is conceptually unsafe,
/// as it is never checked that the prefix IRI is a valid IRI reference.
#[macro_export]
macro_rules! ns_iri {
($prefix:expr, $ident:ident) => {
$crate::ns_iri!($prefix, $ident, stringify!($ident));
};
($prefix:expr, $ident:ident, $suffix:expr) => {
#[allow(non_upper_case_globals)]
pub static $ident: $crate::term::iri::Iri<&'static str> =
$crate::term::iri::Iri::from_raw_parts_unchecked($prefix, Some($suffix), true);
};
}

//pub static $ident:term::Term<'static> = term::Term::Iri(term::IriData{ns:$prefix, suffix:$suffix});

/// The standard `rdf:` namespace.
Expand Down Expand Up @@ -127,9 +169,9 @@ pub mod rdf {
datatype,
bagID,
aboutEach,
aboutEachPrefix
aboutEachPrefix;
type_, "type"
);
ns_term!("http://www.w3.org/1999/02/22-rdf-syntax-ns#", type_, "type");
}

/// The standard `xsd:` namespace.
Expand Down
34 changes: 14 additions & 20 deletions sophia/src/parser/rio_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::result::Result as StdResult;
use rio_api::model::*;
use rio_api::parser::*;

use crate::ns::xsd;
use crate::quad::stream::*;
use crate::quad::streaming_mode::StreamedQuad;
use crate::term::{BoxTerm, RefTerm};
Expand Down Expand Up @@ -198,26 +197,21 @@ fn consume_err<E>(opt: &mut Option<E>) -> E {
pub fn rio2refterm(t: GeneralizedTerm) -> RefTerm {
use Literal::*;

unsafe {
match t {
GeneralizedTerm::BlankNode(b) => RefTerm::new_bnode(b.id).unwrap(),
GeneralizedTerm::NamedNode(n) => RefTerm::new_iri(n.iri)
.expect("Already checked by parser but determine if absolute."),
GeneralizedTerm::Literal(Simple { value }) => {
RefTerm::new_literal_dt_unchecked(value, xsd::string)
}
GeneralizedTerm::Literal(LanguageTaggedString { value, language }) => {
RefTerm::new_literal_lang_unchecked(value, language)
}
GeneralizedTerm::Literal(Typed { value, datatype }) => {
RefTerm::new_literal_dt_unchecked(
value,
RefTerm::new_iri(datatype.iri)
.expect("Already checked by parser but determine if absolute."),
)
}
GeneralizedTerm::Variable(v) => RefTerm::new_variable_unchecked(v.name),
match t {
GeneralizedTerm::BlankNode(b) => RefTerm::new_bnode(b.id).unwrap(),
GeneralizedTerm::NamedNode(n) => {
RefTerm::new_iri(n.iri).expect("Already checked by parser but determine if absolute.")
}
GeneralizedTerm::Literal(Simple { value }) => value.into(),
GeneralizedTerm::Literal(LanguageTaggedString { value, language }) => {
RefTerm::new_literal_lang_unchecked(value, language)
}
GeneralizedTerm::Literal(Typed { value, datatype }) => RefTerm::new_literal_dt_unchecked(
value,
RefTerm::new_iri(datatype.iri)
.expect("Already checked by parser but determine if absolute."),
),
GeneralizedTerm::Variable(v) => RefTerm::new_variable_unchecked(v.name),
}
}

Expand Down
2 changes: 1 addition & 1 deletion sophia/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ where

/// Make a matcher corresponding to term `t`, given binding `b`.
fn matcher(t: &RcTerm, b: &BindingMap) -> Binding {
if let Variable(var) = t {
if let Term::Variable(var) = t {
let vname: &str = var.as_ref();
b.get(vname).cloned().into()
} else {
Expand Down
6 changes: 3 additions & 3 deletions sophia/src/serializer/nq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ where
}

impl NqSerializer<Vec<u8>> {
/// Create a new serializer wich targets a `String`.
/// Create a new serializer which targets a `String`.
#[inline]
pub fn new_stringifier() -> Self {
NqSerializer::new(Vec::new())
}
/// Create a new serializer wich targets a `String` with a custom config.
/// Create a new serializer which targets a `String` with a custom config.
#[inline]
pub fn new_stringifier_with_config(config: NqConfig) -> Self {
NqSerializer::new_with_config(Vec::new(), config)
Expand Down Expand Up @@ -132,7 +132,7 @@ pub(crate) mod test {
[
me,
StaticTerm::new_iri("http://schema.org/name").unwrap(),
StaticTerm::new_literal_dt("Pierre-Antoine", xsd::string).unwrap(),
"Pierre-Antoine".into(),
],
Some(StaticTerm::new_iri("http://champin.net/").unwrap()),
),
Expand Down
6 changes: 3 additions & 3 deletions sophia/src/serializer/nt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ where
}

impl NtSerializer<Vec<u8>> {
/// Create a new serializer wich targets a `String`.
/// Create a new serializer which targets a `String`.
#[inline]
pub fn new_stringifier() -> Self {
NtSerializer::new(Vec::new())
}
/// Create a new serializer wich targets a `String` with a custom config.
/// Create a new serializer which targets a `String` with a custom config.
#[inline]
pub fn new_stringifier_with_config(config: NtConfig) -> Self {
NtSerializer::new_with_config(Vec::new(), config)
Expand Down Expand Up @@ -122,7 +122,7 @@ pub(crate) mod test {
[
me,
StaticTerm::new_iri("http://schema.org/name").unwrap(),
StaticTerm::new_literal_dt("Pierre-Antoine", xsd::string).unwrap(),
"Pierre-Antoine".into(),
],
];
let s = NtSerializer::new_stringifier()
Expand Down
Loading

0 comments on commit 77976a5

Please sign in to comment.