use json_syntax::JsonValue;
use linked_data::ser::to_rdf_quads_interpretation_with;
use rdf_syntax::{GenInterpretation, IriBuf};
use rdf_syntax::{Generator, Quad, Term};
fn resolve_sync<F: std::future::Future>(f: F) -> F::Output {
let waker = futures::task::noop_waker_ref();
let mut cx = std::task::Context::from_waker(waker);
let mut f = std::pin::pin!(f);
match f.as_mut().poll(&mut cx) {
std::task::Poll::Ready(result) => result,
std::task::Poll::Pending => {
panic!("async function backed by a sync loader returned Poll::Pending")
}
}
}
use crate::JsonLdVisit;
use crate::{
JsonLdDirection, JsonLdDocument, JsonLdExpandedDocument, JsonLdLocatedError,
JsonLdProcessingMode, RemoteJsonLdContext,
algorithms::{
AsyncJsonLdEnvironment, JsonLdCompactionOptions, JsonLdContextProcessingOptions,
JsonLdEnvironment, JsonLdExpansionOptions, JsonLdExpansionPolicy, RdfSerializationOptions,
},
};
mod document;
#[derive(Clone)]
pub struct JsonLdOptions {
pub base: Option<IriBuf>,
pub compact_arrays: bool,
pub compact_to_relative: bool,
pub expand_context: Option<RemoteJsonLdContext>,
pub ordered: bool,
pub processing_mode: JsonLdProcessingMode,
pub rdf_direction: Option<JsonLdDirection>,
pub produce_generalized_rdf: bool,
pub expansion_policy: JsonLdExpansionPolicy,
}
impl JsonLdOptions {
pub fn unordered(self) -> Self {
Self {
ordered: false,
..self
}
}
pub fn with_expand_context(self, context: RemoteJsonLdContext) -> Self {
Self {
expand_context: Some(context),
..self
}
}
pub fn context_processing_options(&self) -> JsonLdContextProcessingOptions {
JsonLdContextProcessingOptions {
processing_mode: self.processing_mode,
..Default::default()
}
}
pub fn expansion_options(&self) -> JsonLdExpansionOptions {
JsonLdExpansionOptions {
processing_mode: self.processing_mode,
ordered: self.ordered,
policy: self.expansion_policy,
}
}
pub fn compaction_options(&self) -> JsonLdCompactionOptions {
JsonLdCompactionOptions {
processing_mode: self.processing_mode,
compact_to_relative: self.compact_to_relative,
compact_arrays: self.compact_arrays,
ordered: self.ordered,
}
}
pub fn rdf_serialization_options(&self) -> RdfSerializationOptions {
RdfSerializationOptions {
produce_generalized_rdf: self.produce_generalized_rdf,
}
}
}
impl Default for JsonLdOptions {
fn default() -> Self {
Self {
base: None,
compact_arrays: true,
compact_to_relative: true,
expand_context: None,
ordered: false,
processing_mode: JsonLdProcessingMode::JsonLd1_1,
rdf_direction: None,
produce_generalized_rdf: false,
expansion_policy: JsonLdExpansionPolicy::default(),
}
}
}
pub type JsonLdExpandResult = Result<JsonLdExpandedDocument, JsonLdLocatedError>;
pub type JsonLdIntoDocumentResult = Result<JsonLdDocument, JsonLdLocatedError>;
pub type JsonLdCompactResult = Result<JsonValue, JsonLdLocatedError>;
pub type JsonLdFlattenResult = Result<JsonValue, JsonLdLocatedError>;
pub type JsonLdToRdfResult = Result<Vec<Quad<Term>>, JsonLdLocatedError>;
pub type JsonLdCompareResult = Result<bool, JsonLdLocatedError>;
pub trait JsonLdProcessor: Sized {
fn compare_with(
&self,
other: &Self,
env: impl JsonLdEnvironment,
options: JsonLdOptions,
) -> JsonLdCompareResult {
resolve_sync(self.async_compare_with(other, env.into_async_environment(), options))
}
fn compare(&self, other: &Self, env: impl JsonLdEnvironment) -> JsonLdCompareResult {
self.compare_with(other, env, JsonLdOptions::default())
}
#[allow(async_fn_in_trait)]
async fn async_compare_with(
&self,
other: &Self,
env: impl AsyncJsonLdEnvironment,
options: JsonLdOptions,
) -> JsonLdCompareResult;
#[allow(async_fn_in_trait)]
async fn async_compare(
&self,
other: &Self,
env: impl AsyncJsonLdEnvironment,
) -> JsonLdCompareResult {
self.async_compare_with(other, env, JsonLdOptions::default())
.await
}
fn expand_with(
&self,
env: impl JsonLdEnvironment,
options: JsonLdOptions,
) -> JsonLdExpandResult {
resolve_sync(self.async_expand_with(env.into_async_environment(), options))
}
fn expand(&self, env: impl JsonLdEnvironment) -> JsonLdExpandResult {
self.expand_with(env, JsonLdOptions::default())
}
#[allow(async_fn_in_trait)]
async fn async_expand_with(
&self,
env: impl AsyncJsonLdEnvironment,
options: JsonLdOptions,
) -> JsonLdExpandResult;
#[allow(async_fn_in_trait)]
async fn async_expand(&self, env: impl AsyncJsonLdEnvironment) -> JsonLdExpandResult {
self.async_expand_with(env, JsonLdOptions::default()).await
}
fn compact_with(
&self,
context: RemoteJsonLdContext,
env: impl JsonLdEnvironment,
options: JsonLdOptions,
) -> JsonLdCompactResult {
resolve_sync(self.async_compact_with(context, env.into_async_environment(), options))
}
fn compact(
&self,
context: RemoteJsonLdContext,
env: impl JsonLdEnvironment,
) -> JsonLdCompactResult {
self.compact_with(context, env, JsonLdOptions::default())
}
#[allow(async_fn_in_trait)]
async fn async_compact_with(
&self,
context: RemoteJsonLdContext,
env: impl AsyncJsonLdEnvironment,
options: JsonLdOptions,
) -> JsonLdCompactResult;
#[allow(async_fn_in_trait)]
async fn async_compact(
&self,
context: RemoteJsonLdContext,
env: impl AsyncJsonLdEnvironment,
) -> JsonLdCompactResult {
self.async_compact_with(context, env, JsonLdOptions::default())
.await
}
fn flatten_with(
&self,
context: Option<RemoteJsonLdContext>,
env: impl JsonLdEnvironment,
options: JsonLdOptions,
) -> JsonLdFlattenResult {
resolve_sync(self.async_flatten_with(context, env.into_async_environment(), options))
}
fn flatten(
&self,
context: Option<RemoteJsonLdContext>,
env: impl JsonLdEnvironment,
) -> JsonLdFlattenResult {
self.flatten_with(context, env, JsonLdOptions::default())
}
#[allow(async_fn_in_trait)]
async fn async_flatten_with(
&self,
context: Option<RemoteJsonLdContext>,
env: impl AsyncJsonLdEnvironment,
options: JsonLdOptions,
) -> JsonLdFlattenResult;
#[allow(async_fn_in_trait)]
async fn async_flatten(
&self,
context: Option<RemoteJsonLdContext>,
env: impl AsyncJsonLdEnvironment,
) -> JsonLdFlattenResult {
self.async_flatten_with(context, env, JsonLdOptions::default())
.await
}
fn to_rdf_with(
&self,
env: impl JsonLdEnvironment,
generator: impl Generator,
options: JsonLdOptions,
) -> JsonLdToRdfResult {
resolve_sync(self.async_to_rdf_with(env.into_async_environment(), generator, options))
}
fn to_rdf(&self, env: impl JsonLdEnvironment, generator: impl Generator) -> JsonLdToRdfResult {
self.to_rdf_with(env, generator, JsonLdOptions::default())
}
#[allow(async_fn_in_trait)]
async fn async_to_rdf_with(
&self,
env: impl AsyncJsonLdEnvironment,
mut generator: impl Generator,
options: JsonLdOptions,
) -> JsonLdToRdfResult {
let rdf_serialization_options = options.rdf_serialization_options();
let mut expanded = JsonLdProcessor::async_expand_with(self, env, options).await?;
expanded.relabel(&mut generator);
let interpretation = GenInterpretation::new(generator);
Ok(
to_rdf_quads_interpretation_with(&expanded, interpretation, rdf_serialization_options)
.unwrap(),
)
}
#[allow(async_fn_in_trait)]
async fn async_to_rdf(
&self,
env: impl AsyncJsonLdEnvironment,
generator: impl Generator,
) -> JsonLdToRdfResult {
self.async_to_rdf_with(env, generator, JsonLdOptions::default())
.await
}
}