Skip to main content

ld_core/
anonymous.rs

1use iri_rs::Iri;
2use rdfx::Interpretation;
3
4use crate::{
5	BorrowedRdfTerm, CowRdfTerm, GraphVisitor, LinkedData, LinkedDataGraph,
6	LinkedDataPredicateObjects, LinkedDataResource, LinkedDataSubject, PredicateObjectsVisitor,
7	ResourceInterpretation, SubjectVisitor, Visitor,
8};
9
10/// Binding of a value to a predicate, serialized on an anonymous subject.
11pub struct AnonymousBinding<'a, T>(pub Iri<&'a str>, pub &'a T);
12
13impl<'a, T> AnonymousBinding<'a, T> {
14	/// Binds `value` to the `iri` predicate.
15	pub fn new(iri: Iri<&'a str>, value: &'a T) -> Self {
16		Self(iri, value)
17	}
18}
19
20impl<'a, I: Interpretation, T> LinkedDataResource<I> for AnonymousBinding<'a, T> {
21	fn interpretation(&self, _interpretation: &mut I) -> ResourceInterpretation<'_, I> {
22		ResourceInterpretation::Uninterpreted(None)
23	}
24}
25
26impl<'a, I: Interpretation, T: LinkedDataPredicateObjects<I>> LinkedDataSubject<I>
27	for AnonymousBinding<'a, T>
28{
29	fn visit_subject<S>(&self, mut serializer: S) -> Result<S::Ok, S::Error>
30	where
31		S: SubjectVisitor<I>,
32	{
33		serializer.predicate(&IriValue(self.0), self.1)?;
34		serializer.end()
35	}
36}
37
38impl<'a, I: Interpretation, T: LinkedDataPredicateObjects<I>> LinkedDataPredicateObjects<I>
39	for AnonymousBinding<'a, T>
40{
41	fn visit_objects<S>(&self, mut serializer: S) -> Result<S::Ok, S::Error>
42	where
43		S: PredicateObjectsVisitor<I>,
44	{
45		serializer.object(self)?;
46		serializer.end()
47	}
48}
49
50impl<'a, I: Interpretation, T: LinkedDataPredicateObjects<I>> LinkedDataGraph<I>
51	for AnonymousBinding<'a, T>
52{
53	fn visit_graph<S>(&self, mut serializer: S) -> Result<S::Ok, S::Error>
54	where
55		S: GraphVisitor<I>,
56	{
57		serializer.subject(self)?;
58		serializer.end()
59	}
60}
61
62impl<'a, I: Interpretation, T: LinkedDataPredicateObjects<I>> LinkedData<I>
63	for AnonymousBinding<'a, T>
64{
65	fn visit<S>(&self, mut serializer: S) -> Result<S::Ok, S::Error>
66	where
67		S: Visitor<I>,
68	{
69		serializer.default_graph(self)?;
70		serializer.end()
71	}
72}
73
74/// Wrapper exposing an `Iri<&str>` as a `LinkedDataResource` predicate.
75struct IriValue<'a>(Iri<&'a str>);
76
77impl<'a, I: Interpretation> LinkedDataResource<I> for IriValue<'a> {
78	fn interpretation(&self, _interpretation: &mut I) -> ResourceInterpretation<'_, I> {
79		ResourceInterpretation::Uninterpreted(Some(CowRdfTerm::Borrowed(BorrowedRdfTerm::Iri(
80			self.0,
81		))))
82	}
83}