json-ld-compaction 0.21.4

JSON-LD document compaction implementation
Documentation
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
use crate::{Options, TypeLangValue};
use contextual::WithContext;
use json_ld_core::{
	context::inverse::{LangSelection, Selection, TypeSelection},
	object, Container, Context, Indexed, Nullable, Object, ProcessingMode, Term, Type, Value,
};
use json_ld_syntax::{is_keyword, is_keyword_like};
use rdf_types::Vocabulary;
use std::hash::Hash;

pub struct IriConfusedWithPrefix;

/// Compact the given term without considering any value.
///
/// Calls [`compact_iri_full`] with `None` for `value`.
pub(crate) fn compact_iri<N>(
	vocabulary: &N,
	active_context: &Context<N::Iri, N::BlankId>,
	var: &Term<N::Iri, N::BlankId>,
	vocab: bool,
	reverse: bool,
	options: Options,
) -> Result<Option<String>, IriConfusedWithPrefix>
where
	N: Vocabulary,
	N::Iri: Clone + Hash + Eq,
	N::BlankId: Clone + Hash + Eq,
{
	compact_iri_full::<N, Object<N::Iri, N::BlankId>>(
		vocabulary,
		active_context,
		var,
		None,
		vocab,
		reverse,
		options,
	)
}

pub(crate) fn compact_key<N>(
	vocabulary: &N,
	active_context: &Context<N::Iri, N::BlankId>,
	var: &Term<N::Iri, N::BlankId>,
	vocab: bool,
	reverse: bool,
	options: Options,
) -> Result<Option<json_syntax::object::Key>, IriConfusedWithPrefix>
where
	N: Vocabulary,
	N::Iri: Clone + Hash + Eq,
	N::BlankId: Clone + Hash + Eq,
{
	Ok(compact_iri(vocabulary, active_context, var, vocab, reverse, options)?.map(Into::into))
}

/// Compact the given term considering the given value object.
///
/// Calls [`compact_iri_full`] with `Some(value)`.
pub(crate) fn compact_iri_with<N, O>(
	vocabulary: &N,
	active_context: &Context<N::Iri, N::BlankId>,
	var: &Term<N::Iri, N::BlankId>,
	value: &Indexed<O>,
	vocab: bool,
	reverse: bool,
	options: Options,
) -> Result<Option<String>, IriConfusedWithPrefix>
where
	N: Vocabulary,
	N::Iri: Clone + Hash + Eq,
	N::BlankId: Clone + Hash + Eq,
	O: object::Any<N::Iri, N::BlankId>,
{
	compact_iri_full(
		vocabulary,
		active_context,
		var,
		Some(value),
		vocab,
		reverse,
		options,
	)
}

/// Compact the given term.
///
/// Default value for `value` is `None` and `false` for `vocab` and `reverse`.
pub(crate) fn compact_iri_full<N, O>(
	vocabulary: &N,
	active_context: &Context<N::Iri, N::BlankId>,
	var: &Term<N::Iri, N::BlankId>,
	value: Option<&Indexed<O>>,
	vocab: bool,
	reverse: bool,
	options: Options,
) -> Result<Option<String>, IriConfusedWithPrefix>
where
	N: Vocabulary,
	N::Iri: Clone + Hash + Eq,
	N::BlankId: Clone + Hash + Eq,
	O: object::Any<N::Iri, N::BlankId>,
{
	if var.is_null() {
		return Ok(None);
	}

	if vocab {
		if let Some(entry) = active_context.inverse().get(var) {
			// Initialize containers to an empty array.
			// This array will be used to keep track of an ordered list of preferred container
			// mapping for a term, based on what is compatible with value.
			let mut containers = Vec::new();
			let mut type_lang_value = None;

			if let Some(value) = value {
				if value.index().is_some() && !value.is_graph() {
					containers.push(Container::Index);
					containers.push(Container::IndexSet);
				}
			}

			let mut has_index = false;
			let mut is_simple_value = false; // value object with no type, no index, no language and no direction.

			if reverse {
				type_lang_value = Some(TypeLangValue::Type(TypeSelection::Reverse));
				containers.push(Container::Set);
			} else {
				let value_ref = value.map(|v| {
					has_index = v.index().is_some();
					v.inner().as_ref()
				});

				match value_ref {
					Some(object::Ref::List(list)) => {
						if !has_index {
							containers.push(Container::List);
						}

						let mut common_type = None;
						let mut common_lang_dir = None;

						if list.is_empty() {
							common_lang_dir = Some(Nullable::Some((
								active_context.default_language(),
								active_context.default_base_direction(),
							)))
						} else {
							for item in list {
								let mut item_type = None;
								let mut item_lang_dir = None;
								let mut is_value = false;

								match item.inner() {
									Object::Value(value) => {
										is_value = true;
										match value {
											Value::LangString(lang_str) => {
												item_lang_dir = Some(Nullable::Some((
													lang_str.language(),
													lang_str.direction(),
												)))
											}
											Value::Literal(_, Some(ty)) => {
												item_type = Some(Type::Iri(ty.clone()))
											}
											Value::Literal(_, None) => {
												item_lang_dir = Some(Nullable::Null)
											}
											Value::Json(_) => item_type = Some(Type::Json),
										}
									}
									_ => item_type = Some(Type::Id),
								}

								if common_lang_dir.is_none() {
									common_lang_dir = item_lang_dir
								} else if is_value && common_lang_dir != item_lang_dir {
									common_lang_dir = Some(Nullable::Some((None, None)))
								}

								if common_type.is_none() {
									common_type = Some(item_type)
								} else if *common_type.as_ref().unwrap() != item_type {
									common_type = Some(None)
								}

								if common_lang_dir == Some(Nullable::Some((None, None)))
									&& common_type == Some(None)
								{
									break;
								}
							}
						}

						if common_lang_dir.is_none() {
							common_lang_dir = Some(Nullable::Some((None, None)))
						}
						let common_lang_dir = common_lang_dir.unwrap();

						if common_type.is_none() {
							common_type = Some(None)
						}
						let common_type = common_type.unwrap();

						if let Some(common_type) = common_type {
							type_lang_value =
								Some(TypeLangValue::Type(TypeSelection::Type(common_type)))
						} else {
							type_lang_value =
								Some(TypeLangValue::Lang(LangSelection::Lang(common_lang_dir)))
						}
					}
					Some(object::Ref::Node(node)) if node.is_graph() => {
						// Otherwise, if value is a graph object, prefer a mapping most
						// appropriate for the particular value.
						if has_index {
							// If value contains an @index entry, append the values
							// @graph@index and @graph@index@set to containers.
							containers.push(Container::GraphIndex);
							containers.push(Container::GraphIndexSet);
						}

						if node.id.is_some() {
							// If value contains an @id entry, append the values @graph@id and
							// @graph@id@set to containers.
							containers.push(Container::GraphId);
							containers.push(Container::GraphIdSet);
						}

						// Append the values @graph, @graph@set, and @set to containers.
						containers.push(Container::Graph);
						containers.push(Container::GraphSet);
						containers.push(Container::Set);

						if !has_index {
							// If value does not contain an @index entry, append the values
							// @graph@index and @graph@index@set to containers.
							containers.push(Container::GraphIndex);
							containers.push(Container::GraphIndexSet);
						}

						if node.id.is_none() {
							// If the value does not contain an @id entry, append the values
							// @graph@id and @graph@id@set to containers.
							containers.push(Container::GraphId);
							containers.push(Container::GraphIdSet);
						}

						// Append the values @index and @index@set to containers.
						containers.push(Container::Index);
						containers.push(Container::IndexSet);

						type_lang_value = Some(TypeLangValue::Type(TypeSelection::Type(Type::Id)))
					}
					Some(object::Ref::Value(v)) => {
						// If value is a value object:
						if (v.direction().is_some() || v.language().is_some()) && !has_index {
							type_lang_value = Some(TypeLangValue::Lang(LangSelection::Lang(
								Nullable::Some((v.language(), v.direction())),
							)));
							containers.push(Container::Language);
							containers.push(Container::LanguageSet)
						} else if let Some(ty) = v.typ() {
							type_lang_value = Some(TypeLangValue::Type(TypeSelection::Type(
								ty.as_syntax_type().cloned(),
							)))
						} else {
							is_simple_value =
								v.direction().is_none() && v.language().is_none() && !has_index
						}

						containers.push(Container::Set)
					}
					_ => {
						// Otherwise, set type/language to @type and set type/language value
						// to @id, and append @id, @id@set, @type, and @set@type, to containers.
						type_lang_value = Some(TypeLangValue::Type(TypeSelection::Type(Type::Id)));
						containers.push(Container::Id);
						containers.push(Container::IdSet);
						containers.push(Container::Type);
						containers.push(Container::SetType);

						containers.push(Container::Set)
					}
				}
			}

			containers.push(Container::None);

			if options.processing_mode != ProcessingMode::JsonLd1_0 && !has_index {
				containers.push(Container::Index);
				containers.push(Container::IndexSet)
			}

			if options.processing_mode != ProcessingMode::JsonLd1_0 && is_simple_value {
				containers.push(Container::Language);
				containers.push(Container::LanguageSet)
			}

			let mut is_empty_list = false;
			if let Some(value) = value {
				if let object::Ref::List(list) = value.inner().as_ref() {
					if list.is_empty() {
						is_empty_list = true;
					}
				}
			}

			// If type/language value is @reverse, append @reverse to preferred values.
			let selection = if is_empty_list {
				Selection::Any
			} else {
				match type_lang_value {
					Some(TypeLangValue::Type(type_value)) => {
						let mut selection: Vec<TypeSelection<N::Iri>> = Vec::new();

						if type_value == TypeSelection::Reverse {
							selection.push(TypeSelection::Reverse);
						}

						let mut has_id_type = false;
						if let Some(value) = value {
							if let Some(id) = value.id() {
								if type_value == TypeSelection::Type(Type::Id)
									|| type_value == TypeSelection::Reverse
								{
									has_id_type = true;
									let mut vocab = false;
									let compacted_iri = compact_iri(
										vocabulary,
										active_context,
										&id.clone().into_term(),
										true,
										false,
										options,
									)?
									.unwrap();
									if let Some(def) = active_context.get(compacted_iri.as_str()) {
										if let Some(iri_mapping) = def.value() {
											vocab = iri_mapping == id;
										}
									}

									if vocab {
										selection.push(TypeSelection::Type(Type::Vocab));
										selection.push(TypeSelection::Type(Type::Id));
									} else {
										selection.push(TypeSelection::Type(Type::Id));
										selection.push(TypeSelection::Type(Type::Vocab));
									}

									selection.push(TypeSelection::Type(Type::None));
								}
							}
						}

						if !has_id_type {
							selection.push(type_value);
							selection.push(TypeSelection::Type(Type::None));
						}

						selection.push(TypeSelection::Any);

						Selection::Type(selection)
					}
					Some(TypeLangValue::Lang(lang_value)) => {
						let mut selection = vec![
							lang_value,
							LangSelection::Lang(Nullable::Some((None, None))),
							LangSelection::Any,
						];

						if let LangSelection::Lang(Nullable::Some((Some(_), Some(dir)))) =
							lang_value
						{
							selection.push(LangSelection::Lang(Nullable::Some((None, Some(dir)))));
						}

						Selection::Lang(selection)
					}
					None => Selection::Lang(vec![
						LangSelection::Lang(Nullable::Null),
						LangSelection::Lang(Nullable::Some((None, None))),
						LangSelection::Any,
					]),
				}
			};

			if let Some(term) = entry.select(&containers, &selection) {
				return Ok(Some(term.to_string()));
			}
		}

		// At this point, there is no simple term that var can be compacted to.
		// If vocab is true and active context has a vocabulary mapping:
		if let Some(vocab_mapping) = active_context.vocabulary() {
			// If var begins with the vocabulary mapping's value but is longer, then initialize
			// suffix to the substring of var that does not match. If suffix does not have a term
			// definition in active context, then return suffix.
			if let Some(suffix) = var
				.with(vocabulary)
				.as_str()
				.strip_prefix(vocab_mapping.with(vocabulary).as_str())
			{
				if !suffix.is_empty() && active_context.get(suffix).is_none() {
					return Ok(Some(suffix.into()));
				}
			}
		}
	}

	// The var could not be compacted using the active context's vocabulary mapping.
	// Try to create a compact IRI, starting by initializing compact IRI to null.
	// This variable will be used to store the created compact IRI, if any.
	let mut compact_iri = String::new();

	// For each term definition definition in active context:
	for binding in active_context.definitions() {
		let key = binding.term();
		let definition = binding.definition();
		// If the IRI mapping of definition is null, its IRI mapping equals var,
		// its IRI mapping is not a substring at the beginning of var,
		// or definition does not have a true prefix flag,
		// definition's key cannot be used as a prefix.
		// Continue with the next definition.
		match definition.value() {
			Some(iri_mapping) if definition.prefix() => {
				if let Some(suffix) = var
					.with(vocabulary)
					.as_str()
					.strip_prefix(iri_mapping.with(vocabulary).as_str())
				{
					if !suffix.is_empty() {
						// Initialize candidate by concatenating definition key,
						// a colon (:),
						// and the substring of var that follows after the value of the definition's IRI mapping.
						let mut candidate = key.to_string();
						candidate.push(':');
						candidate.push_str(suffix);

						// If either compact IRI is null,
						// candidate is shorter or the same length but lexicographically less than
						// compact IRI and candidate does not have a term definition in active
						// context, or if that term definition has an IRI mapping that equals var
						// and value is null, set compact IRI to candidate.
						let candidate_def = active_context.get(candidate.as_str());
						if (compact_iri.is_empty()
							|| (candidate.len() <= compact_iri.len() && candidate < compact_iri))
							&& (candidate_def.is_none()
								|| (candidate_def.is_some()
									&& (candidate_def.and_then(|def| def.value()) == Some(var))
									&& value.is_none()))
						{
							compact_iri = candidate
						}
					}
				}
			}
			_ => (),
		}
	}

	// If compact IRI is not null, return compact IRI.
	if !compact_iri.is_empty() {
		return Ok(Some(compact_iri.as_str().into()));
	}

	// To ensure that the IRI var is not confused with a compact IRI,
	// if the IRI scheme of var matches any term in active context with prefix flag set to true,
	// and var has no IRI authority (preceded by double-forward-slash (//),
	// an IRI confused with prefix error has been detected, and processing is aborted.
	if let Some(iri) = var.as_iri() {
		let iri = vocabulary.iri(iri).unwrap();
		if active_context.contains_term(iri.scheme().as_str()) {
			return Err(IriConfusedWithPrefix);
		}
	}

	// If vocab is false,
	// transform var to a relative IRI reference using the base IRI from active context,
	// if it exists.
	if !vocab {
		if let Some(base_iri) = active_context.base_iri() {
			let base_iri = vocabulary.iri(base_iri).unwrap();
			if let Some(iri) = var.as_iri() {
				let iri = vocabulary.iri(iri).unwrap();
				return Ok(Some(disambiguate_keyword(
					iri.relative_to(base_iri).as_str().into(),
				)));
			}
		}
	}

	// Finally, return var as is.
	Ok(Some(var.with(vocabulary).to_string()))
}

fn disambiguate_keyword(s: String) -> String {
	if is_keyword_like(&s) && !is_keyword(&s) {
		"./".to_string() + &s
	} else {
		s
	}
}