json-ld 0.22.0

JSON-LD 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
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
use crate::object::{JsonLdObjectMut, JsonLdObjectRef};
use crate::syntax::JsonLdKeyword;
use crate::{JsonLdDirection, JsonLdLangString, JsonLdLenient, JsonLdType, JsonLdVisit, object};
use educe::Educe;
use json_syntax::{JsonNumber, JsonNumberBuf, JsonValue, ryu_js};
use langtag::LangTag;
use rdf_syntax::{IdRef, Iri, IriBuf};
use rdf_syntax::{Literal, RDF_JSON};
use std::hash::Hash;
use xsd_types::{XSD_BOOLEAN, XSD_FLOAT, XSD_INTEGER};

/// Value type.
pub enum JsonLdValueType {
	Json,
	Id(IriBuf),
}

impl JsonLdValueType {
	pub fn as_id(&self) -> Option<IdRef<'_>> {
		match self {
			Self::Json => None,
			Self::Id(t) => Some(IdRef::Iri(t)),
		}
	}
}

/// Value type reference.
#[derive(Educe)]
#[educe(Clone, Copy)]
pub enum JsonLdValueTypeRef<'a> {
	Json,
	Id(&'a Iri),
}

impl<'a> JsonLdValueTypeRef<'a> {
	pub fn into_object_type(self) -> JsonLdType {
		match self {
			Self::Json => JsonLdType::Json,
			Self::Id(id) => JsonLdType::Iri(id.to_owned()),
		}
	}

	pub fn into_id_ref(self) -> Option<IdRef<'a>> {
		match self {
			Self::Json => None,
			Self::Id(t) => Some(IdRef::Iri(t)),
		}
	}
}

/// Literal type.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum JsonLdLiteralType {
	/// JSON literal (`@json`).
	Json,

	/// Typed by an IRI.
	Iri(IriBuf),
}

impl JsonLdLiteralType {
	/// Returns this type IRI.
	///
	/// If the type is `@json`, this will return [`RDF_JSON`].
	pub fn iri(&self) -> &Iri {
		match self {
			Self::Json => RDF_JSON,
			Self::Iri(iri) => iri,
		}
	}

	/// Returns whether this is the JSON type.
	///
	/// Returns `true` if it is `@json` or the [`RDF_JSON`] IRI.
	pub fn is_json(&self) -> bool {
		match self {
			Self::Json => true,
			Self::Iri(iri) => iri == RDF_JSON,
		}
	}
}

#[cfg(feature = "serde")]
impl serde::Serialize for JsonLdLiteralType {
	fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
		match self {
			Self::Json => serializer.serialize_str("@json"),
			Self::Iri(iri) => serializer.serialize_str(iri.as_str()),
		}
	}
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for JsonLdLiteralType {
	fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
		let s = String::deserialize(deserializer)?;
		if s == "@json" {
			Ok(Self::Json)
		} else {
			IriBuf::new(s)
				.map(Self::Iri)
				.map_err(serde::de::Error::custom)
		}
	}
}

/// Literal value.
///
/// A JSON-LD value object with a `@value` entry and an optional `@type`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct JsonLdLiteralValue {
	/// The value.
	#[cfg_attr(feature = "serde", serde(rename = "@value"))]
	pub value: JsonValue,

	/// Optional type.
	#[cfg_attr(
		feature = "serde",
		serde(rename = "@type", default, skip_serializing_if = "Option::is_none")
	)]
	pub type_: Option<JsonLdLiteralType>,
}

impl JsonLdLiteralValue {
	/// Creates a new literal value.
	pub fn new(value: JsonValue, type_: Option<JsonLdLiteralType>) -> Self {
		Self { value, type_ }
	}

	/// Creates a null literal value.
	pub fn null() -> Self {
		Self::new(JsonValue::Null, None)
	}

	/// Creates a JSON literal value (`@type: "@json"`).
	pub fn json(value: JsonValue) -> Self {
		Self::new(value, Some(JsonLdLiteralType::Json))
	}

	/// Returns the value as a string if it is one.
	#[inline(always)]
	pub fn as_str(&self) -> Option<&str> {
		self.value.as_str().map(AsRef::as_ref)
	}

	/// Returns the value as a boolean if it is one.
	#[inline(always)]
	pub fn as_bool(&self) -> Option<bool> {
		self.value.as_boolean()
	}

	/// Returns the value as a number if it is one.
	#[inline(always)]
	pub fn as_number(&self) -> Option<&JsonNumber> {
		self.value.as_number()
	}

	/// Returns the type IRI.
	pub fn type_iri(&self) -> Option<&Iri> {
		match &self.type_ {
			Some(JsonLdLiteralType::Iri(iri)) => Some(iri),
			_ => None,
		}
	}

	/// Returns true if this is a JSON literal.
	pub fn is_json(&self) -> bool {
		self.type_.as_ref().is_some_and(JsonLdLiteralType::is_json)
	}

	/// Puts this literal into canonical form using the given `buffer`.
	///
	/// The buffer is used to compute the canonical form of numbers.
	pub fn canonicalize_with(&mut self, buffer: &mut ryu_js::Buffer) {
		if let JsonValue::Number(n) = &mut self.value {
			*n = JsonNumberBuf::from_number(n.canonicalized_with(buffer))
		}
	}

	/// Puts this literal into canonical form.
	pub fn canonicalize(&mut self) {
		let mut buffer = ryu_js::Buffer::new();
		self.canonicalize_with(&mut buffer)
	}
}

/// Value object.
///
/// Either a typed literal value, or an internationalized language string.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
pub enum JsonLdValue {
	/// Language tagged string.
	LangString(JsonLdLangString),

	/// Typed literal value (including JSON literals).
	Literal(JsonLdLiteralValue),
}

impl JsonLdValue {
	/// Creates a `null` value object.
	#[inline(always)]
	pub fn null() -> Self {
		Self::Literal(JsonLdLiteralValue::null())
	}

	#[inline(always)]
	pub fn as_str(&self) -> Option<&str> {
		match self {
			JsonLdValue::Literal(lit) => lit.as_str(),
			JsonLdValue::LangString(s) => Some(s.as_str()),
		}
	}

	#[inline(always)]
	pub fn as_literal(&self) -> Option<&JsonLdLiteralValue> {
		match self {
			Self::Literal(lit) => Some(lit),
			_ => None,
		}
	}

	pub fn literal_type(&self) -> Option<&JsonLdLiteralType> {
		match self {
			Self::Literal(lit) => lit.type_.as_ref(),
			_ => None,
		}
	}

	pub fn literal_type_iri(&self) -> Option<&Iri> {
		match self {
			Self::Literal(lit) => lit.type_iri(),
			_ => None,
		}
	}

	/// Set the literal value type, and returns the old type.
	///
	/// Has no effect and return `None` if the value is not a literal value.
	pub fn set_literal_type(&mut self, ty: Option<JsonLdLiteralType>) -> Option<JsonLdLiteralType> {
		match self {
			Self::Literal(lit) => std::mem::replace(&mut lit.type_, ty),
			_ => None,
		}
	}

	#[inline(always)]
	pub fn as_bool(&self) -> Option<bool> {
		match self {
			JsonLdValue::Literal(lit) => lit.as_bool(),
			_ => None,
		}
	}

	#[inline(always)]
	pub fn as_number(&self) -> Option<&JsonNumber> {
		match self {
			JsonLdValue::Literal(lit) => lit.as_number(),
			_ => None,
		}
	}

	/// Return the type of the value if any.
	pub fn typ(&self) -> Option<JsonLdValueTypeRef<'_>> {
		match self {
			JsonLdValue::Literal(lit) => match &lit.type_ {
				Some(JsonLdLiteralType::Json) => Some(JsonLdValueTypeRef::Json),
				Some(JsonLdLiteralType::Iri(iri)) => Some(JsonLdValueTypeRef::Id(iri)),
				None => None,
			},
			_ => None,
		}
	}

	/// If the value is a language tagged string, return its associated language if any.
	///
	/// Returns `None` if the value is not a language tagged string.
	#[inline(always)]
	pub fn language(&self) -> Option<JsonLdLenient<&LangTag>> {
		match self {
			JsonLdValue::LangString(tag) => tag.language(),
			_ => None,
		}
	}

	/// If the value is a language tagged string, return its associated direction if any.
	///
	/// Returns `None` if the value is not a language tagged string.
	#[inline(always)]
	pub fn direction(&self) -> Option<JsonLdDirection> {
		match self {
			JsonLdValue::LangString(s) => s.direction(),
			_ => None,
		}
	}

	/// Returns true if this is a JSON literal.
	pub fn is_json(&self) -> bool {
		matches!(self, Self::Literal(lit) if lit.is_json())
	}

	/// Returns the JSON value if this is a JSON literal.
	pub fn as_json(&self) -> Option<&JsonValue> {
		match self {
			Self::Literal(lit) if lit.is_json() => Some(&lit.value),
			_ => None,
		}
	}

	/// Puts this value object literal into canonical form using the given
	/// `buffer`.
	///
	/// The buffer is used to compute the canonical form of numbers.
	pub fn canonicalize_with(&mut self, buffer: &mut ryu_js::Buffer) {
		match self {
			Self::Literal(l) => l.canonicalize_with(buffer),
			Self::LangString(_) => (),
		}
	}

	/// Puts this literal into canonical form.
	pub fn canonicalize(&mut self) {
		let mut buffer = ryu_js::Buffer::new();
		self.canonicalize_with(&mut buffer)
	}

	#[inline(always)]
	pub fn entries(&self) -> JsonLdValueEntries<'_> {
		match self {
			Self::Literal(lit) => JsonLdValueEntries {
				value: Some(JsonLdValueValueEntryRef::Value(&lit.value)),
				type_: match &lit.type_ {
					Some(JsonLdLiteralType::Json) => Some(JsonLdValueTypeRef::Json),
					Some(JsonLdLiteralType::Iri(iri)) => Some(JsonLdValueTypeRef::Id(iri)),
					None => None,
				},
				language: None,
				direction: None,
			},
			Self::LangString(l) => JsonLdValueEntries {
				value: Some(JsonLdValueValueEntryRef::LangString(l.as_str())),
				type_: None,
				language: l.language(),
				direction: l.direction(),
			},
		}
	}
}

impl JsonLdVisit for JsonLdValue {
	fn visit_with(&self, f: &mut impl FnMut(JsonLdObjectRef)) {
		f(JsonLdObjectRef::Value(self))
	}

	fn visit_mut_with(&mut self, f: &mut impl FnMut(JsonLdObjectMut)) {
		f(JsonLdObjectMut::Value(self))
	}
}

impl object::AnyJsonLdObject for JsonLdValue {
	#[inline(always)]
	fn as_ref(&self) -> object::JsonLdObjectRef<'_> {
		object::JsonLdObjectRef::Value(self)
	}
}

impl From<Literal> for JsonLdValue {
	fn from(literal: Literal) -> Self {
		match literal.r#type {
			rdf_syntax::LiteralType::Any(ty) => {
				if ty == XSD_BOOLEAN {
					match literal.value.as_str() {
						"true" => {
							return Self::Literal(JsonLdLiteralValue::new(
								JsonValue::Boolean(true),
								Some(JsonLdLiteralType::Iri(ty)),
							));
						}
						"false" => {
							return Self::Literal(JsonLdLiteralValue::new(
								JsonValue::Boolean(false),
								Some(JsonLdLiteralType::Iri(ty)),
							));
						}
						_ => (),
					}
				} else if ty == XSD_INTEGER || ty == XSD_FLOAT {
					if let Ok(number) = literal.value.parse::<JsonNumberBuf>() {
						return Self::Literal(JsonLdLiteralValue::new(
							JsonValue::Number(number),
							Some(JsonLdLiteralType::Iri(ty)),
						));
					}
				} else if ty == RDF_JSON
					&& let Ok(json) = json_syntax::from_str(&literal.value)
				{
					return Self::Literal(JsonLdLiteralValue::json(json));
				}

				Self::Literal(JsonLdLiteralValue::new(
					JsonValue::String(literal.value.into()),
					Some(JsonLdLiteralType::Iri(ty)),
				))
			}
			rdf_syntax::LiteralType::LangString(langtag) => {
				Self::LangString(JsonLdLangString::from_language(literal.value, langtag))
			}
		}
	}
}

#[derive(Educe)]
#[educe(Clone, Copy)]
pub enum JsonLdValueEntryRef<'a> {
	Value(JsonLdValueValueEntryRef<'a>),
	Type(JsonLdValueTypeRef<'a>),
	Language(JsonLdLenient<&'a LangTag>),
	Direction(JsonLdDirection),
}

impl<'a> JsonLdValueEntryRef<'a> {
	pub fn into_key(self) -> JsonLdValueEntryKey {
		match self {
			Self::Value(_) => JsonLdValueEntryKey::Value,
			Self::Type(_) => JsonLdValueEntryKey::Type,
			Self::Language(_) => JsonLdValueEntryKey::Language,
			Self::Direction(_) => JsonLdValueEntryKey::Direction,
		}
	}

	pub fn key(&self) -> JsonLdValueEntryKey {
		self.into_key()
	}

	pub fn into_value(self) -> JsonLdValueEntryValueRef<'a> {
		match self {
			Self::Value(v) => JsonLdValueEntryValueRef::Value(v),
			Self::Type(v) => JsonLdValueEntryValueRef::Type(v),
			Self::Language(v) => JsonLdValueEntryValueRef::Language(v),
			Self::Direction(v) => JsonLdValueEntryValueRef::Direction(v),
		}
	}

	pub fn value(&self) -> JsonLdValueEntryValueRef<'a> {
		match self {
			Self::Value(v) => JsonLdValueEntryValueRef::Value(*v),
			Self::Type(v) => JsonLdValueEntryValueRef::Type(*v),
			Self::Language(v) => JsonLdValueEntryValueRef::Language(*v),
			Self::Direction(v) => JsonLdValueEntryValueRef::Direction(*v),
		}
	}
}

#[derive(Educe)]
#[educe(Clone, Copy)]
pub enum JsonLdValueEntryValueRef<'a> {
	Value(JsonLdValueValueEntryRef<'a>),
	Type(JsonLdValueTypeRef<'a>),
	Language(JsonLdLenient<&'a LangTag>),
	Direction(JsonLdDirection),
}

pub enum JsonLdValueValueEntryRef<'a> {
	Value(&'a JsonValue),
	LangString(&'a str),
}

impl<'a> Clone for JsonLdValueValueEntryRef<'a> {
	fn clone(&self) -> Self {
		*self
	}
}

impl<'a> Copy for JsonLdValueValueEntryRef<'a> {}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum JsonLdValueEntryKey {
	Value,
	Type,
	Language,
	Direction,
}

impl JsonLdValueEntryKey {
	pub fn into_keyword(self) -> JsonLdKeyword {
		match self {
			Self::Value => JsonLdKeyword::Value,
			Self::Type => JsonLdKeyword::Type,
			Self::Language => JsonLdKeyword::Language,
			Self::Direction => JsonLdKeyword::Direction,
		}
	}

	pub fn as_keyword(&self) -> JsonLdKeyword {
		self.into_keyword()
	}

	pub fn into_str(&self) -> &'static str {
		match self {
			Self::Value => "@value",
			Self::Type => "@type",
			Self::Language => "@language",
			Self::Direction => "@direction",
		}
	}

	pub fn as_str(&self) -> &'static str {
		self.into_str()
	}
}

#[derive(Educe)]
#[educe(Clone)]
pub struct JsonLdValueEntries<'a> {
	value: Option<JsonLdValueValueEntryRef<'a>>,
	type_: Option<JsonLdValueTypeRef<'a>>,
	language: Option<JsonLdLenient<&'a LangTag>>,
	direction: Option<JsonLdDirection>,
}

impl<'a> Iterator for JsonLdValueEntries<'a> {
	type Item = JsonLdValueEntryRef<'a>;

	fn size_hint(&self) -> (usize, Option<usize>) {
		let mut len = 0;

		if self.value.is_some() {
			len += 1
		}

		if self.type_.is_some() {
			len += 1
		}

		if self.language.is_some() {
			len += 1
		}

		if self.direction.is_some() {
			len += 1
		}

		(len, Some(len))
	}

	fn next(&mut self) -> Option<Self::Item> {
		self.value
			.take()
			.map(JsonLdValueEntryRef::Value)
			.or_else(|| {
				self.type_
					.take()
					.map(JsonLdValueEntryRef::Type)
					.or_else(|| {
						self.language
							.take()
							.map(JsonLdValueEntryRef::Language)
							.or_else(|| self.direction.take().map(JsonLdValueEntryRef::Direction))
					})
			})
	}
}

impl<'a> ExactSizeIterator for JsonLdValueEntries<'a> {}

impl<'a> DoubleEndedIterator for JsonLdValueEntries<'a> {
	fn next_back(&mut self) -> Option<Self::Item> {
		self.direction
			.take()
			.map(JsonLdValueEntryRef::Direction)
			.or_else(|| {
				self.language
					.take()
					.map(JsonLdValueEntryRef::Language)
					.or_else(|| {
						self.type_
							.take()
							.map(JsonLdValueEntryRef::Type)
							.or_else(|| self.value.take().map(JsonLdValueEntryRef::Value))
					})
			})
	}
}