linked_data/
lib.rs

1//! This library provides primitive traits to serialize and deserialize
2//! Linked-Data types. It is shipped with derive macros (using the `derive`
3//! feature) that can automatically implement those primitives for you.
4//!
5//! # Example
6//!
7//! ```
8//! use iref::IriBuf;
9//! use static_iref::iri;
10//!
11//! #[derive(linked_data::Serialize, linked_data::Deserialize)]
12//! #[ld(prefix("ex" = "http://example.org/"))]
13//! struct Foo {
14//!   #[ld(id)]
15//!   id: IriBuf,
16//!
17//!   #[ld("ex:name")]
18//!   name: String,
19//!
20//!   #[ld("ex:email")]
21//!   email: String
22//! }
23//!
24//! let value = Foo {
25//!   id: iri!("http://example.org/JohnSmith").to_owned(),
26//!   name: "John Smith".to_owned(),
27//!   email: "john.smith@example.org".to_owned()
28//! };
29//!
30//! let quads = linked_data::to_quads(rdf_types::generator::Blank::new(), &value)
31//!   .expect("RDF serialization failed");
32//!
33//! for quad in quads {
34//!   use rdf_types::RdfDisplay;
35//!   println!("{} .", quad.rdf_display())
36//! }
37//! ```
38//!
39//! This should print the following:
40//! ```text
41//! <http://example.org/JohnSmith> <http://example.org/name> "John Smith" .
42//! <http://example.org/JohnSmith> <http://example.org/email> "john.smith@example.org" .
43//! ```
44use educe::Educe;
45use iref::{Iri, IriBuf};
46#[cfg(feature = "derive")]
47pub use linked_data_derive::{Deserialize, Serialize};
48use rdf_types::{
49	dataset::{PatternMatchingDataset, TraversableDataset},
50	interpretation::ReverseIriInterpretation,
51	vocabulary::IriVocabulary,
52	Interpretation, Vocabulary,
53};
54
55#[doc(hidden)]
56pub use iref;
57
58#[doc(hidden)]
59pub use rdf_types;
60
61#[doc(hidden)]
62pub use xsd_types;
63
64#[doc(hidden)]
65pub use json_syntax;
66
67mod anonymous;
68mod datatypes;
69mod graph;
70mod r#impl;
71mod macros;
72mod predicate;
73mod quads;
74mod rdf;
75mod reference;
76mod resource;
77mod subject;
78
79pub use anonymous::*;
80pub use graph::*;
81pub use predicate::*;
82pub use quads::{
83	to_interpreted_graph_quads, to_interpreted_quads, to_interpreted_subject_quads,
84	to_lexical_quads, to_lexical_quads_with, to_lexical_subject_quads,
85	to_lexical_subject_quads_with, to_quads, to_quads_with, IntoQuadsError,
86};
87pub use rdf::*;
88pub use reference::*;
89pub use resource::*;
90pub use subject::*;
91
92#[derive(Debug, thiserror::Error)]
93pub enum FromLinkedDataError {
94	/// Resource has no IRI representation.
95	#[error("expected IRI")]
96	ExpectedIri(ContextIris),
97
98	#[error("unsupported IRI `{found}`")]
99	UnsupportedIri {
100		/// Error context.
101		context: ContextIris,
102
103		/// Unsupported IRI.
104		found: IriBuf,
105
106		/// Optional hint listing the supported IRIs.
107		supported: Option<Vec<IriBuf>>,
108	},
109
110	/// Resource has no literal representation.
111	#[error("expected literal")]
112	ExpectedLiteral(ContextIris),
113
114	/// Resource has literal representations, but none of the expected type.
115	#[error("literal type mismatch")]
116	LiteralTypeMismatch {
117		context: ContextIris,
118		expected: Option<IriBuf>,
119		found: IriBuf,
120	},
121
122	/// Resource has a literal representation of the correct type, but the
123	/// lexical value could not be successfully parsed.
124	#[error("invalid literal")]
125	InvalidLiteral(ContextIris),
126
127	/// Missing required value.
128	#[error("missing required value")]
129	MissingRequiredValue(ContextIris),
130
131	/// Too many values.
132	#[error("too many values")]
133	TooManyValues(ContextIris),
134
135	/// Generic error for invalid subjects.
136	#[error("invalid subject")]
137	InvalidSubject {
138		context: ContextIris,
139		subject: Option<IriBuf>,
140	},
141}
142
143impl FromLinkedDataError {
144	pub fn context(&self) -> &ContextIris {
145		match self {
146			Self::ExpectedIri(c) => c,
147			Self::UnsupportedIri { context, .. } => context,
148			Self::ExpectedLiteral(c) => c,
149			Self::LiteralTypeMismatch { context, .. } => context,
150			Self::InvalidLiteral(c) => c,
151			Self::MissingRequiredValue(c) => c,
152			Self::TooManyValues(c) => c,
153			Self::InvalidSubject { context, .. } => context,
154		}
155	}
156}
157
158/// Linked-Data type.
159///
160/// A Linked-Data type represents an RDF dataset which can be visited using the
161/// [`visit`](Self::visit) method.
162pub trait LinkedData<I: Interpretation = (), V: Vocabulary = ()> {
163	/// Visit the RDF dataset represented by this type.
164	fn visit<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
165	where
166		S: Visitor<I, V>;
167}
168
169impl<'a, I: Interpretation, V: Vocabulary, T: ?Sized + LinkedData<I, V>> LinkedData<I, V>
170	for &'a T
171{
172	fn visit<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
173	where
174		S: Visitor<I, V>,
175	{
176		T::visit(self, visitor)
177	}
178}
179
180impl<I: Interpretation, V: Vocabulary, T: ?Sized + LinkedData<I, V>> LinkedData<I, V> for Box<T> {
181	fn visit<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
182	where
183		S: Visitor<I, V>,
184	{
185		T::visit(self, visitor)
186	}
187}
188
189impl<I: Interpretation, V: Vocabulary> LinkedData<I, V> for Iri {
190	fn visit<S>(&self, visitor: S) -> Result<S::Ok, S::Error>
191	where
192		S: Visitor<I, V>,
193	{
194		visitor.end()
195	}
196}
197
198/// RDF dataset visitor.
199pub trait Visitor<I: Interpretation = (), V: Vocabulary = ()> {
200	/// Type of the value returned by the visitor when the dataset has been
201	/// entirely visited.
202	type Ok;
203
204	/// Error type.
205	type Error;
206
207	/// Visits the default graph of the dataset.
208	fn default_graph<T>(&mut self, value: &T) -> Result<(), Self::Error>
209	where
210		T: ?Sized + LinkedDataGraph<I, V>;
211
212	/// Visits a named graph of the dataset.
213	fn named_graph<T>(&mut self, value: &T) -> Result<(), Self::Error>
214	where
215		T: ?Sized + LinkedDataResource<I, V> + LinkedDataGraph<I, V>;
216
217	/// Ends the dataset visit.
218	fn end(self) -> Result<Self::Ok, Self::Error>;
219}
220
221/// Any mutable reference to a visitor is itself a visitor.
222impl<'s, I: Interpretation, V: Vocabulary, S: Visitor<I, V>> Visitor<I, V> for &'s mut S {
223	type Ok = ();
224	type Error = S::Error;
225
226	fn default_graph<T>(&mut self, value: &T) -> Result<(), Self::Error>
227	where
228		T: ?Sized + LinkedDataGraph<I, V>,
229	{
230		S::default_graph(self, value)
231	}
232
233	fn named_graph<T>(&mut self, value: &T) -> Result<(), Self::Error>
234	where
235		T: ?Sized + LinkedDataResource<I, V> + LinkedDataGraph<I, V>,
236	{
237		S::named_graph(self, value)
238	}
239
240	fn end(self) -> Result<Self::Ok, Self::Error> {
241		Ok(())
242	}
243}
244
245#[derive(Educe)]
246#[educe(Debug(bound = "I::Resource: core::fmt::Debug"), Clone, Copy)]
247pub enum ResourceOrIriRef<'a, I: Interpretation> {
248	Resource(&'a I::Resource),
249	Iri(&'a Iri),
250	Anonymous,
251}
252
253impl<'a, I: Interpretation> ResourceOrIriRef<'a, I> {
254	pub fn into_iri<V>(self, vocabulary: &V, interpretation: &I) -> Option<IriBuf>
255	where
256		V: IriVocabulary,
257		I: ReverseIriInterpretation<Iri = V::Iri>,
258	{
259		match self {
260			Self::Resource(r) => interpretation
261				.iris_of(r)
262				.next()
263				.map(|i| vocabulary.iri(i).unwrap().to_owned()),
264			Self::Iri(i) => Some(i.to_owned()),
265			Self::Anonymous => None,
266		}
267	}
268}
269
270#[derive(Educe)]
271#[educe(Debug(bound = "I::Resource: core::fmt::Debug"), Clone, Copy)]
272pub enum Context<'a, I: Interpretation> {
273	Subject,
274	Predicate {
275		subject: ResourceOrIriRef<'a, I>,
276	},
277	Object {
278		subject: ResourceOrIriRef<'a, I>,
279		predicate: ResourceOrIriRef<'a, I>,
280	},
281}
282
283impl<'a, I: Interpretation> Context<'a, I> {
284	pub fn with_subject(self, subject: &'a I::Resource) -> Self {
285		Self::Predicate {
286			subject: ResourceOrIriRef::Resource(subject),
287		}
288	}
289
290	pub fn with_predicate(self, predicate: &'a I::Resource) -> Self {
291		match self {
292			Self::Predicate { subject } => Self::Object {
293				subject,
294				predicate: ResourceOrIriRef::Resource(predicate),
295			},
296			_ => Self::Subject,
297		}
298	}
299
300	pub fn with_predicate_iri(self, predicate: &'a Iri) -> Self {
301		match self {
302			Self::Predicate { subject } => Self::Object {
303				subject,
304				predicate: ResourceOrIriRef::Iri(predicate),
305			},
306			_ => Self::Subject,
307		}
308	}
309
310	pub fn with_anonymous_predicate(self) -> Self {
311		match self {
312			Self::Predicate { subject } => Self::Object {
313				subject,
314				predicate: ResourceOrIriRef::Anonymous,
315			},
316			_ => Self::Subject,
317		}
318	}
319
320	pub fn into_iris<V>(self, vocabulary: &V, interpretation: &I) -> ContextIris
321	where
322		V: IriVocabulary,
323		I: ReverseIriInterpretation<Iri = V::Iri>,
324	{
325		match self {
326			Self::Subject => ContextIris::Subject,
327			Self::Predicate { subject } => ContextIris::Predicate {
328				subject: subject.into_iri(vocabulary, interpretation),
329			},
330			Self::Object { subject, predicate } => ContextIris::Object {
331				subject: subject.into_iri(vocabulary, interpretation),
332				predicate: predicate.into_iri(vocabulary, interpretation),
333			},
334		}
335	}
336}
337
338#[derive(Debug, Clone)]
339pub enum ContextIris {
340	Subject,
341	Predicate {
342		subject: Option<IriBuf>,
343	},
344	Object {
345		subject: Option<IriBuf>,
346		predicate: Option<IriBuf>,
347	},
348}
349
350impl<'a, I: Interpretation> Default for Context<'a, I> {
351	fn default() -> Self {
352		Self::Subject
353	}
354}
355
356pub trait LinkedDataDeserialize<V: Vocabulary = (), I: Interpretation = ()>: Sized {
357	fn deserialize_dataset_in(
358		vocabulary: &V,
359		interpretation: &I,
360		dataset: &(impl TraversableDataset<Resource = I::Resource> + PatternMatchingDataset),
361		context: Context<I>,
362	) -> Result<Self, FromLinkedDataError>;
363
364	fn deserialize_dataset(
365		vocabulary: &V,
366		interpretation: &I,
367		dataset: &(impl TraversableDataset<Resource = I::Resource> + PatternMatchingDataset),
368	) -> Result<Self, FromLinkedDataError> {
369		Self::deserialize_dataset_in(vocabulary, interpretation, dataset, Context::default())
370	}
371}