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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use crate::object::{InvalidExpandedJson, TryFromJson};
use crate::Term;
use contextual::{AsRefWithContext, DisplayWithContext, WithContext};
use iref::{Iri, IriBuf};
use json_ld_syntax::IntoJsonWithContextMeta;
use locspan::Meta;
use locspan_derive::*;
use rdf_types::{
	BlankId, BlankIdBuf, BlankIdVocabulary, InvalidBlankId, IriVocabulary, Vocabulary,
	VocabularyMut,
};
use std::borrow::Borrow;
use std::convert::TryFrom;
use std::fmt;

pub use rdf_types::MetaGenerator as Generator;

pub type ValidVocabularyId<V> =
	ValidId<<V as IriVocabulary>::Iri, <V as BlankIdVocabulary>::BlankId>;

pub type MetaValidVocabularyId<V, M> = Meta<ValidVocabularyId<V>, M>;

pub type VocabularyId<V> = Id<<V as IriVocabulary>::Iri, <V as BlankIdVocabulary>::BlankId>;

pub type MetaVocabularyId<V, M> = Meta<VocabularyId<V>, M>;

/// Node identifier.
///
/// Used to reference a node across a document or to a remote document.
/// It can be an identifier (IRI), a blank node identifier for local blank nodes
/// or an invalid reference (a string that is neither an IRI nor blank node identifier).
#[derive(
	Clone, PartialEq, Eq, Hash, PartialOrd, Ord, StrippedPartialEq, StrippedEq, StrippedHash,
)]
#[stripped(I, B)]
pub enum Id<I = IriBuf, B = BlankIdBuf> {
	Valid(ValidId<I, B>),

	/// Invalid reference.
	Invalid(#[stripped] String),
}

impl<I, B, M> TryFromJson<I, B, M> for Id<I, B> {
	fn try_from_json_in(
		vocabulary: &mut impl VocabularyMut<Iri = I, BlankId = B>,
		Meta(value, meta): locspan::Meta<json_syntax::Value<M>, M>,
	) -> Result<Meta<Self, M>, locspan::Meta<InvalidExpandedJson<M>, M>> {
		match value {
			json_syntax::Value::String(s) => match Iri::new(s.as_str()) {
				Ok(iri) => Ok(Meta(
					Self::Valid(ValidId::Iri(vocabulary.insert(iri))),
					meta,
				)),
				Err(_) => match BlankId::new(s.as_str()) {
					Ok(blank_id) => Ok(Meta(
						Self::Valid(ValidId::Blank(vocabulary.insert_blank_id(blank_id))),
						meta,
					)),
					Err(_) => Ok(Meta(Self::Invalid(s.to_string()), meta)),
				},
			},
			_ => Err(Meta(InvalidExpandedJson::InvalidId, meta)),
		}
	}
}

impl<I: From<IriBuf>, B: From<BlankIdBuf>> Id<I, B> {
	pub fn from_string(s: String) -> Self {
		match IriBuf::from_string(s) {
			Ok(iri) => Self::Valid(ValidId::Iri(iri.into())),
			Err((_, s)) => match BlankIdBuf::new(s) {
				Ok(blank) => Self::Valid(ValidId::Blank(blank.into())),
				Err(InvalidBlankId(s)) => Self::Invalid(s),
			},
		}
	}
}

impl<I, B> Id<I, B> {
	pub fn iri(iri: I) -> Self {
		Self::Valid(ValidId::Iri(iri))
	}

	pub fn blank(b: B) -> Self {
		Self::Valid(ValidId::Blank(b))
	}

	pub fn from_string_in(
		vocabulary: &mut impl VocabularyMut<Iri = I, BlankId = B>,
		s: String,
	) -> Self {
		match Iri::new(&s) {
			Ok(iri) => Self::Valid(ValidId::Iri(vocabulary.insert(iri))),
			Err(_) => match BlankId::new(&s) {
				Ok(blank) => Self::Valid(ValidId::Blank(vocabulary.insert_blank_id(blank))),
				Err(_) => Self::Invalid(s),
			},
		}
	}

	/// Checks if this is a valid reference.
	///
	/// Returns `true` is this reference is a node identifier or a blank node identifier,
	/// `false` otherwise.
	#[inline(always)]
	pub fn is_valid(&self) -> bool {
		!matches!(self, Self::Invalid(_))
	}

	pub fn into_blank(self) -> Option<B> {
		match self {
			Self::Valid(ValidId::Blank(b)) => Some(b),
			_ => None,
		}
	}

	/// If the reference is a node identifier, returns the node IRI.
	///
	/// Returns `None` if it is a blank node reference.
	#[inline(always)]
	pub fn as_iri(&self) -> Option<&I> {
		match self {
			Id::Valid(ValidId::Iri(k)) => Some(k),
			_ => None,
		}
	}

	#[inline(always)]
	pub fn into_term(self) -> Term<I, B> {
		Term::Ref(self)
	}

	pub fn as_ref(&self) -> Ref<I, B> {
		match self {
			Self::Valid(ValidId::Iri(t)) => Ref::Iri(t),
			Self::Valid(ValidId::Blank(id)) => Ref::Blank(id),
			Self::Invalid(id) => Ref::Invalid(id.as_str()),
		}
	}
}

impl<I: AsRef<str>, B: AsRef<str>> Id<I, B> {
	/// Get a string representation of the reference.
	///
	/// This will either return a string slice of an IRI, or a blank node identifier.
	#[inline(always)]
	pub fn as_str(&self) -> &str {
		match self {
			Id::Valid(ValidId::Iri(id)) => id.as_ref(),
			Id::Valid(ValidId::Blank(id)) => id.as_ref(),
			Id::Invalid(id) => id.as_str(),
		}
	}
}

impl<T, B, N: Vocabulary<Iri = T, BlankId = B>> AsRefWithContext<str, N> for Id<T, B> {
	fn as_ref_with<'a>(&'a self, vocabulary: &'a N) -> &'a str {
		match self {
			Id::Valid(ValidId::Iri(id)) => vocabulary.iri(id).unwrap().into_str(),
			Id::Valid(ValidId::Blank(id)) => vocabulary.blank_id(id).unwrap().as_str(),
			Id::Invalid(id) => id.as_str(),
		}
	}
}

impl<I: PartialEq, B> PartialEq<I> for Id<I, B> {
	fn eq(&self, other: &I) -> bool {
		match self {
			Id::Valid(ValidId::Iri(id)) => id == other,
			_ => false,
		}
	}
}

impl<T: PartialEq<str>, B: PartialEq<str>> PartialEq<str> for Id<T, B> {
	fn eq(&self, other: &str) -> bool {
		match self {
			Id::Valid(ValidId::Iri(iri)) => iri == other,
			Id::Valid(ValidId::Blank(blank)) => blank == other,
			Id::Invalid(id) => id == other,
		}
	}
}

impl<'a, T, B> From<&'a Id<T, B>> for Id<&'a T, &'a B> {
	fn from(r: &'a Id<T, B>) -> Id<&'a T, &'a B> {
		match r {
			Id::Valid(ValidId::Iri(id)) => Id::Valid(ValidId::Iri(id)),
			Id::Valid(ValidId::Blank(id)) => Id::Valid(ValidId::Blank(id)),
			Id::Invalid(id) => Id::Invalid(id.clone()),
		}
	}
}

impl<T, B> From<T> for Id<T, B> {
	#[inline(always)]
	fn from(id: T) -> Id<T, B> {
		Id::Valid(ValidId::Iri(id))
	}
}

impl<T: PartialEq, B: PartialEq> PartialEq<Term<T, B>> for Id<T, B> {
	#[inline]
	fn eq(&self, term: &Term<T, B>) -> bool {
		match term {
			Term::Ref(prop) => self == prop,
			_ => false,
		}
	}
}

impl<T: PartialEq, B: PartialEq> PartialEq<Id<T, B>> for Term<T, B> {
	#[inline]
	fn eq(&self, r: &Id<T, B>) -> bool {
		match self {
			Term::Ref(prop) => prop == r,
			_ => false,
		}
	}
}

impl<T, B> TryFrom<Term<T, B>> for Id<T, B> {
	type Error = Term<T, B>;

	#[inline]
	fn try_from(term: Term<T, B>) -> Result<Id<T, B>, Term<T, B>> {
		match term {
			Term::Ref(prop) => Ok(prop),
			term => Err(term),
		}
	}
}

impl<T: fmt::Display, B: fmt::Display> fmt::Display for Id<T, B> {
	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Id::Valid(id) => id.fmt(f),
			Id::Invalid(id) => id.fmt(f),
		}
	}
}

impl<T, B, N: Vocabulary<Iri = T, BlankId = B>> DisplayWithContext<N> for Id<T, B> {
	fn fmt_with(&self, vocabulary: &N, f: &mut fmt::Formatter) -> fmt::Result {
		use fmt::Display;
		match self {
			Id::Valid(id) => id.fmt_with(vocabulary, f),
			Id::Invalid(id) => id.fmt(f),
		}
	}
}

impl<T: fmt::Debug, B: fmt::Debug> fmt::Debug for Id<T, B> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Id::Valid(id) => write!(f, "Id::Valid({:?})", id),
			Id::Invalid(id) => write!(f, "Id::Invalid({:?})", id),
		}
	}
}

impl<T, B, M, N: Vocabulary<Iri = T, BlankId = B>> IntoJsonWithContextMeta<M, N> for Id<T, B> {
	fn into_json_meta_with(self, meta: M, context: &N) -> Meta<json_syntax::Value<M>, M> {
		Meta(self.into_with(context).to_string().into(), meta)
	}
}

/// Types that can be converted into a borrowed node reference.
///
/// This is a convenient trait is used to simplify the use of references.
/// For instance consider the [`Node::get`](crate::Node::get) method, used to get the objects associated to the
/// given reference property for a given node.
/// It essentially have the following signature:
/// ```ignore
/// fn get(&self, id: &Id<T, B>) -> Objects;
/// ```
/// However building a `Id` by hand can be tedious, especially while using [`Lexicon`](crate::Lexicon) and
/// [`Vocab`](crate::Vocab). It can be as verbose as `node.get(&Id::Id(Lexicon::Id(MyVocab::Term)))`.
/// Thanks to `IntoId` which is implemented by `Lexicon<V>` for any type `V` implementing `Vocab`,
/// it is simplified into `node.get(MyVocab::Term)` (while the first syntax remains correct).
/// The signature of `get` becomes:
/// ```ignore
/// fn get<R: IntoId<T>>(self, id: R) -> Objects;
/// ```
pub trait IntoId<T, B> {
	/// The target type of the conversion, which can be borrowed as a `Id<T, B>`.
	type Id: Borrow<Id<T, B>>;

	/// Convert the value into a reference.
	fn to_ref(self) -> Self::Id;
}

impl<'a, T, B> IntoId<T, B> for &'a Id<T, B> {
	type Id = &'a Id<T, B>;

	#[inline(always)]
	fn to_ref(self) -> Self::Id {
		self
	}
}

impl<T, B> IntoId<T, B> for T {
	type Id = Id<T, B>;

	fn to_ref(self) -> Self::Id {
		Id::Valid(ValidId::Iri(self))
	}
}

pub use rdf_types::Subject as ValidId;

impl<T, B> From<ValidId<T, B>> for Id<T, B> {
	fn from(r: ValidId<T, B>) -> Self {
		Id::Valid(r)
	}
}

impl<T, B> TryFrom<Id<T, B>> for ValidId<T, B> {
	type Error = String;

	fn try_from(r: Id<T, B>) -> Result<Self, Self::Error> {
		match r {
			Id::Valid(r) => Ok(r),
			Id::Invalid(id) => Err(id),
		}
	}
}

impl<'a, T, B> TryFrom<&'a Id<T, B>> for &'a ValidId<T, B> {
	type Error = &'a String;

	fn try_from(r: &'a Id<T, B>) -> Result<Self, Self::Error> {
		match r {
			Id::Valid(r) => Ok(r),
			Id::Invalid(id) => Err(id),
		}
	}
}

impl<'a, T, B> TryFrom<&'a mut Id<T, B>> for &'a mut ValidId<T, B> {
	type Error = &'a mut String;

	fn try_from(r: &'a mut Id<T, B>) -> Result<Self, Self::Error> {
		match r {
			Id::Valid(r) => Ok(r),
			Id::Invalid(id) => Err(id),
		}
	}
}

impl<T: fmt::Display, B: fmt::Display> crate::rdf::Display for ValidId<T, B> {
	#[inline]
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::Iri(id) => write!(f, "<{}>", id),
			Self::Blank(b) => write!(f, "{}", b),
		}
	}
}

/// Id to a reference.
#[derive(Clone, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum Ref<'a, T = IriBuf, B = BlankIdBuf> {
	/// Node identifier, essentially an IRI.
	Iri(&'a T),

	/// Blank node identifier.
	Blank(&'a B),

	/// Invalid reference.
	Invalid(&'a str),
}

pub trait IdentifyAll<T, B, M> {
	fn identify_all_with<N: Vocabulary<Iri = T, BlankId = B>, G: Generator<N, M>>(
		&mut self,
		vocabulary: &mut N,
		generator: G,
	) where
		M: Clone;

	fn identify_all<G: Generator<(), M>>(&mut self, generator: G)
	where
		M: Clone,
		(): Vocabulary<Iri = T, BlankId = B>;
}