json_ld_core/deserialization/object/
node.rs

1use linked_data::{
2	LinkedData, LinkedDataGraph, LinkedDataPredicateObjects, LinkedDataResource, LinkedDataSubject,
3	ResourceInterpretation,
4};
5use rdf_types::{vocabulary::IriVocabularyMut, Interpretation, Vocabulary};
6
7use crate::{rdf::RDF_TYPE, IndexedNode, IndexedObject, Node};
8
9impl<T, B, V: Vocabulary, I: Interpretation> LinkedDataResource<I, V> for Node<T, B>
10where
11	T: LinkedDataResource<I, V>,
12	B: LinkedDataResource<I, V>,
13{
14	fn interpretation(
15		&self,
16		vocabulary: &mut V,
17		interpretation: &mut I,
18	) -> ResourceInterpretation<I, V> {
19		match &self.id {
20			Some(crate::Id::Valid(id)) => id.interpretation(vocabulary, interpretation),
21			_ => ResourceInterpretation::Uninterpreted(None),
22		}
23	}
24}
25
26impl<T, B, V: Vocabulary<Iri = T>, I: Interpretation> LinkedDataSubject<I, V> for Node<T, B>
27where
28	T: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
29	B: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
30	V: IriVocabularyMut,
31{
32	fn visit_subject<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
33	where
34		S: linked_data::SubjectVisitor<I, V>,
35	{
36		if !self.types().is_empty() {
37			visitor.predicate(RDF_TYPE, &Types(self.types()))?;
38		}
39
40		for (property, objects) in self.properties() {
41			if let crate::Id::Valid(id) = property {
42				visitor.predicate(id, &Objects(objects))?;
43			}
44		}
45
46		if let Some(reverse_properties) = self.reverse_properties() {
47			for (property, nodes) in reverse_properties {
48				if let crate::Id::Valid(id) = property {
49					visitor.reverse_predicate(id, &Nodes(nodes))?;
50				}
51			}
52		}
53
54		if self.is_graph() {
55			visitor.graph(self)?;
56		}
57
58		if let Some(included) = self.included() {
59			for node in included {
60				visitor.include(node.inner())?;
61			}
62		}
63
64		visitor.end()
65	}
66}
67
68impl<T, B, V: Vocabulary<Iri = T>, I: Interpretation> LinkedDataPredicateObjects<I, V>
69	for Node<T, B>
70where
71	T: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
72	B: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
73	V: IriVocabularyMut,
74{
75	fn visit_objects<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
76	where
77		S: linked_data::PredicateObjectsVisitor<I, V>,
78	{
79		visitor.object(self)?;
80		visitor.end()
81	}
82}
83
84impl<T, B, V: Vocabulary<Iri = T>, I: Interpretation> LinkedDataGraph<I, V> for Node<T, B>
85where
86	T: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
87	B: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
88	V: IriVocabularyMut,
89{
90	fn visit_graph<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
91	where
92		S: linked_data::GraphVisitor<I, V>,
93	{
94		match self.graph() {
95			Some(g) => {
96				for object in g.iter() {
97					visitor.subject(object.inner())?;
98				}
99			}
100			None => {
101				visitor.subject(self)?;
102			}
103		}
104
105		visitor.end()
106	}
107}
108
109impl<T, B, V: Vocabulary<Iri = T>, I: Interpretation> LinkedData<I, V> for Node<T, B>
110where
111	T: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
112	B: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
113	V: IriVocabularyMut,
114{
115	fn visit<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
116	where
117		S: linked_data::Visitor<I, V>,
118	{
119		if self.is_graph() {
120			visitor.named_graph(self)?;
121		} else {
122			visitor.default_graph(self)?;
123		}
124
125		visitor.end()
126	}
127}
128
129struct Types<'a, T, B>(&'a [crate::Id<T, B>]);
130
131impl<'a, T, B, V: Vocabulary<Iri = T>, I: Interpretation> LinkedDataPredicateObjects<I, V>
132	for Types<'a, T, B>
133where
134	T: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
135	B: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
136	V: IriVocabularyMut,
137{
138	fn visit_objects<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
139	where
140		S: linked_data::PredicateObjectsVisitor<I, V>,
141	{
142		for ty in self.0 {
143			if let crate::Id::Valid(id) = ty {
144				visitor.object(id)?;
145			}
146		}
147
148		visitor.end()
149	}
150}
151
152struct Objects<'a, T, B>(&'a [IndexedObject<T, B>]);
153
154impl<'a, T, B, V: Vocabulary<Iri = T>, I: Interpretation> LinkedDataPredicateObjects<I, V>
155	for Objects<'a, T, B>
156where
157	T: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
158	B: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
159	V: IriVocabularyMut,
160{
161	fn visit_objects<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
162	where
163		S: linked_data::PredicateObjectsVisitor<I, V>,
164	{
165		for object in self.0 {
166			visitor.object(object.inner())?;
167		}
168
169		visitor.end()
170	}
171}
172
173struct Nodes<'a, T, B>(&'a [IndexedNode<T, B>]);
174
175impl<'a, T, B, V: Vocabulary<Iri = T>, I: Interpretation> LinkedDataPredicateObjects<I, V>
176	for Nodes<'a, T, B>
177where
178	T: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
179	B: LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
180	V: IriVocabularyMut,
181{
182	fn visit_objects<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
183	where
184		S: linked_data::PredicateObjectsVisitor<I, V>,
185	{
186		for node in self.0 {
187			visitor.object(node.inner())?;
188		}
189
190		visitor.end()
191	}
192}