Skip to content

Commit

Permalink
Add vocabulary::ByRef.
Browse files Browse the repository at this point in the history
Add `vocabulary::Predicate`.
Impl `ExtractFromVocabulary` for `Triple`.
Impl `ExtractedFromVocabulary` for `Triple`.
  • Loading branch information
timothee-haudebourg committed Mar 25, 2024
1 parent c4a61c2 commit 1dc4940
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 35 deletions.
8 changes: 4 additions & 4 deletions src/literal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ impl<V: IriVocabulary> ExtractFromVocabulary<V> for Literal<V::Iri> {
impl<V: IriVocabulary> ExtractedFromVocabulary<V> for Literal<V::Iri> {
type Extracted = Literal;

fn exported_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
fn extracted_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
Literal::new(
self.value.clone(),
self.type_.exported_from_vocabulary(vocabulary),
self.type_.extracted_from_vocabulary(vocabulary),
)
}
}
Expand Down Expand Up @@ -342,10 +342,10 @@ impl<'a, V: IriVocabulary> ExtractFromVocabulary<V> for LiteralRef<'a, V::Iri> {
impl<'a, V: IriVocabulary> ExtractedFromVocabulary<V> for LiteralRef<'a, V::Iri> {
type Extracted = Literal;

fn exported_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
fn extracted_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
Literal::new(
self.value.to_owned(),
self.type_.exported_from_vocabulary(vocabulary),
self.type_.extracted_from_vocabulary(vocabulary),
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/literal/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<V: IriVocabulary> ExtractFromVocabulary<V> for LiteralType<V::Iri> {
impl<V: IriVocabulary> ExtractedFromVocabulary<V> for LiteralType<V::Iri> {
type Extracted = LiteralType;

fn exported_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
fn extracted_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
match self {
Self::Any(t) => LiteralType::Any(vocabulary.iri(t).unwrap().to_owned()),
Self::LangString(t) => LiteralType::LangString(t.clone()),
Expand Down Expand Up @@ -312,7 +312,7 @@ impl<'a, V: IriVocabulary> ExtractFromVocabulary<V> for LiteralTypeRef<'a, V::Ir
impl<'a, V: IriVocabulary> ExtractedFromVocabulary<V> for LiteralTypeRef<'a, V::Iri> {
type Extracted = LiteralType;

fn exported_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
fn extracted_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
match *self {
Self::Any(t) => LiteralType::Any(vocabulary.iri(t).unwrap().to_owned()),
Self::LangString(t) => LiteralType::LangString(t.to_owned()),
Expand Down
36 changes: 30 additions & 6 deletions src/quad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use iref::{Iri, IriBuf};
use crate::{
interpretation::Interpret,
vocabulary::{
EmbedIntoVocabulary, EmbeddedIntoVocabulary, ExtractFromVocabulary,
ByRef, EmbedIntoVocabulary, EmbeddedIntoVocabulary, ExtractFromVocabulary,
ExtractedFromVocabulary, TryExtractFromVocabulary,
},
GraphLabel, Id, Interpretation, LexicalGraphLabelRef, LexicalObjectRef, LexicalSubjectRef,
Expand Down Expand Up @@ -320,6 +320,30 @@ impl<
}
}

impl<V, S, P, O, G> ExtractFromVocabulary<V> for ByRef<Quad<S, P, O, G>>
where
ByRef<S>: ExtractFromVocabulary<V>,
ByRef<P>: ExtractFromVocabulary<V>,
ByRef<O>: ExtractFromVocabulary<V>,
ByRef<G>: ExtractFromVocabulary<V>,
{
type Extracted = Quad<
<ByRef<S> as ExtractFromVocabulary<V>>::Extracted,
<ByRef<P> as ExtractFromVocabulary<V>>::Extracted,
<ByRef<O> as ExtractFromVocabulary<V>>::Extracted,
<ByRef<G> as ExtractFromVocabulary<V>>::Extracted,
>;

fn extract_from_vocabulary(self, vocabulary: &V) -> Self::Extracted {
Quad(
ByRef(self.0 .0).extract_from_vocabulary(vocabulary),
ByRef(self.0 .1).extract_from_vocabulary(vocabulary),
ByRef(self.0 .2).extract_from_vocabulary(vocabulary),
ByRef(self.0 .3).extract_from_vocabulary(vocabulary),
)
}
}

impl<
V,
S: ExtractedFromVocabulary<V>,
Expand All @@ -330,12 +354,12 @@ impl<
{
type Extracted = Quad<S::Extracted, P::Extracted, O::Extracted, G::Extracted>;

fn exported_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
fn extracted_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
Quad(
self.0.exported_from_vocabulary(vocabulary),
self.1.exported_from_vocabulary(vocabulary),
self.2.exported_from_vocabulary(vocabulary),
self.3.exported_from_vocabulary(vocabulary),
self.0.extracted_from_vocabulary(vocabulary),
self.1.extracted_from_vocabulary(vocabulary),
self.2.extracted_from_vocabulary(vocabulary),
self.3.extracted_from_vocabulary(vocabulary),
)
}
}
Expand Down
19 changes: 16 additions & 3 deletions src/term/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use locspan_derive::*;

use crate::{
vocabulary::{
BlankIdVocabulary, EmbedIntoVocabulary, EmbeddedIntoVocabulary, ExtractFromVocabulary,
ExtractedFromVocabulary, IriVocabulary,
BlankIdVocabulary, ByRef, EmbedIntoVocabulary, EmbeddedIntoVocabulary,
ExtractFromVocabulary, ExtractedFromVocabulary, IriVocabulary,
},
BlankId, BlankIdBuf, LexicalGraphLabelRef, LexicalSubjectRef, MaybeBlankId, MaybeIri,
RdfDisplay, Term, TryAsBlankId, TryAsIri, TryIntoBlankId, TryIntoIri, Vocabulary,
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<V, I: EmbeddedIntoVocabulary<V>, B: EmbeddedIntoVocabulary<V>> EmbeddedInto
impl<V: IriVocabulary + BlankIdVocabulary> ExtractedFromVocabulary<V> for Id<V::Iri, V::BlankId> {
type Extracted = Id<IriBuf, BlankIdBuf>;

fn exported_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
fn extracted_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
match self {
Self::Iri(i) => Id::Iri(vocabulary.iri(i).unwrap().to_owned()),
Self::Blank(b) => Id::Blank(vocabulary.blank_id(b).unwrap().to_owned()),
Expand All @@ -163,6 +163,19 @@ impl<V: IriVocabulary + BlankIdVocabulary> ExtractFromVocabulary<V> for Id<V::Ir
}
}

impl<'a, V: IriVocabulary + BlankIdVocabulary> ExtractFromVocabulary<V>
for ByRef<Id<&'a V::Iri, &'a V::BlankId>>
{
type Extracted = Id<IriBuf, BlankIdBuf>;

fn extract_from_vocabulary(self, vocabulary: &V) -> Self::Extracted {
match self.0 {
Id::Iri(i) => Id::Iri(vocabulary.iri(i).unwrap().to_owned()),
Id::Blank(b) => Id::Blank(vocabulary.blank_id(b).unwrap().to_owned()),
}
}
}

/// Type that can turn an `Id<I, B>` into an `Id`.
pub trait TryExportId<I, B> {
type Error;
Expand Down
50 changes: 38 additions & 12 deletions src/term/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::interpretation::{Interpret, LiteralInterpretationMut};
use crate::vocabulary::{
EmbedIntoVocabulary, EmbeddedIntoVocabulary, ExtractFromVocabulary, ExtractedFromVocabulary,
LiteralVocabulary, TryExtractFromVocabulary,
ByRef, EmbedIntoVocabulary, EmbeddedIntoVocabulary, ExtractFromVocabulary,
ExtractedFromVocabulary, LiteralVocabulary, TryExtractFromVocabulary,
};
use crate::{BlankIdBuf, Literal, RdfDisplay};
use iref::IriBuf;
Expand Down Expand Up @@ -234,30 +234,56 @@ impl<I: LiteralInterpretationMut<L>, T: Interpret<I, Interpreted = I::Resource>,

impl<V: LiteralVocabulary, I: ExtractedFromVocabulary<V>> ExtractedFromVocabulary<V>
for Term<I, V::Literal>
where
V::Literal: ExtractedFromVocabulary<V>,
{
type Extracted = Term<I::Extracted, <V::Literal as ExtractedFromVocabulary<V>>::Extracted>;
type Extracted = Term<I::Extracted, Literal>;

fn exported_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
fn extracted_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
match self {
Self::Id(i) => Term::Id(i.exported_from_vocabulary(vocabulary)),
Self::Literal(l) => Term::Literal(l.exported_from_vocabulary(vocabulary)),
Self::Id(i) => Term::Id(i.extracted_from_vocabulary(vocabulary)),
Self::Literal(l) => Term::Literal(
vocabulary
.literal(l)
.unwrap()
.extract_from_vocabulary(vocabulary),
),
}
}
}

impl<V: LiteralVocabulary, I: ExtractFromVocabulary<V>> ExtractFromVocabulary<V>
for Term<I, V::Literal>
where
V::Literal: ExtractedFromVocabulary<V>,
{
type Extracted = Term<I::Extracted, <V::Literal as ExtractedFromVocabulary<V>>::Extracted>;
type Extracted = Term<I::Extracted, Literal>;

fn extract_from_vocabulary(self, vocabulary: &V) -> Self::Extracted {
match self {
Self::Id(i) => Term::Id(i.extract_from_vocabulary(vocabulary)),
Self::Literal(l) => Term::Literal(l.exported_from_vocabulary(vocabulary)),
Self::Literal(l) => Term::Literal(
vocabulary
.owned_literal(l)
.ok()
.unwrap()
.extract_from_vocabulary(vocabulary),
),
}
}
}

impl<'a, V: LiteralVocabulary, I> ExtractFromVocabulary<V> for ByRef<Term<I, &'a V::Literal>>
where
ByRef<I>: ExtractFromVocabulary<V>,
{
type Extracted = Term<<ByRef<I> as ExtractFromVocabulary<V>>::Extracted, Literal>;

fn extract_from_vocabulary(self, vocabulary: &V) -> Self::Extracted {
match self.0 {
Term::Id(i) => Term::Id(ByRef(i).extract_from_vocabulary(vocabulary)),
Term::Literal(l) => Term::Literal(
vocabulary
.literal(l)
.unwrap()
.extract_from_vocabulary(vocabulary),
),
}
}
}
Expand Down
58 changes: 57 additions & 1 deletion src/triple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::{cmp::Ordering, fmt};
use iref::{Iri, IriBuf};

use crate::{
vocabulary::{EmbedIntoVocabulary, EmbeddedIntoVocabulary},
vocabulary::{
ByRef, EmbedIntoVocabulary, EmbeddedIntoVocabulary, ExtractFromVocabulary,
ExtractedFromVocabulary,
},
Id, LexicalObjectRef, LexicalSubjectRef, Object, Quad, RdfDisplay, Term,
};

Expand Down Expand Up @@ -200,6 +203,59 @@ impl<'a> LexicalTripleRef<'a> {
}
}

impl<V, S: ExtractFromVocabulary<V>, P: ExtractFromVocabulary<V>, O: ExtractFromVocabulary<V>>
ExtractFromVocabulary<V> for Triple<S, P, O>
{
type Extracted = Triple<S::Extracted, P::Extracted, O::Extracted>;

fn extract_from_vocabulary(self, vocabulary: &V) -> Self::Extracted {
Triple(
self.0.extract_from_vocabulary(vocabulary),
self.1.extract_from_vocabulary(vocabulary),
self.2.extract_from_vocabulary(vocabulary),
)
}
}

impl<V, S, P, O> ExtractFromVocabulary<V> for ByRef<Triple<S, P, O>>
where
ByRef<S>: ExtractFromVocabulary<V>,
ByRef<P>: ExtractFromVocabulary<V>,
ByRef<O>: ExtractFromVocabulary<V>,
{
type Extracted = Triple<
<ByRef<S> as ExtractFromVocabulary<V>>::Extracted,
<ByRef<P> as ExtractFromVocabulary<V>>::Extracted,
<ByRef<O> as ExtractFromVocabulary<V>>::Extracted,
>;

fn extract_from_vocabulary(self, vocabulary: &V) -> Self::Extracted {
Triple(
ByRef(self.0 .0).extract_from_vocabulary(vocabulary),
ByRef(self.0 .1).extract_from_vocabulary(vocabulary),
ByRef(self.0 .2).extract_from_vocabulary(vocabulary),
)
}
}

impl<
V,
S: ExtractedFromVocabulary<V>,
P: ExtractedFromVocabulary<V>,
O: ExtractedFromVocabulary<V>,
> ExtractedFromVocabulary<V> for Triple<S, P, O>
{
type Extracted = Triple<S::Extracted, P::Extracted, O::Extracted>;

fn extracted_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
Triple(
self.0.extracted_from_vocabulary(vocabulary),
self.1.extracted_from_vocabulary(vocabulary),
self.2.extracted_from_vocabulary(vocabulary),
)
}
}

impl<V, S: EmbedIntoVocabulary<V>, P: EmbedIntoVocabulary<V>, O: EmbedIntoVocabulary<V>>
EmbedIntoVocabulary<V> for Triple<S, P, O>
{
Expand Down
4 changes: 2 additions & 2 deletions src/vocabulary/impl/indexed/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ where
{
type Extracted = Literal;

fn exported_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
fn extracted_from_vocabulary(&self, vocabulary: &V) -> Self::Extracted {
let literal = vocabulary.literal(self).unwrap();
let value = literal.value.to_owned();
let type_ = literal.type_.exported_from_vocabulary(vocabulary);
let type_ = literal.type_.extracted_from_vocabulary(vocabulary);
Literal::new(value, type_)
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/vocabulary/impl/none.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ptr::addr_of_mut;

use crate::{
vocabulary::{
BlankIdVocabulary, BlankIdVocabularyMut, IriVocabulary, IriVocabularyMut,
Expand All @@ -18,13 +20,13 @@ static mut NO_VOCABULARY: NoVocabulary = ();
/// Returns a static reference to unit (no vocabulary).
#[inline(always)]
pub fn no_vocabulary() -> &'static NoVocabulary {
unsafe { &NO_VOCABULARY }
&()
}

/// Returns a static mutable reference to unit (no vocabulary).
#[inline(always)]
pub fn no_vocabulary_mut() -> &'static mut NoVocabulary {
unsafe { &mut NO_VOCABULARY }
unsafe { &mut *addr_of_mut!(NO_VOCABULARY) }
}

impl IriVocabulary for NoVocabulary {
Expand Down
Loading

0 comments on commit 1dc4940

Please sign in to comment.