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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std::fmt;
use std::hash::Hash;
use std::convert::TryFrom;
use iref::{Iri, IriBuf};
use crate::{
Id,
Reference,
ToReference,
Lenient
};
pub trait Vocab: Clone + PartialEq + Eq + Hash {
fn from_iri(iri: Iri) -> Option<Self>;
fn as_iri(&self) -> Iri;
}
impl<T: Clone + PartialEq + Eq + Hash> Vocab for T where for<'a> T: TryFrom<Iri<'a>>, for<'a> &'a T: Into<Iri<'a>> {
fn from_iri(iri: Iri) -> Option<Self> {
match T::try_from(iri) {
Ok(t) => Some(t),
Err(_) => None
}
}
fn as_iri(&self) -> Iri {
self.into()
}
}
impl<V: Vocab> ToReference<Lexicon<V>> for V {
type Reference = Reference<Lexicon<V>>;
fn to_ref(&self) -> Self::Reference {
Reference::Id(Lexicon::Id(self.clone()))
}
}
impl<V: Vocab> PartialEq<V> for Lenient<Reference<Lexicon<V>>> {
fn eq(&self, other: &V) -> bool {
match self {
Lenient::Ok(Reference::Id(Lexicon::Id(v))) => {
other == v
},
_ => false
}
}
}
impl<V: Vocab> PartialEq<V> for Reference<Lexicon<V>> {
fn eq(&self, other: &V) -> bool {
match self {
Reference::Id(Lexicon::Id(v)) => {
other == v
},
_ => false
}
}
}
impl<V: Vocab> PartialEq<V> for Lexicon<V> {
fn eq(&self, other: &V) -> bool {
match self {
Lexicon::Id(v) => {
other == v
},
_ => false
}
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Lexicon<V: Vocab> {
Id(V),
Iri(IriBuf)
}
impl<V: Vocab> fmt::Display for Lexicon<V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Lexicon::Id(id) => id.as_iri().fmt(f),
Lexicon::Iri(iri) => iri.fmt(f)
}
}
}
impl<V: Vocab> Id for Lexicon<V> {
fn from_iri(iri: Iri) -> Lexicon<V> {
if let Some(v) = V::from_iri(iri) {
Lexicon::Id(v)
} else {
Lexicon::Iri(iri.into())
}
}
fn as_iri(&self) -> Iri {
match self {
Lexicon::Id(id) => id.as_iri(),
Lexicon::Iri(iri) => iri.as_iri(),
}
}
}