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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
use std::str::FromStr;

use crate::{object::value, Direction, Id, Indexed, IndexedObject, Node, Object, ValidId};
use iref::{Iri, IriBuf};
use json_syntax::Print;
use langtag::LangTagBuf;
use rdf_types::{
	vocabulary::{IriVocabularyMut, LiteralVocabularyMut},
	Generator, Literal, Vocabulary,
};
use smallvec::SmallVec;
use static_iref::iri;

mod quad;
pub use quad::*;

pub const RDF_TYPE: &Iri = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
pub const RDF_FIRST: &Iri = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#first");
pub const RDF_REST: &Iri = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest");
pub const RDF_VALUE: &Iri = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#value");
pub const RDF_DIRECTION: &Iri = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#direction");
pub const RDF_JSON: &Iri = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#JSON");
/// IRI of the `http://www.w3.org/1999/02/22-rdf-syntax-ns#nil` value.
pub const RDF_NIL: &Iri = iri!("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil");

pub const XSD_BOOLEAN: &Iri = iri!("http://www.w3.org/2001/XMLSchema#boolean");
pub const XSD_INTEGER: &Iri = iri!("http://www.w3.org/2001/XMLSchema#integer");
pub const XSD_DOUBLE: &Iri = iri!("http://www.w3.org/2001/XMLSchema#double");
pub const XSD_STRING: &Iri = iri!("http://www.w3.org/2001/XMLSchema#string");

/// JSON-LD to RDF triple.
pub type Triple<T, B, L> = rdf_types::Triple<ValidId<T, B>, ValidId<T, B>, Value<T, B, L>>;

impl<T: Clone, B: Clone> Id<T, B> {
	fn rdf_value<L>(&self) -> Option<Value<T, B, L>> {
		match self {
			Id::Valid(id) => Some(Value::Id(id.clone())),
			Id::Invalid(_) => None,
		}
	}
}

/// Direction representation method.
///
/// Used by the RDF serializer to decide how to encode
/// [`Direction`](crate::Direction)s.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum RdfDirection {
	/// Encode direction in the string value type IRI using the
	/// `https://www.w3.org/ns/i18n#` prefix.
	///
	/// If a language tag is present the IRI will be of the form
	/// `https://www.w3.org/ns/i18n#language_direction` or simply
	/// `https://www.w3.org/ns/i18n#direction` otherwise where `direction` is
	/// either `rtl` or `ltr`.
	I18nDatatype,

	/// Encode the direction using a compound literal value.
	///
	/// In this case the direction tagged string is encoded with a fresh blank
	/// node identifier `_:b` and the following triples:
	/// ```nquads
	/// _:b http://www.w3.org/1999/02/22-rdf-syntax-ns#value value@language
	/// _:b http://www.w3.org/1999/02/22-rdf-syntax-ns#direction direction
	/// ```
	/// where `direction` is either `rtl` or `ltr`.
	CompoundLiteral,
}

#[derive(Debug, Clone)]
pub struct InvalidRdfDirection(pub String);

impl FromStr for RdfDirection {
	type Err = InvalidRdfDirection;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		match s {
			"i18n-datatype" => Ok(Self::I18nDatatype),
			"compound-literal" => Ok(Self::CompoundLiteral),
			_ => Err(InvalidRdfDirection(s.to_string())),
		}
	}
}

impl<'a> TryFrom<&'a str> for RdfDirection {
	type Error = InvalidRdfDirection;

	fn try_from(value: &'a str) -> Result<Self, Self::Error> {
		value.parse()
	}
}

/// Iterator over the triples of a compound literal representing a language
/// tagged string with direction.
pub struct CompoundLiteralTriples<T, B, L> {
	/// Compound literal identifier.
	id: ValidId<T, B>,

	/// String value.
	value: Option<Value<T, B, L>>,

	/// Direction value.
	direction: Option<Value<T, B, L>>,
}

impl<T: Clone, B: Clone, L: Clone> CompoundLiteralTriples<T, B, L> {
	fn next(&mut self, vocabulary: &mut impl IriVocabularyMut<Iri = T>) -> Option<Triple<T, B, L>> {
		if let Some(value) = self.value.take() {
			return Some(rdf_types::Triple(
				self.id.clone(),
				ValidId::Iri(vocabulary.insert(RDF_VALUE)),
				value,
			));
		}

		if let Some(direction) = self.direction.take() {
			return Some(rdf_types::Triple(
				self.id.clone(),
				ValidId::Iri(vocabulary.insert(RDF_DIRECTION)),
				direction,
			));
		}

		None
	}
}

/// Compound literal.
pub struct CompoundLiteral<T, B, L> {
	value: Value<T, B, L>,
	triples: Option<CompoundLiteralTriples<T, B, L>>,
}

impl<T: Clone> crate::object::Value<T> {
	fn rdf_value_with<V, G: Generator<V>>(
		&self,
		vocabulary: &mut V,
		generator: &mut G,
		rdf_direction: Option<RdfDirection>,
	) -> Option<CompoundLiteral<T, V::BlankId, V::Literal>>
	where
		V: Vocabulary<Iri = T> + IriVocabularyMut + LiteralVocabularyMut,
	{
		match self {
			Self::Json(json) => {
				let ty = vocabulary.insert(RDF_JSON);
				Some(CompoundLiteral {
					value: Value::Literal(vocabulary.insert_owned_literal(Literal::new(
						json.compact_print().to_string(),
						rdf_types::LiteralType::Any(ty),
					))),
					triples: None,
				})
			}
			Self::LangString(lang_string) => {
				let (string, language, direction) = lang_string.parts();

				let language = match language {
					Some(language) => match language.as_well_formed() {
						Some(tag) => Some(tag.to_owned()),
						None => return None,
					},
					None => None,
				};

				match direction {
					Some(direction) => match rdf_direction {
						Some(RdfDirection::I18nDatatype) => {
							let ty = vocabulary.insert(i18n(language, *direction).as_iri());
							Some(CompoundLiteral {
								value: Value::Literal(vocabulary.insert_owned_literal(
									Literal::new(
										string.to_string(),
										rdf_types::LiteralType::Any(ty),
									),
								)),
								triples: None,
							})
						}
						Some(RdfDirection::CompoundLiteral) => {
							let id = generator.next(vocabulary);
							Some(CompoundLiteral {
								value: id.into_term(),
								triples: None,
							})
						}
						None => match language {
							Some(tag) => Some(CompoundLiteral {
								value: Value::Literal(vocabulary.insert_owned_literal(
									Literal::new(
										string.to_string(),
										rdf_types::LiteralType::LangString(tag),
									),
								)),
								triples: None,
							}),
							None => {
								let ty = vocabulary.insert(XSD_STRING);
								Some(CompoundLiteral {
									value: Value::Literal(vocabulary.insert_owned_literal(
										Literal::new(
											string.to_string(),
											rdf_types::LiteralType::Any(ty),
										),
									)),
									triples: None,
								})
							}
						},
					},
					None => match language {
						Some(tag) => Some(CompoundLiteral {
							value: Value::Literal(vocabulary.insert_owned_literal(Literal::new(
								string.to_string(),
								rdf_types::LiteralType::LangString(tag),
							))),
							triples: None,
						}),
						None => {
							let ty = vocabulary.insert(XSD_STRING);
							Some(CompoundLiteral {
								value: Value::Literal(vocabulary.insert_owned_literal(
									Literal::new(
										string.to_string(),
										rdf_types::LiteralType::Any(ty),
									),
								)),
								triples: None,
							})
						}
					},
				}
			}
			Self::Literal(lit, ty) => {
				let (rdf_lit, prefered_rdf_ty) = match lit {
					value::Literal::Boolean(b) => {
						let lit = if *b {
							"true".to_string()
						} else {
							"false".to_string()
						};

						(lit, Some(vocabulary.insert(XSD_BOOLEAN)))
					}
					value::Literal::Null => ("null".to_string(), None),
					value::Literal::Number(n) => {
						if n.is_i64()
							&& !ty
								.as_ref()
								.map(|t| vocabulary.iri(t).unwrap() == XSD_DOUBLE)
								.unwrap_or(false)
						{
							(n.to_string(), Some(vocabulary.insert(XSD_INTEGER)))
						} else {
							(
								pretty_dtoa::dtoa(n.as_f64_lossy(), XSD_CANONICAL_FLOAT),
								Some(vocabulary.insert(XSD_DOUBLE)),
							)
						}
					}
					value::Literal::String(s) => (s.to_string(), None),
				};

				let rdf_ty = match ty {
					Some(id) => Some(id.clone()),
					None => prefered_rdf_ty,
				};

				Some(CompoundLiteral {
					value: match rdf_ty {
						Some(ty) => Value::Literal(vocabulary.insert_owned_literal(Literal::new(
							rdf_lit,
							rdf_types::LiteralType::Any(ty),
						))),
						None => {
							let ty = vocabulary.insert(XSD_STRING);
							Value::Literal(vocabulary.insert_owned_literal(Literal::new(
								rdf_lit,
								rdf_types::LiteralType::Any(ty),
							)))
						}
					},
					triples: None,
				})
			}
		}
	}
}

// <https://www.w3.org/TR/xmlschema11-2/#f-doubleLexmap>
const XSD_CANONICAL_FLOAT: pretty_dtoa::FmtFloatConfig = pretty_dtoa::FmtFloatConfig::default()
	.force_e_notation()
	.capitalize_e(true);

impl<T: Clone, B: Clone> Node<T, B> {
	fn rdf_value<L>(&self) -> Option<Value<T, B, L>> {
		self.id.as_ref().and_then(Id::rdf_value)
	}
}

impl<T: Clone, B: Clone> Object<T, B> {
	fn rdf_value_with<V, G: Generator<V>>(
		&self,
		vocabulary: &mut V,
		generator: &mut G,
		rdf_direction: Option<RdfDirection>,
	) -> Option<CompoundValue<T, B, V::Literal>>
	where
		V: Vocabulary<Iri = T, BlankId = B> + IriVocabularyMut + LiteralVocabularyMut,
	{
		match self {
			Self::Value(value) => value
				.rdf_value_with(vocabulary, generator, rdf_direction)
				.map(|compound_value| CompoundValue {
					value: compound_value.value,
					triples: compound_value.triples.map(CompoundValueTriples::literal),
				}),
			Self::Node(node) => node.rdf_value().map(|value| CompoundValue {
				value,
				triples: None,
			}),
			Self::List(list) => {
				if list.is_empty() {
					Some(CompoundValue {
						value: Value::Id(ValidId::Iri(vocabulary.insert(RDF_NIL))),
						triples: None,
					})
				} else {
					let id = generator.next(vocabulary);
					Some(CompoundValue {
						value: Clone::clone(&id).into_term(),
						triples: Some(CompoundValueTriples::List(ListTriples::new(
							list.as_slice(),
							id,
						))),
					})
				}
			}
		}
	}
}

pub struct CompoundValue<'a, T, B, L> {
	value: Value<T, B, L>,
	triples: Option<CompoundValueTriples<'a, T, B, L>>,
}

impl<'a, T: Clone, B: Clone> crate::quad::ObjectRef<'a, T, B> {
	pub fn rdf_value_with<V, G: Generator<V>>(
		&self,
		vocabulary: &mut V,
		generator: &mut G,
		rdf_direction: Option<RdfDirection>,
	) -> Option<CompoundValue<'a, T, B, V::Literal>>
	where
		V: Vocabulary<Iri = T, BlankId = B> + IriVocabularyMut + LiteralVocabularyMut,
	{
		match self {
			Self::Object(object) => object.rdf_value_with(vocabulary, generator, rdf_direction),
			Self::Node(node) => node.rdf_value().map(|value| CompoundValue {
				value,
				triples: None,
			}),
			Self::Ref(r) => r.rdf_value().map(|value| CompoundValue {
				value,
				triples: None,
			}),
		}
	}
}

enum ListItemTriples<'a, T, B, L> {
	NestedList(NestedListTriples<'a, T, B>),
	CompoundLiteral(Box<CompoundLiteralTriples<T, B, L>>),
}

struct NestedListTriples<'a, T, B> {
	head_ref: Option<ValidId<T, B>>,
	previous: Option<ValidId<T, B>>,
	iter: std::slice::Iter<'a, IndexedObject<T, B>>,
}

struct ListNode<'a, 'i, T, B> {
	id: &'i ValidId<T, B>,
	object: &'a Indexed<Object<T, B>>,
}

impl<'a, T, B> NestedListTriples<'a, T, B> {
	fn new(list: &'a [IndexedObject<T, B>], head_ref: ValidId<T, B>) -> Self {
		Self {
			head_ref: Some(head_ref),
			previous: None,
			iter: list.iter(),
		}
	}

	fn previous(&self) -> Option<&ValidId<T, B>> {
		self.previous.as_ref()
	}

	/// Pull the next object of the list.
	///
	/// Uses the given generator to assign as id to the list element.
	fn next<V: Vocabulary<Iri = T, BlankId = B>, G: Generator<V>>(
		&mut self,
		vocabulary: &mut V,
		generator: &mut G,
	) -> Option<ListNode<'a, '_, T, B>> {
		if let Some(next) = self.iter.next() {
			let id = match self.head_ref.take() {
				Some(id) => id,
				None => generator.next(vocabulary),
			};

			self.previous = Some(id);
			Some(ListNode {
				object: next,
				id: self.previous.as_ref().unwrap(),
			})
		} else {
			None
		}
	}
}

pub enum CompoundValueTriples<'a, T, B, L> {
	Literal(Box<CompoundLiteralTriples<T, B, L>>),
	List(ListTriples<'a, T, B, L>),
}

impl<'a, T, B, L> CompoundValueTriples<'a, T, B, L> {
	pub fn literal(l: CompoundLiteralTriples<T, B, L>) -> Self {
		Self::Literal(Box::new(l))
	}

	pub fn with<'n, V: Vocabulary<Iri = T, BlankId = B, Literal = L>, G: Generator<V>>(
		self,
		vocabulary: &'n mut V,
		generator: G,
		rdf_direction: Option<RdfDirection>,
	) -> CompoundValueTriplesWith<'a, 'n, V, G> {
		CompoundValueTriplesWith {
			vocabulary,
			generator,
			rdf_direction,
			inner: self,
		}
	}

	pub fn next<V, G: Generator<V>>(
		&mut self,
		vocabulary: &mut V,
		generator: &mut G,
		rdf_direction: Option<RdfDirection>,
	) -> Option<Triple<T, B, L>>
	where
		T: Clone,
		B: Clone,
		L: Clone,
		V: Vocabulary<Iri = T, BlankId = B, Literal = L> + IriVocabularyMut + LiteralVocabularyMut,
	{
		match self {
			Self::Literal(l) => l.next(vocabulary),
			Self::List(l) => l.next(vocabulary, generator, rdf_direction),
		}
	}
}

pub struct CompoundValueTriplesWith<'a, 'n, N: Vocabulary, G: Generator<N>> {
	vocabulary: &'n mut N,
	generator: G,
	rdf_direction: Option<RdfDirection>,
	inner: CompoundValueTriples<'a, N::Iri, N::BlankId, N::Literal>,
}

impl<'a, 'n, N: Vocabulary + IriVocabularyMut, G: Generator<N>> Iterator
	for CompoundValueTriplesWith<'a, 'n, N, G>
where
	N::Iri: AsRef<Iri> + Clone,
	N::BlankId: Clone,
	N::Literal: Clone,
	N: LiteralVocabularyMut,
{
	type Item = Triple<N::Iri, N::BlankId, N::Literal>;

	fn next(&mut self) -> Option<Self::Item> {
		self.inner
			.next(self.vocabulary, &mut self.generator, self.rdf_direction)
	}
}

/// Iterator over the RDF quads generated from a list of JSON-LD objects.
///
/// If the list contains nested lists, the iterator will also emit quads for those nested lists.
pub struct ListTriples<'a, T, B, L> {
	stack: SmallVec<[ListItemTriples<'a, T, B, L>; 2]>,
	pending: Option<Triple<T, B, L>>,
}

impl<'a, T, B, L> ListTriples<'a, T, B, L> {
	pub fn new(list: &'a [IndexedObject<T, B>], head_ref: ValidId<T, B>) -> Self {
		let mut stack = SmallVec::new();
		stack.push(ListItemTriples::NestedList(NestedListTriples::new(
			list, head_ref,
		)));

		Self {
			stack,
			pending: None,
		}
	}

	pub fn with<'n, V: Vocabulary<Iri = T, BlankId = B, Literal = L>, G: Generator<V>>(
		self,
		vocabulary: &'n mut V,
		generator: G,
		rdf_direction: Option<RdfDirection>,
	) -> ListTriplesWith<'a, 'n, V, G> {
		ListTriplesWith {
			vocabulary,
			generator,
			rdf_direction,
			inner: self,
		}
	}

	pub fn next<V, G: Generator<V>>(
		&mut self,
		vocabulary: &mut V,
		generator: &mut G,
		rdf_direction: Option<RdfDirection>,
	) -> Option<Triple<T, B, L>>
	where
		T: Clone,
		B: Clone,
		L: Clone,
		V: Vocabulary<Iri = T, BlankId = B, Literal = L> + IriVocabularyMut + LiteralVocabularyMut,
	{
		loop {
			if let Some(pending) = self.pending.take() {
				break Some(pending);
			}

			match self.stack.last_mut() {
				Some(ListItemTriples::CompoundLiteral(lit)) => match lit.next(vocabulary) {
					Some(triple) => break Some(triple),
					None => {
						self.stack.pop();
					}
				},
				Some(ListItemTriples::NestedList(list)) => {
					let previous = list.previous().cloned();
					match list.next(vocabulary, generator) {
						Some(node) => {
							if let Some(compound_value) =
								node.object
									.rdf_value_with(vocabulary, generator, rdf_direction)
							{
								let id = node.id.clone();

								if let Some(compound_triples) = compound_value.triples {
									match compound_triples {
										CompoundValueTriples::List(list) => {
											self.stack.extend(list.stack)
										}
										CompoundValueTriples::Literal(lit) => {
											self.stack.push(ListItemTriples::CompoundLiteral(lit))
										}
									}
								}

								self.pending = Some(rdf_types::Triple(
									id.clone(),
									ValidId::Iri(vocabulary.insert(RDF_FIRST)),
									compound_value.value,
								));

								if let Some(previous_id) = previous {
									break Some(rdf_types::Triple(
										previous_id,
										ValidId::Iri(vocabulary.insert(RDF_REST)),
										id.into_term(),
									));
								}
							}
						}
						None => {
							self.stack.pop();
							if let Some(previous_id) = previous {
								break Some(rdf_types::Triple(
									previous_id,
									ValidId::Iri(vocabulary.insert(RDF_REST)),
									Value::Id(ValidId::Iri(vocabulary.insert(RDF_NIL))),
								));
							}
						}
					}
				}
				None => break None,
			}
		}
	}
}

pub struct ListTriplesWith<'a, 'n, V: Vocabulary, G: Generator<V>> {
	vocabulary: &'n mut V,
	generator: G,
	rdf_direction: Option<RdfDirection>,
	inner: ListTriples<'a, V::Iri, V::BlankId, V::Literal>,
}

impl<'a, 'n, N: Vocabulary + IriVocabularyMut, G: Generator<N>> Iterator
	for ListTriplesWith<'a, 'n, N, G>
where
	N::Iri: AsRef<Iri> + Clone,
	N::BlankId: Clone,
	N::Literal: Clone,
	N: LiteralVocabularyMut,
{
	type Item = Triple<N::Iri, N::BlankId, N::Literal>;

	fn next(&mut self) -> Option<Self::Item> {
		self.inner
			.next(self.vocabulary, &mut self.generator, self.rdf_direction)
	}
}

fn i18n(language: Option<LangTagBuf>, direction: Direction) -> IriBuf {
	let iri = match &language {
		Some(language) => format!("https://www.w3.org/ns/i18n#{language}_{direction}"),
		None => format!("https://www.w3.org/ns/i18n#{direction}"),
	};

	IriBuf::new(iri).unwrap()
}

pub type Value<T, B, L> = rdf_types::Object<ValidId<T, B>, L>;