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
use crate::{
	context::{
		self,
		inverse::{Inversible, LangSelection, TypeSelection},
		Loader, Local,
	},
	object,
	syntax::{ContainerType, Keyword, Term},
	util::AsJson,
	ContextMut, Error, Id, Indexed, Object, ProcessingMode, Value,
};
use futures::future::{BoxFuture, FutureExt};
use json::JsonValue;
use std::collections::HashSet;

mod iri;
mod node;
mod property;
mod value;

pub(crate) use iri::*;
use node::*;
use property::*;
use value::*;

#[derive(Clone, Copy)]
pub struct Options {
	pub processing_mode: ProcessingMode,
	pub compact_to_relative: bool,
	pub compact_arrays: bool,
	pub ordered: bool,
}

impl From<Options> for context::ProcessingOptions {
	fn from(options: Options) -> context::ProcessingOptions {
		context::ProcessingOptions {
			processing_mode: options.processing_mode,
			..Default::default()
		}
	}
}

impl From<crate::expansion::Options> for Options {
	fn from(options: crate::expansion::Options) -> Options {
		Options {
			processing_mode: options.processing_mode,
			ordered: options.ordered,
			..Options::default()
		}
	}
}

impl Default for Options {
	fn default() -> Options {
		Options {
			processing_mode: ProcessingMode::default(),
			compact_to_relative: true,
			compact_arrays: true,
			ordered: false,
		}
	}
}

pub trait Compact<T: Id> {
	fn compact_with<'a, C: ContextMut<T>, L: Loader>(
		&'a self,
		active_context: Inversible<T, &'a C>,
		type_scoped_context: Inversible<T, &'a C>,
		active_property: Option<&'a str>,
		loader: &'a mut L,
		options: Options,
	) -> BoxFuture<'a, Result<JsonValue, Error>>
	where
		T: 'a,
		C: Sync + Send,
		C::LocalContext: Send + Sync + From<L::Output>,
		L: Sync + Send;

	fn compact<'a, C: ContextMut<T>, L: Loader>(
		&'a self,
		active_context: Inversible<T, &'a C>,
		loader: &'a mut L,
	) -> BoxFuture<'a, Result<JsonValue, Error>>
	where
		Self: Sync,
		T: 'a + Sync + Send,
		C: Sync + Send,
		C::LocalContext: Send + Sync + From<L::Output>,
		L: Sync + Send,
	{
		async move {
			self.compact_with(
				active_context.clone(),
				active_context,
				None,
				loader,
				Options::default(),
			)
			.await
		}
		.boxed()
	}
}

enum TypeLangValue<'a, T: Id> {
	Type(TypeSelection<T>),
	Lang(LangSelection<'a>),
}

pub trait CompactIndexed<T: Id> {
	fn compact_indexed_with<'a, C: ContextMut<T>, L: Loader>(
		&'a self,
		index: Option<&'a str>,
		active_context: Inversible<T, &'a C>,
		type_scoped_context: Inversible<T, &'a C>,
		active_property: Option<&'a str>,
		loader: &'a mut L,
		options: Options,
	) -> BoxFuture<'a, Result<JsonValue, Error>>
	where
		T: 'a,
		C: Sync + Send,
		C::LocalContext: Send + Sync + From<L::Output>,
		L: Sync + Send;
}

impl<T: Sync + Send + Id, V: Sync + Send + CompactIndexed<T>> Compact<T> for Indexed<V> {
	fn compact_with<'a, C: ContextMut<T>, L: Loader>(
		&'a self,
		active_context: Inversible<T, &'a C>,
		type_scoped_context: Inversible<T, &'a C>,
		active_property: Option<&'a str>,
		loader: &'a mut L,
		options: Options,
	) -> BoxFuture<'a, Result<JsonValue, Error>>
	where
		T: 'a,
		C: Sync + Send,
		C::LocalContext: Send + Sync + From<L::Output>,
		L: Sync + Send,
	{
		self.inner().compact_indexed_with(
			self.index(),
			active_context,
			type_scoped_context,
			active_property,
			loader,
			options,
		)
	}
}

impl<T: Sync + Send + Id, N: object::Any<T> + Sync + Send> CompactIndexed<T> for N {
	fn compact_indexed_with<'a, C: ContextMut<T>, L: Loader>(
		&'a self,
		index: Option<&'a str>,
		active_context: Inversible<T, &'a C>,
		type_scoped_context: Inversible<T, &'a C>,
		active_property: Option<&'a str>,
		loader: &'a mut L,
		options: Options,
	) -> BoxFuture<'a, Result<JsonValue, Error>>
	where
		T: 'a,
		C: Sync + Send,
		C::LocalContext: Send + Sync + From<L::Output>,
		L: Sync + Send,
	{
		match self.as_ref() {
			object::Ref::Value(value) => async move {
				compact_indexed_value_with(
					value,
					index,
					active_context,
					active_property,
					loader,
					options,
				)
				.await
			}
			.boxed(),
			object::Ref::Node(node) => async move {
				compact_indexed_node_with(
					node,
					index,
					active_context,
					type_scoped_context,
					active_property,
					loader,
					options,
				)
				.await
			}
			.boxed(),
			object::Ref::List(list) => async move {
				let mut active_context = active_context;
				// If active context has a previous context, the active context is not propagated.
				// If element does not contain an @value entry, and element does not consist of
				// a single @id entry, set active context to previous context from active context,
				// as the scope of a term-scoped context does not apply when processing new node objects.
				if let Some(previous_context) = active_context.previous_context() {
					active_context = Inversible::new(previous_context)
				}

				// If the term definition for active property in active context has a local context:
				// FIXME https://github.com/w3c/json-ld-api/issues/502
				//       Seems that the term definition should be looked up in `type_scoped_context`.
				let mut active_context = active_context.into_borrowed();
				let mut list_container = false;
				if let Some(active_property) = active_property {
					if let Some(active_property_definition) =
						type_scoped_context.get(active_property)
					{
						if let Some(local_context) = &active_property_definition.context {
							active_context = Inversible::new(
								local_context
									.process_with(
										*active_context.as_ref(),
										loader,
										active_property_definition.base_url(),
										context::ProcessingOptions::from(options).with_override(),
									)
									.await?
									.into_inner(),
							)
							.into_owned()
						}

						list_container = active_property_definition
							.container
							.contains(ContainerType::List);
					}
				}

				if list_container {
					compact_collection_with(
						list.iter(),
						active_context.as_ref(),
						active_context.as_ref(),
						active_property,
						loader,
						options,
					)
					.await
				} else {
					let mut result = json::object::Object::new();
					compact_property(
						&mut result,
						Term::Keyword(Keyword::List),
						list,
						active_context.as_ref(),
						loader,
						false,
						options,
					)
					.await?;

					// If expanded property is @index and active property has a container mapping in
					// active context that includes @index,
					if let Some(index) = index {
						let mut index_container = false;
						if let Some(active_property) = active_property {
							if let Some(active_property_definition) =
								active_context.get(active_property)
							{
								if active_property_definition
									.container
									.contains(ContainerType::Index)
								{
									// then the compacted result will be inside of an @index container,
									// drop the @index entry by continuing to the next expanded property.
									index_container = true;
								}
							}
						}

						if !index_container {
							// Initialize alias by IRI compacting expanded property.
							let alias = compact_iri(
								active_context.as_ref(),
								&Term::Keyword(Keyword::Index),
								true,
								false,
								options,
							)?;

							// Add an entry alias to result whose value is set to expanded value and continue with the next expanded property.
							result.insert(alias.as_str().unwrap(), index.as_json());
						}
					}

					Ok(JsonValue::Object(result))
				}
			}
			.boxed(),
		}
	}
}

/// Default value of `as_array` is false.
fn add_value(map: &mut json::object::Object, key: &str, value: JsonValue, as_array: bool) {
	match map.get(key) {
		Some(JsonValue::Array(_)) => (),
		Some(original_value) => {
			let value = original_value.clone();
			map.insert(key, JsonValue::Array(vec![value]))
		}
		None if as_array => map.insert(key, JsonValue::Array(Vec::new())),
		None => (),
	}

	match value {
		JsonValue::Array(values) => {
			for value in values {
				add_value(map, key, value, false)
			}
		}
		value => match map.get_mut(key) {
			Some(JsonValue::Array(values)) => values.push(value),
			Some(_) => unreachable!(),
			None => map.insert(key, value),
		},
	}
}

/// Get the `@value` field of a value object.
fn value_value<T: Id>(value: &Value<T>) -> JsonValue {
	use crate::object::value::Literal;
	match value {
		Value::Literal(lit, _ty) => match lit {
			Literal::Null => JsonValue::Null,
			Literal::Boolean(b) => b.as_json(),
			Literal::Number(n) => JsonValue::Number(*n),
			Literal::String(s) => s.as_json(),
		},
		Value::LangString(str) => str.as_str().into(),
		Value::Json(json) => json.clone(),
	}
}

fn compact_collection_with<
	'a,
	T: 'a + Sync + Send + Id,
	O: 'a + Send + Iterator<Item = &'a Indexed<Object<T>>>,
	C: ContextMut<T>,
	L: Loader,
>(
	items: O,
	active_context: Inversible<T, &'a C>,
	type_scoped_context: Inversible<T, &'a C>,
	active_property: Option<&'a str>,
	loader: &'a mut L,
	options: Options,
) -> BoxFuture<'a, Result<JsonValue, Error>>
where
	C: Sync + Send,
	C::LocalContext: Send + Sync + From<L::Output>,
	L: Sync + Send,
{
	async move {
		let mut result = Vec::new();

		for item in items {
			match item
				.compact_with(
					active_context.clone(),
					type_scoped_context.clone(),
					active_property,
					loader,
					options,
				)
				.await?
			{
				JsonValue::Null => (),
				compacted_item => result.push(compacted_item),
			}
		}

		let mut list_or_set = false;
		if let Some(active_property) = active_property {
			if let Some(active_property_definition) = active_context.get(active_property) {
				list_or_set = active_property_definition
					.container
					.contains(ContainerType::List)
					|| active_property_definition
						.container
						.contains(ContainerType::Set);
			}
		}

		if result.is_empty()
			|| result.len() > 1
			|| !options.compact_arrays
			|| active_property == Some("@graph")
			|| active_property == Some("@set")
			|| list_or_set
		{
			return Ok(JsonValue::Array(result));
		}

		Ok(result.into_iter().next().unwrap())
	}
	.boxed()
}

impl<T: Sync + Send + Id> Compact<T> for HashSet<Indexed<Object<T>>> {
	fn compact_with<'a, C: ContextMut<T>, L: Loader>(
		&'a self,
		active_context: Inversible<T, &'a C>,
		type_scoped_context: Inversible<T, &'a C>,
		active_property: Option<&'a str>,
		loader: &'a mut L,
		options: Options,
	) -> BoxFuture<'a, Result<JsonValue, Error>>
	where
		T: 'a,
		C: Sync + Send,
		C::LocalContext: Send + Sync + From<L::Output>,
		L: Sync + Send,
	{
		compact_collection_with(
			self.iter(),
			active_context,
			type_scoped_context,
			active_property,
			loader,
			options,
		)
	}
}