1use btree_indexmap::BTreeIndexMultiSet;
3use educe::Educe;
4use json_syntax::JsonNumber;
5use langtag::LangTag;
6use rdf_syntax::Id;
7use rdf_syntax::Iri;
8use std::hash::Hash;
9
10use crate::syntax::JsonLdKeyword;
11use crate::{JsonLdIndexed, JsonLdLenient, JsonLdVisit};
12
13pub mod list;
14pub mod node;
15mod typ;
16pub mod value;
17
18pub use list::JsonLdList;
19pub use node::{JsonLdGraph, JsonLdIndexedNode, JsonLdNode, JsonLdNodes};
20pub use value::{JsonLdLiteralValue, JsonLdValue};
21
22use typ::JsonLdObjectTypeRef;
23
24pub trait AnyJsonLdObject {
26 fn as_ref(&self) -> JsonLdObjectRef<'_>;
27
28 #[inline]
29 fn id(&self) -> Option<&JsonLdLenient<Id>> {
30 match self.as_ref() {
31 JsonLdObjectRef::Node(n) => n.id.as_ref(),
32 _ => None,
33 }
34 }
35
36 #[inline]
37 fn language(&self) -> Option<JsonLdLenient<&LangTag>> {
38 match self.as_ref() {
39 JsonLdObjectRef::Value(value) => value.language(),
40 _ => None,
41 }
42 }
43
44 #[inline]
45 fn is_value(&self) -> bool {
46 matches!(self.as_ref(), JsonLdObjectRef::Value(_))
47 }
48
49 #[inline]
50 fn is_node(&self) -> bool {
51 matches!(self.as_ref(), JsonLdObjectRef::Node(_))
52 }
53
54 #[inline]
55 fn is_graph(&self) -> bool {
56 match self.as_ref() {
57 JsonLdObjectRef::Node(n) => n.is_graph(),
58 _ => false,
59 }
60 }
61
62 #[inline]
63 fn is_list(&self) -> bool {
64 matches!(self.as_ref(), JsonLdObjectRef::List(_))
65 }
66}
67
68pub enum JsonLdObjectRef<'a> {
70 Value(&'a JsonLdValue),
72
73 Node(&'a JsonLdNode),
75
76 List(&'a JsonLdList),
78}
79
80pub enum JsonLdObjectMut<'a> {
81 Value(&'a mut JsonLdValue),
82
83 Node(&'a mut JsonLdNode),
84
85 List(&'a mut JsonLdList),
86}
87
88impl<'a> JsonLdObjectMut<'a> {
89 pub fn reborrow(&mut self) -> JsonLdObjectMut<'_> {
90 match self {
91 Self::Value(v) => JsonLdObjectMut::Value(v),
92 Self::Node(n) => JsonLdObjectMut::Node(n),
93 Self::List(l) => JsonLdObjectMut::List(l),
94 }
95 }
96}
97
98pub type JsonLdIndexedObject = JsonLdIndexed<JsonLdObject>;
100
101#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
109#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
110#[cfg_attr(feature = "serde", serde(untagged))]
111pub enum JsonLdObject {
112 Value(JsonLdValue),
114
115 List(JsonLdList),
117
118 Node(Box<JsonLdNode>),
120}
121
122impl JsonLdObject {
123 #[inline(always)]
125 pub fn null() -> Self {
126 Self::Value(JsonLdValue::null())
127 }
128
129 #[inline(always)]
131 pub fn node(n: JsonLdNode) -> Self {
132 Self::Node(Box::new(n))
133 }
134
135 #[inline(always)]
137 pub fn id(&self) -> Option<&JsonLdLenient<Id>> {
138 match self {
139 JsonLdObject::Node(n) => n.id.as_ref(),
140 _ => None,
141 }
142 }
143
144 pub fn types(&self) -> JsonLdObjectTypes<'_> {
178 match self {
179 Self::Value(value) => JsonLdObjectTypes::Value(value.typ()),
180 Self::Node(node) => {
181 JsonLdObjectTypes::Node(node.types.as_ref().map(BTreeIndexMultiSet::iter))
182 }
183 Self::List(_) => JsonLdObjectTypes::List,
184 }
185 }
186
187 #[inline(always)]
192 pub fn as_iri(&self) -> Option<&Iri> {
193 match self {
194 JsonLdObject::Node(node) => node.as_iri(),
195 _ => None,
196 }
197 }
198
199 #[inline(always)]
201 pub fn is_value(&self) -> bool {
202 matches!(self, JsonLdObject::Value(_))
203 }
204
205 #[inline(always)]
207 pub fn as_value(&self) -> Option<&JsonLdValue> {
208 match self {
209 Self::Value(v) => Some(v),
210 _ => None,
211 }
212 }
213
214 #[inline(always)]
216 pub fn as_value_mut(&mut self) -> Option<&mut JsonLdValue> {
217 match self {
218 Self::Value(v) => Some(v),
219 _ => None,
220 }
221 }
222
223 #[inline(always)]
225 pub fn into_value(self) -> Option<JsonLdValue> {
226 match self {
227 Self::Value(v) => Some(v),
228 _ => None,
229 }
230 }
231
232 #[inline(always)]
234 pub fn is_node(&self) -> bool {
235 matches!(self, JsonLdObject::Node(_))
236 }
237
238 #[inline(always)]
240 pub fn as_node(&self) -> Option<&JsonLdNode> {
241 match self {
242 Self::Node(n) => Some(n),
243 _ => None,
244 }
245 }
246
247 #[inline(always)]
249 pub fn as_node_mut(&mut self) -> Option<&mut JsonLdNode> {
250 match self {
251 Self::Node(n) => Some(n),
252 _ => None,
253 }
254 }
255
256 #[inline(always)]
258 pub fn into_node(self) -> Option<JsonLdNode> {
259 match self {
260 Self::Node(n) => Some(*n),
261 _ => None,
262 }
263 }
264
265 #[inline(always)]
267 pub fn is_graph(&self) -> bool {
268 match self {
269 JsonLdObject::Node(n) => n.is_graph(),
270 _ => false,
271 }
272 }
273
274 #[inline(always)]
276 pub fn is_list(&self) -> bool {
277 matches!(self, JsonLdObject::List(_))
278 }
279
280 #[inline(always)]
282 pub fn as_list(&self) -> Option<&JsonLdList> {
283 match self {
284 Self::List(l) => Some(l),
285 _ => None,
286 }
287 }
288
289 #[inline(always)]
291 pub fn as_list_mut(&mut self) -> Option<&mut JsonLdList> {
292 match self {
293 Self::List(l) => Some(l),
294 _ => None,
295 }
296 }
297
298 #[inline(always)]
300 pub fn into_list(self) -> Option<JsonLdList> {
301 match self {
302 Self::List(l) => Some(l),
303 _ => None,
304 }
305 }
306
307 #[inline(always)]
313 pub fn as_str(&self) -> Option<&str> {
314 match self {
315 JsonLdObject::Value(value) => value.as_str(),
316 JsonLdObject::Node(node) => node.as_str(),
317 _ => None,
318 }
319 }
320
321 #[inline(always)]
323 pub fn as_bool(&self) -> Option<bool> {
324 match self {
325 JsonLdObject::Value(value) => value.as_bool(),
326 _ => None,
327 }
328 }
329
330 #[inline(always)]
332 pub fn as_number(&self) -> Option<&JsonNumber> {
333 match self {
334 JsonLdObject::Value(value) => value.as_number(),
335 _ => None,
336 }
337 }
338
339 #[inline(always)]
342 pub fn language(&self) -> Option<JsonLdLenient<&LangTag>> {
343 match self {
344 JsonLdObject::Value(value) => value.language(),
345 _ => None,
346 }
347 }
348
349 pub fn equivalent(&self, other: &Self) -> bool {
354 match (self, other) {
355 (Self::Value(a), Self::Value(b)) => a == b,
356 (Self::Node(a), Self::Node(b)) => a.equivalent(b),
357 _ => false,
358 }
359 }
360
361 pub fn entries(&self) -> Entries<'_> {
364 match self {
365 Self::Value(value) => Entries::Value(value.entries()),
366 Self::List(list) => Entries::List(Some(list.entry())),
367 Self::Node(node) => Entries::Node(node.entries()),
368 }
369 }
370}
371
372impl JsonLdVisit for JsonLdObject {
373 fn visit_with(&self, f: &mut impl FnMut(JsonLdObjectRef)) {
374 match self {
375 Self::Value(v) => v.visit_with(f),
376 Self::List(l) => l.visit_with(f),
377 Self::Node(n) => n.visit_with(f),
378 }
379 }
380
381 fn visit_mut_with(&mut self, f: &mut impl FnMut(JsonLdObjectMut)) {
382 match self {
383 Self::Value(v) => v.visit_mut_with(f),
384 Self::List(l) => l.visit_mut_with(f),
385 Self::Node(n) => n.visit_mut_with(f),
386 }
387 }
388}
389
390impl JsonLdIndexed<JsonLdObject> {
391 pub fn equivalent(&self, other: &Self) -> bool {
392 self.index() == other.index() && self.inner().equivalent(other.inner())
393 }
394
395 #[inline(always)]
397 pub fn into_indexed_node(self) -> Option<JsonLdIndexed<JsonLdNode>> {
398 let (object, index) = self.into_parts();
399 object
400 .into_node()
401 .map(|node| JsonLdIndexed::new(node, index))
402 }
403
404 #[inline(always)]
406 pub fn into_indexed_value(self) -> Option<JsonLdIndexed<JsonLdValue>> {
407 let (object, index) = self.into_parts();
408 object
409 .into_value()
410 .map(|value| JsonLdIndexed::new(value, index))
411 }
412
413 #[inline(always)]
415 pub fn into_indexed_list(self) -> Option<JsonLdIndexed<JsonLdList>> {
416 let (object, index) = self.into_parts();
417 object
418 .into_list()
419 .map(|list| JsonLdIndexed::new(list, index))
420 }
421
422 pub fn into_unnamed_graph(self) -> Result<JsonLdGraph, Self> {
424 let (obj, index) = self.into_parts();
425 match obj {
426 JsonLdObject::Node(n) => match n.into_unnamed_graph() {
427 Ok(g) => Ok(g),
428 Err(n) => Err(JsonLdIndexed::new(JsonLdObject::node(n), index)),
429 },
430 obj => Err(JsonLdIndexed::new(obj, index)),
431 }
432 }
433
434 pub fn entries(&self) -> IndexedEntries<'_> {
435 IndexedEntries {
436 index: self.index(),
437 inner: self.inner().entries(),
438 }
439 }
440}
441
442#[derive(Educe)]
443#[educe(Clone)]
444pub enum Entries<'a> {
445 Value(value::JsonLdValueEntries<'a>),
446 List(Option<&'a [JsonLdIndexedObject]>),
447 Node(node::JsonLdNodeEntries<'a>),
448}
449
450impl<'a> Iterator for Entries<'a> {
451 type Item = JsonLdObjectEntryRef<'a>;
452
453 fn size_hint(&self) -> (usize, Option<usize>) {
454 let len = match self {
455 Self::Value(v) => v.len(),
456 Self::List(l) => usize::from(l.is_some()),
457 Self::Node(n) => n.len(),
458 };
459
460 (len, Some(len))
461 }
462
463 fn next(&mut self) -> Option<Self::Item> {
464 match self {
465 Self::Value(v) => v.next().map(JsonLdObjectEntryRef::Value),
466 Self::List(l) => l.take().map(JsonLdObjectEntryRef::List),
467 Self::Node(n) => n.next().map(JsonLdObjectEntryRef::Node),
468 }
469 }
470}
471
472impl<'a> ExactSizeIterator for Entries<'a> {}
473
474#[derive(Educe)]
475#[educe(Clone)]
476pub struct IndexedEntries<'a> {
477 index: Option<&'a str>,
478 inner: Entries<'a>,
479}
480
481impl<'a> Iterator for IndexedEntries<'a> {
482 type Item = JsonLdIndexedObjectEntryRef<'a>;
483
484 fn size_hint(&self) -> (usize, Option<usize>) {
485 let len = self.inner.len() + usize::from(self.index.is_some());
486 (len, Some(len))
487 }
488
489 fn next(&mut self) -> Option<Self::Item> {
490 self.index
491 .take()
492 .map(JsonLdIndexedObjectEntryRef::Index)
493 .or_else(|| self.inner.next().map(JsonLdIndexedObjectEntryRef::Object))
494 }
495}
496
497impl<'a> ExactSizeIterator for IndexedEntries<'a> {}
498
499#[derive(Educe, PartialEq, Eq)]
500#[educe(Clone, Copy)]
501pub enum JsonLdObjectEntryKeyRef<'a> {
502 Value(value::JsonLdValueEntryKey),
503 List,
504 Node(node::JsonLdNodeEntryKeyRef<'a>),
505}
506
507impl<'a> JsonLdObjectEntryKeyRef<'a> {
508 pub fn into_keyword(self) -> Option<JsonLdKeyword> {
509 match self {
510 Self::Value(e) => Some(e.into_keyword()),
511 Self::List => Some(JsonLdKeyword::List),
512 Self::Node(e) => e.into_keyword(),
513 }
514 }
515
516 pub fn as_keyword(&self) -> Option<JsonLdKeyword> {
517 self.into_keyword()
518 }
519
520 pub fn into_str(self) -> &'a str {
521 match self {
522 Self::Value(e) => e.into_str(),
523 Self::List => "@list",
524 Self::Node(e) => e.into_str(),
525 }
526 }
527
528 pub fn as_str(self) -> &'a str {
529 self.into_str()
530 }
531}
532
533#[derive(Educe)]
534#[educe(Clone, Copy)]
535pub enum JsonLdObjectEntryValueRef<'a> {
536 Value(value::JsonLdValueEntryRef<'a>),
537 List(&'a [JsonLdIndexedObject]),
538 Node(node::JsonLdNodeEntryValueRef<'a>),
539}
540
541#[derive(Educe)]
542#[educe(Clone, Copy)]
543pub enum JsonLdObjectEntryRef<'a> {
544 Value(value::JsonLdValueEntryRef<'a>),
545 List(&'a [JsonLdIndexedObject]),
546 Node(node::JsonLdNodeEntryRef<'a>),
547}
548
549impl<'a> JsonLdObjectEntryRef<'a> {
550 pub fn into_key(self) -> JsonLdObjectEntryKeyRef<'a> {
551 match self {
552 Self::Value(e) => JsonLdObjectEntryKeyRef::Value(e.key()),
553 Self::List(_) => JsonLdObjectEntryKeyRef::List,
554 Self::Node(e) => JsonLdObjectEntryKeyRef::Node(e.key()),
555 }
556 }
557
558 pub fn key(&self) -> JsonLdObjectEntryKeyRef<'a> {
559 self.into_key()
560 }
561
562 pub fn into_value(self) -> JsonLdObjectEntryValueRef<'a> {
563 match self {
564 Self::Value(v) => JsonLdObjectEntryValueRef::Value(v),
565 Self::List(v) => JsonLdObjectEntryValueRef::List(v),
566 Self::Node(e) => JsonLdObjectEntryValueRef::Node(e.value()),
567 }
568 }
569
570 pub fn value(&self) -> JsonLdObjectEntryValueRef<'a> {
571 self.into_value()
572 }
573
574 pub fn into_key_value(self) -> (JsonLdObjectEntryKeyRef<'a>, JsonLdObjectEntryValueRef<'a>) {
575 match self {
576 Self::Value(e) => (
577 JsonLdObjectEntryKeyRef::Value(e.key()),
578 JsonLdObjectEntryValueRef::Value(e),
579 ),
580 Self::List(e) => (
581 JsonLdObjectEntryKeyRef::List,
582 JsonLdObjectEntryValueRef::List(e),
583 ),
584 Self::Node(e) => {
585 let (k, v) = e.into_key_value();
586 (
587 JsonLdObjectEntryKeyRef::Node(k),
588 JsonLdObjectEntryValueRef::Node(v),
589 )
590 }
591 }
592 }
593
594 pub fn as_key_value(&self) -> (JsonLdObjectEntryKeyRef<'a>, JsonLdObjectEntryValueRef<'a>) {
595 self.into_key_value()
596 }
597}
598
599#[derive(Educe, PartialEq, Eq)]
600#[educe(Clone, Copy)]
601pub enum JsonLdIndexedObjectEntryKeyRef<'a> {
602 Index,
603 Object(JsonLdObjectEntryKeyRef<'a>),
604}
605
606impl<'a> JsonLdIndexedObjectEntryKeyRef<'a> {
607 pub fn into_keyword(self) -> Option<JsonLdKeyword> {
608 match self {
609 Self::Index => Some(JsonLdKeyword::Index),
610 Self::Object(e) => e.into_keyword(),
611 }
612 }
613
614 pub fn as_keyword(&self) -> Option<JsonLdKeyword> {
615 self.into_keyword()
616 }
617
618 pub fn into_str(self) -> &'a str {
619 match self {
620 Self::Index => "@index",
621 Self::Object(e) => e.into_str(),
622 }
623 }
624
625 pub fn as_str(&self) -> &'a str {
626 self.into_str()
627 }
628}
629
630#[derive(Educe)]
631#[educe(Clone, Copy)]
632pub enum JsonLdIndexedObjectEntryValueRef<'a> {
633 Index(&'a str),
634 Object(JsonLdObjectEntryValueRef<'a>),
635}
636
637#[derive(Educe)]
638#[educe(Clone, Copy)]
639pub enum JsonLdIndexedObjectEntryRef<'a> {
640 Index(&'a str),
641 Object(JsonLdObjectEntryRef<'a>),
642}
643
644impl<'a> JsonLdIndexedObjectEntryRef<'a> {
645 pub fn into_key(self) -> JsonLdIndexedObjectEntryKeyRef<'a> {
646 match self {
647 Self::Index(_) => JsonLdIndexedObjectEntryKeyRef::Index,
648 Self::Object(e) => JsonLdIndexedObjectEntryKeyRef::Object(e.key()),
649 }
650 }
651
652 pub fn key(&self) -> JsonLdIndexedObjectEntryKeyRef<'a> {
653 self.into_key()
654 }
655
656 pub fn into_value(self) -> JsonLdIndexedObjectEntryValueRef<'a> {
657 match self {
658 Self::Index(v) => JsonLdIndexedObjectEntryValueRef::Index(v),
659 Self::Object(e) => JsonLdIndexedObjectEntryValueRef::Object(e.value()),
660 }
661 }
662
663 pub fn value(&self) -> JsonLdIndexedObjectEntryValueRef<'a> {
664 self.into_value()
665 }
666
667 pub fn into_key_value(
668 self,
669 ) -> (
670 JsonLdIndexedObjectEntryKeyRef<'a>,
671 JsonLdIndexedObjectEntryValueRef<'a>,
672 ) {
673 match self {
674 Self::Index(v) => (
675 JsonLdIndexedObjectEntryKeyRef::Index,
676 JsonLdIndexedObjectEntryValueRef::Index(v),
677 ),
678 Self::Object(e) => {
679 let (k, v) = e.into_key_value();
680 (
681 JsonLdIndexedObjectEntryKeyRef::Object(k),
682 JsonLdIndexedObjectEntryValueRef::Object(v),
683 )
684 }
685 }
686 }
687
688 pub fn as_key_value(
689 &self,
690 ) -> (
691 JsonLdIndexedObjectEntryKeyRef<'a>,
692 JsonLdIndexedObjectEntryValueRef<'a>,
693 ) {
694 self.into_key_value()
695 }
696}
697
698impl AnyJsonLdObject for JsonLdObject {
699 #[inline(always)]
700 fn as_ref(&self) -> JsonLdObjectRef<'_> {
701 match self {
702 JsonLdObject::Value(value) => JsonLdObjectRef::Value(value),
703 JsonLdObject::Node(node) => JsonLdObjectRef::Node(node),
704 JsonLdObject::List(list) => JsonLdObjectRef::List(list),
705 }
706 }
707}
708
709impl From<JsonLdValue> for JsonLdObject {
710 #[inline(always)]
711 fn from(value: JsonLdValue) -> Self {
712 Self::Value(value)
713 }
714}
715
716impl From<JsonLdNode> for JsonLdObject {
717 #[inline(always)]
718 fn from(node: JsonLdNode) -> Self {
719 Self::node(node)
720 }
721}
722
723pub enum JsonLdObjectTypes<'a> {
725 Value(Option<value::JsonLdValueTypeRef<'a>>),
726 Node(Option<btree_indexmap::multi_set::Iter<'a, JsonLdLenient<Id>>>),
727 List,
728}
729
730impl<'a> Iterator for JsonLdObjectTypes<'a> {
731 type Item = JsonLdObjectTypeRef<'a>;
732
733 fn next(&mut self) -> Option<Self::Item> {
734 match self {
735 Self::Value(ty) => ty.take().map(JsonLdObjectTypeRef::from_value_type),
736 Self::Node(tys) => tys.as_mut()?.next().map(JsonLdObjectTypeRef::from_id),
737 Self::List => None,
738 }
739 }
740}
741
742pub struct JsonLdObjects<'a>(Option<std::slice::Iter<'a, JsonLdIndexedObject>>);
744
745impl<'a> JsonLdObjects<'a> {
746 #[inline(always)]
747 pub(crate) fn new(inner: Option<std::slice::Iter<'a, JsonLdIndexedObject>>) -> Self {
748 Self(inner)
749 }
750}
751
752impl<'a> Iterator for JsonLdObjects<'a> {
753 type Item = &'a JsonLdIndexedObject;
754
755 #[inline(always)]
756 fn next(&mut self) -> Option<&'a JsonLdIndexedObject> {
757 match &mut self.0 {
758 None => None,
759 Some(it) => it.next(),
760 }
761 }
762}