Skip to main content

json_ld/algorithms/
mod.rs

1use crate::{AsyncJsonLdLoader, JsonLdLoader, JsonLdWarning, ToAsyncJsonLdLoader};
2
3mod compaction;
4mod context_processing;
5mod deserialization;
6mod expansion;
7mod flattening;
8mod serialization;
9
10pub use compaction::*;
11pub use context_processing::*;
12pub use deserialization::RdfSerializationOptions;
13pub use expansion::*;
14
15pub trait AsyncJsonLdEnvironment {
16	type Loader: AsyncJsonLdLoader;
17
18	fn loader(&self) -> &Self::Loader;
19
20	fn warn(&self, w: JsonLdWarning);
21
22	fn as_ref(&self) -> AsyncProcessingEnvironmentRef<'_, Self> {
23		AsyncProcessingEnvironmentRef(self)
24	}
25}
26
27impl<L: AsyncJsonLdLoader> AsyncJsonLdEnvironment for L {
28	type Loader = Self;
29
30	fn loader(&self) -> &Self::Loader {
31		self
32	}
33
34	fn warn(&self, _: JsonLdWarning) {
35		// Ignore.
36	}
37}
38
39pub struct AsyncProcessingEnvironmentRef<'a, T: ?Sized>(pub &'a T);
40
41impl<'a, T: ?Sized + AsyncJsonLdEnvironment> AsyncJsonLdEnvironment
42	for AsyncProcessingEnvironmentRef<'a, T>
43{
44	type Loader = T::Loader;
45
46	fn loader(&self) -> &Self::Loader {
47		self.0.loader()
48	}
49
50	fn warn(&self, w: JsonLdWarning) {
51		self.0.warn(w);
52	}
53}
54
55pub trait JsonLdEnvironment {
56	type Loader: JsonLdLoader;
57
58	fn loader(&self) -> &Self::Loader;
59
60	fn warn(&self, w: JsonLdWarning);
61
62	fn as_async_environment(&self) -> &ToAsyncJsonLdEnvironment<Self> {
63		ToAsyncJsonLdEnvironment::from_ref(self)
64	}
65
66	fn into_async_environment(self) -> ToAsyncJsonLdEnvironment<Self>
67	where
68		Self: Sized,
69	{
70		ToAsyncJsonLdEnvironment(self)
71	}
72}
73
74impl<L: JsonLdLoader> JsonLdEnvironment for L {
75	type Loader = Self;
76
77	fn loader(&self) -> &Self::Loader {
78		self
79	}
80
81	fn warn(&self, _: JsonLdWarning) {
82		// Ignore.
83	}
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
87#[repr(transparent)]
88pub struct ToAsyncJsonLdEnvironment<T: ?Sized>(pub T);
89
90impl<T: ?Sized> ToAsyncJsonLdEnvironment<T> {
91	pub fn from_ref(env: &T) -> &Self {
92		unsafe {
93			// SAFETY: `ToAsyncJsonLdEnvironment` uses the `transparent` repr.
94			std::mem::transmute(env)
95		}
96	}
97}
98
99impl<T: JsonLdEnvironment> AsyncJsonLdEnvironment for ToAsyncJsonLdEnvironment<T> {
100	type Loader = ToAsyncJsonLdLoader<T::Loader>;
101
102	fn loader(&self) -> &Self::Loader {
103		self.0.loader().as_async_loader()
104	}
105
106	fn warn(&self, w: JsonLdWarning) {
107		self.0.warn(w);
108	}
109}