1use json_syntax::JsonValue;
2use linked_data::ser::to_rdf_quads_interpretation_with;
3use rdf_syntax::{GenInterpretation, IriBuf};
4use rdf_syntax::{Generator, Quad, Term};
5
6fn resolve_sync<F: std::future::Future>(f: F) -> F::Output {
12 let waker = futures::task::noop_waker_ref();
13 let mut cx = std::task::Context::from_waker(waker);
14 let mut f = std::pin::pin!(f);
15 match f.as_mut().poll(&mut cx) {
16 std::task::Poll::Ready(result) => result,
17 std::task::Poll::Pending => {
18 panic!("async function backed by a sync loader returned Poll::Pending")
19 }
20 }
21}
22
23use crate::JsonLdVisit;
24use crate::{
25 JsonLdDirection, JsonLdDocument, JsonLdExpandedDocument, JsonLdLocatedError,
26 JsonLdProcessingMode, RemoteJsonLdContext,
27 algorithms::{
28 AsyncJsonLdEnvironment, JsonLdCompactionOptions, JsonLdContextProcessingOptions,
29 JsonLdEnvironment, JsonLdExpansionOptions, JsonLdExpansionPolicy, RdfSerializationOptions,
30 },
31};
32
33mod document;
34
35#[derive(Clone)]
37pub struct JsonLdOptions {
38 pub base: Option<IriBuf>,
42
43 pub compact_arrays: bool,
49
50 pub compact_to_relative: bool,
55
56 pub expand_context: Option<RemoteJsonLdContext>,
58
59 pub ordered: bool,
66
67 pub processing_mode: JsonLdProcessingMode,
71
72 pub rdf_direction: Option<JsonLdDirection>,
75
76 pub produce_generalized_rdf: bool,
79
80 pub expansion_policy: JsonLdExpansionPolicy,
82}
83
84impl JsonLdOptions {
85 pub fn unordered(self) -> Self {
87 Self {
88 ordered: false,
89 ..self
90 }
91 }
92
93 pub fn with_expand_context(self, context: RemoteJsonLdContext) -> Self {
96 Self {
97 expand_context: Some(context),
98 ..self
99 }
100 }
101
102 pub fn context_processing_options(&self) -> JsonLdContextProcessingOptions {
104 JsonLdContextProcessingOptions {
105 processing_mode: self.processing_mode,
106 ..Default::default()
107 }
108 }
109
110 pub fn expansion_options(&self) -> JsonLdExpansionOptions {
112 JsonLdExpansionOptions {
113 processing_mode: self.processing_mode,
114 ordered: self.ordered,
115 policy: self.expansion_policy,
116 }
117 }
118
119 pub fn compaction_options(&self) -> JsonLdCompactionOptions {
121 JsonLdCompactionOptions {
122 processing_mode: self.processing_mode,
123 compact_to_relative: self.compact_to_relative,
124 compact_arrays: self.compact_arrays,
125 ordered: self.ordered,
126 }
127 }
128
129 pub fn rdf_serialization_options(&self) -> RdfSerializationOptions {
130 RdfSerializationOptions {
131 produce_generalized_rdf: self.produce_generalized_rdf,
132 }
133 }
134}
135
136impl Default for JsonLdOptions {
137 fn default() -> Self {
138 Self {
139 base: None,
140 compact_arrays: true,
141 compact_to_relative: true,
142 expand_context: None,
143 ordered: false,
144 processing_mode: JsonLdProcessingMode::JsonLd1_1,
145 rdf_direction: None,
146 produce_generalized_rdf: false,
147 expansion_policy: JsonLdExpansionPolicy::default(),
148 }
149 }
150}
151
152pub type JsonLdExpandResult = Result<JsonLdExpandedDocument, JsonLdLocatedError>;
154
155pub type JsonLdIntoDocumentResult = Result<JsonLdDocument, JsonLdLocatedError>;
157
158pub type JsonLdCompactResult = Result<JsonValue, JsonLdLocatedError>;
160
161pub type JsonLdFlattenResult = Result<JsonValue, JsonLdLocatedError>;
163
164pub type JsonLdToRdfResult = Result<Vec<Quad<Term>>, JsonLdLocatedError>;
165
166pub type JsonLdCompareResult = Result<bool, JsonLdLocatedError>;
168
169pub trait JsonLdProcessor: Sized {
181 fn compare_with(
183 &self,
184 other: &Self,
185 env: impl JsonLdEnvironment,
186 options: JsonLdOptions,
187 ) -> JsonLdCompareResult {
188 resolve_sync(self.async_compare_with(other, env.into_async_environment(), options))
189 }
190
191 fn compare(&self, other: &Self, env: impl JsonLdEnvironment) -> JsonLdCompareResult {
193 self.compare_with(other, env, JsonLdOptions::default())
194 }
195
196 #[allow(async_fn_in_trait)]
198 async fn async_compare_with(
199 &self,
200 other: &Self,
201 env: impl AsyncJsonLdEnvironment,
202 options: JsonLdOptions,
203 ) -> JsonLdCompareResult;
204
205 #[allow(async_fn_in_trait)]
207 async fn async_compare(
208 &self,
209 other: &Self,
210 env: impl AsyncJsonLdEnvironment,
211 ) -> JsonLdCompareResult {
212 self.async_compare_with(other, env, JsonLdOptions::default())
213 .await
214 }
215
216 fn expand_with(
218 &self,
219 env: impl JsonLdEnvironment,
220 options: JsonLdOptions,
221 ) -> JsonLdExpandResult {
222 resolve_sync(self.async_expand_with(env.into_async_environment(), options))
223 }
224
225 fn expand(&self, env: impl JsonLdEnvironment) -> JsonLdExpandResult {
227 self.expand_with(env, JsonLdOptions::default())
228 }
229
230 #[allow(async_fn_in_trait)]
232 async fn async_expand_with(
233 &self,
234 env: impl AsyncJsonLdEnvironment,
235 options: JsonLdOptions,
236 ) -> JsonLdExpandResult;
237
238 #[allow(async_fn_in_trait)]
240 async fn async_expand(&self, env: impl AsyncJsonLdEnvironment) -> JsonLdExpandResult {
241 self.async_expand_with(env, JsonLdOptions::default()).await
242 }
243
244 fn compact_with(
246 &self,
247 context: RemoteJsonLdContext,
248 env: impl JsonLdEnvironment,
249 options: JsonLdOptions,
250 ) -> JsonLdCompactResult {
251 resolve_sync(self.async_compact_with(context, env.into_async_environment(), options))
252 }
253
254 fn compact(
256 &self,
257 context: RemoteJsonLdContext,
258 env: impl JsonLdEnvironment,
259 ) -> JsonLdCompactResult {
260 self.compact_with(context, env, JsonLdOptions::default())
261 }
262
263 #[allow(async_fn_in_trait)]
265 async fn async_compact_with(
266 &self,
267 context: RemoteJsonLdContext,
268 env: impl AsyncJsonLdEnvironment,
269 options: JsonLdOptions,
270 ) -> JsonLdCompactResult;
271
272 #[allow(async_fn_in_trait)]
274 async fn async_compact(
275 &self,
276 context: RemoteJsonLdContext,
277 env: impl AsyncJsonLdEnvironment,
278 ) -> JsonLdCompactResult {
279 self.async_compact_with(context, env, JsonLdOptions::default())
280 .await
281 }
282
283 fn flatten_with(
287 &self,
288 context: Option<RemoteJsonLdContext>,
289 env: impl JsonLdEnvironment,
290 options: JsonLdOptions,
291 ) -> JsonLdFlattenResult {
292 resolve_sync(self.async_flatten_with(context, env.into_async_environment(), options))
293 }
294
295 fn flatten(
297 &self,
298 context: Option<RemoteJsonLdContext>,
299 env: impl JsonLdEnvironment,
300 ) -> JsonLdFlattenResult {
301 self.flatten_with(context, env, JsonLdOptions::default())
302 }
303
304 #[allow(async_fn_in_trait)]
308 async fn async_flatten_with(
309 &self,
310 context: Option<RemoteJsonLdContext>,
311 env: impl AsyncJsonLdEnvironment,
312 options: JsonLdOptions,
313 ) -> JsonLdFlattenResult;
314
315 #[allow(async_fn_in_trait)]
317 async fn async_flatten(
318 &self,
319 context: Option<RemoteJsonLdContext>,
320 env: impl AsyncJsonLdEnvironment,
321 ) -> JsonLdFlattenResult {
322 self.async_flatten_with(context, env, JsonLdOptions::default())
323 .await
324 }
325
326 fn to_rdf_with(
328 &self,
329 env: impl JsonLdEnvironment,
330 generator: impl Generator,
331 options: JsonLdOptions,
332 ) -> JsonLdToRdfResult {
333 resolve_sync(self.async_to_rdf_with(env.into_async_environment(), generator, options))
334 }
335
336 fn to_rdf(&self, env: impl JsonLdEnvironment, generator: impl Generator) -> JsonLdToRdfResult {
338 self.to_rdf_with(env, generator, JsonLdOptions::default())
339 }
340
341 #[allow(async_fn_in_trait)]
343 async fn async_to_rdf_with(
344 &self,
345 env: impl AsyncJsonLdEnvironment,
346 mut generator: impl Generator,
347 options: JsonLdOptions,
348 ) -> JsonLdToRdfResult {
349 let rdf_serialization_options = options.rdf_serialization_options();
350 let mut expanded = JsonLdProcessor::async_expand_with(self, env, options).await?;
351 expanded.relabel(&mut generator);
352 let interpretation = GenInterpretation::new(generator);
353 Ok(
354 to_rdf_quads_interpretation_with(&expanded, interpretation, rdf_serialization_options)
355 .unwrap(),
356 )
357 }
358
359 #[allow(async_fn_in_trait)]
361 async fn async_to_rdf(
362 &self,
363 env: impl AsyncJsonLdEnvironment,
364 generator: impl Generator,
365 ) -> JsonLdToRdfResult {
366 self.async_to_rdf_with(env, generator, JsonLdOptions::default())
367 .await
368 }
369}