1use iref::{Iri, IriBuf};
2use rdf_types::{Interpretation, Vocabulary};
3
4use crate::{LinkedData, LinkedDataPredicateObjects, LinkedDataResource, LinkedDataSubject};
5
6pub trait LinkedDataGraph<I: Interpretation, V: Vocabulary> {
10 fn visit_graph<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
11 where
12 S: GraphVisitor<I, V>;
13}
14
15impl<I: Interpretation, V: Vocabulary> LinkedDataGraph<I, V> for () {
16 fn visit_graph<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
17 where
18 S: GraphVisitor<I, V>,
19 {
20 visitor.end()
21 }
22}
23
24impl<I: Interpretation, V: Vocabulary, T: ?Sized + LinkedDataGraph<I, V>> LinkedDataGraph<I, V>
25 for &T
26{
27 fn visit_graph<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
28 where
29 S: GraphVisitor<I, V>,
30 {
31 T::visit_graph(self, visitor)
32 }
33}
34
35impl<I: Interpretation, V: Vocabulary, T: ?Sized + LinkedDataGraph<I, V>> LinkedDataGraph<I, V>
36 for Box<T>
37{
38 fn visit_graph<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
39 where
40 S: GraphVisitor<I, V>,
41 {
42 T::visit_graph(self, visitor)
43 }
44}
45
46impl<I: Interpretation, V: Vocabulary> LinkedDataGraph<I, V> for Iri {
47 fn visit_graph<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
48 where
49 S: GraphVisitor<I, V>,
50 {
51 visitor.end()
52 }
53}
54
55impl<I: Interpretation, V: Vocabulary> LinkedDataGraph<I, V> for IriBuf {
56 fn visit_graph<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
57 where
58 S: GraphVisitor<I, V>,
59 {
60 visitor.end()
61 }
62}
63
64impl<I: Interpretation, V: Vocabulary, T: LinkedDataSubject<I, V> + LinkedDataResource<I, V>>
65 LinkedDataGraph<I, V> for [T]
66{
67 fn visit_graph<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
68 where
69 S: GraphVisitor<I, V>,
70 {
71 for t in self {
72 visitor.subject(t)?;
73 }
74 visitor.end()
75 }
76}
77
78impl<I: Interpretation, V: Vocabulary, T: LinkedDataSubject<I, V> + LinkedDataResource<I, V>>
79 LinkedDataGraph<I, V> for Vec<T>
80{
81 fn visit_graph<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
82 where
83 S: GraphVisitor<I, V>,
84 {
85 for t in self {
86 visitor.subject(t)?;
87 }
88 visitor.end()
89 }
90}
91
92pub trait GraphVisitor<I: Interpretation, V: Vocabulary> {
93 type Ok;
94 type Error;
95
96 fn subject<T>(&mut self, value: &T) -> Result<(), Self::Error>
97 where
98 T: ?Sized + LinkedDataResource<I, V> + LinkedDataSubject<I, V>;
99
100 fn end(self) -> Result<Self::Ok, Self::Error>;
101}
102
103impl<I: Interpretation, V: Vocabulary, S: GraphVisitor<I, V>> GraphVisitor<I, V> for &mut S {
104 type Ok = ();
105 type Error = S::Error;
106
107 fn subject<T>(&mut self, value: &T) -> Result<(), Self::Error>
108 where
109 T: ?Sized + LinkedDataResource<I, V> + LinkedDataSubject<I, V>,
110 {
111 S::subject(self, value)
112 }
113
114 fn end(self) -> Result<Self::Ok, Self::Error> {
115 Ok(())
116 }
117}
118
119pub struct AnonymousGraph<T>(pub T);
120
121impl<I: Interpretation, V: Vocabulary, T> LinkedDataResource<I, V> for AnonymousGraph<T> {
122 fn interpretation(
123 &self,
124 _vocabulary: &mut V,
125 _interpretation: &mut I,
126 ) -> crate::ResourceInterpretation<I, V> {
127 crate::ResourceInterpretation::Uninterpreted(None)
128 }
129}
130
131impl<I: Interpretation, V: Vocabulary, T: LinkedDataGraph<I, V>> LinkedDataSubject<I, V>
132 for AnonymousGraph<T>
133{
134 fn visit_subject<S>(&self, mut serializer: S) -> Result<S::Ok, S::Error>
135 where
136 S: crate::SubjectVisitor<I, V>,
137 {
138 serializer.graph(&self.0)?;
139 serializer.end()
140 }
141}
142
143impl<I: Interpretation, V: Vocabulary, T: LinkedDataGraph<I, V>> LinkedDataPredicateObjects<I, V>
144 for AnonymousGraph<T>
145{
146 fn visit_objects<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
147 where
148 S: crate::PredicateObjectsVisitor<I, V>,
149 {
150 visitor.object(self)?;
151 visitor.end()
152 }
153}
154
155impl<I: Interpretation, V: Vocabulary, T: LinkedDataGraph<I, V>> LinkedDataGraph<I, V>
156 for AnonymousGraph<T>
157{
158 fn visit_graph<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
159 where
160 S: GraphVisitor<I, V>,
161 {
162 T::visit_graph(&self.0, visitor)
163 }
164}
165
166impl<I: Interpretation, V: Vocabulary, T: LinkedDataGraph<I, V>> LinkedData<I, V>
167 for AnonymousGraph<T>
168{
169 fn visit<S>(&self, mut visitor: S) -> Result<S::Ok, S::Error>
170 where
171 S: crate::Visitor<I, V>,
172 {
173 visitor.named_graph(self)?;
174 visitor.end()
175 }
176}