1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::{BlankId, BlankIdBuf, Id, Namespace};
use iref::{Iri, IriBuf};

mod index;
mod none;
mod scoped;

pub use index::*;
pub use none::*;
pub use scoped::*;

/// Vocabulary.
///
/// A vocabulary is a collection that stores the lexical representation of
/// IRIs and blank node identifiers.
/// This allows the use of custom lightweight types to store, copy and compare
/// IRIs and blank IDs.
///
/// Any vocabulary implements the `Namespace` trait.
pub trait Vocabulary: IriVocabulary + BlankIdVocabulary {}

/// Mutable vocabulary.
pub trait VocabularyMut: Vocabulary + IriVocabularyMut + BlankIdVocabularyMut {}

impl<V: IriVocabulary + BlankIdVocabulary> Vocabulary for V {}

/// Any vocabulary is also a namespace.
impl<V: Vocabulary> Namespace for V {
	type Id = Id<V::Iri, V::BlankId>;
}

impl<V: IriVocabularyMut + BlankIdVocabularyMut> VocabularyMut for V {}

/// IRI vocabulary.
pub trait IriVocabulary {
	type Iri;

	/// Returns the IRI associated to the given IRI id.
	fn iri<'i>(&'i self, id: &'i Self::Iri) -> Option<Iri<'i>>;

	/// Returns a copy of the IRI associated to the given IRI id.
	fn owned_iri(&self, id: Self::Iri) -> Result<IriBuf, Self::Iri> {
		self.iri(&id).map(Iri::to_owned).ok_or(id)
	}

	/// Returns the id of the given IRI, if any.
	fn get(&self, iri: Iri) -> Option<Self::Iri>;
}

/// Mutable IRI vocabulary.
pub trait IriVocabularyMut: IriVocabulary {
	/// Inserts an IRI to the vocabulary and returns its id.
	///
	/// If the IRI was already present in the vocabulary, no new id is created
	/// and the current one is returned.
	fn insert(&mut self, iri: Iri) -> Self::Iri;
}

/// Blank node identifier vocabulary.
pub trait BlankIdVocabulary {
	type BlankId;

	/// Returns the blank node identifier associated to the given id.
	fn blank_id<'b>(&'b self, id: &'b Self::BlankId) -> Option<&'b BlankId>;

	/// Returns a copy of the blank node identifier associated to the given id.
	fn owned_blank_id(&self, id: Self::BlankId) -> Result<BlankIdBuf, Self::BlankId> {
		self.blank_id(&id).map(BlankId::to_owned).ok_or(id)
	}

	/// Returns the vocabulary id of the given blank node identifier, if any.
	fn get_blank_id(&self, id: &BlankId) -> Option<Self::BlankId>;
}

/// Mutable blank node identifier vocabulary.
pub trait BlankIdVocabularyMut: BlankIdVocabulary {
	/// Inserts a blank node identifier to the vocabulary and returns its id.
	///
	/// If the blank id was already present in the vocabulary, no new
	/// vocabulary id is created and the current one is returned.
	fn insert_blank_id(&mut self, id: &BlankId) -> Self::BlankId;
}