Skip to content

Commit

Permalink
make wasm-friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
pchampin committed Nov 24, 2023
1 parent 0ff2f4a commit 376c6f1
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ rio_turtle = { version = "0.8", features = ["generalized"] }
rio_xml = { version = "0.8" }
test-case = "3.1.0"
thiserror = "1.0.32"
tokio = { version="1.33.0", features = ["rt"] }
tokio = { version="1.33.0", features = ["rt", "sync"] }
url = "2.4.1"

[profile.release]
Expand Down
3 changes: 1 addition & 2 deletions jsonld/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ file_url = ["dep:url"]
http_client = ["json-ld/reqwest"]

[dependencies]
futures-util.workspace = true
iref = "2.2"
json-ld = "0.15.0"
json-ld = { version = "0.15.0", git = "https://github.com/pchampin/json-ld", branch="wasm-0.15.0" }
json-syntax = "0.9"
langtag = "0.3.4"
locspan = "0.7.12"
Expand Down
2 changes: 2 additions & 0 deletions jsonld/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use json_syntax::Value;
use locspan::Location;
use sophia_iri::Iri;

pub use json_ld::{BoxFuture, FutureExt};

/// A dummy document loader, that does not load anything.
pub type NoLoader =
json_ld::NoLoader<Iri<Arc<str>>, Location<Iri<Arc<str>>>, Value<Location<Iri<Arc<str>>>>>;
Expand Down
2 changes: 1 addition & 1 deletion jsonld/src/loader/chain_loader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;

use futures_util::future::{BoxFuture, FutureExt};
use json_ld::Loader;
use json_ld::{BoxFuture, FutureExt};
use json_syntax::Value;
use locspan::Location;

Expand Down
2 changes: 1 addition & 1 deletion jsonld/src/loader/closure_loader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use futures_util::future::{BoxFuture, FutureExt};
use json_ld::{BoxFuture, FutureExt};
use json_ld::{Loader, RemoteDocument};
use json_syntax::Parse;
use locspan::{Location, Meta};
Expand Down
2 changes: 1 addition & 1 deletion jsonld/src/loader/file_url_loader.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use futures_util::future::{BoxFuture, FutureExt};
use json_ld::{BoxFuture, FutureExt};
use json_ld::{Loader, RemoteDocument};
use json_syntax::Parse;
use locspan::{Location, Meta};
Expand Down
2 changes: 1 addition & 1 deletion jsonld/src/loader/static_loader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::HashMap, fmt::Debug};

use futures_util::future::{BoxFuture, FutureExt};
use json_ld::{BoxFuture, FutureExt};
use json_ld::{Loader, RemoteDocument};
use json_syntax::Value;
use locspan::{Location, Meta};
Expand Down
7 changes: 3 additions & 4 deletions jsonld/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Defines types for configuring JSON-LD processing.
use std::sync::{LockResult, Mutex, MutexGuard};

use json_ld::expansion::Policy;
pub use json_ld::rdf::RdfDirection;
use json_ld::syntax::context::Value;
Expand All @@ -10,6 +8,7 @@ pub use json_ld::ProcessingMode;
use locspan::Location;
use locspan::Span;
use sophia_iri::Iri;
use tokio::sync::{Mutex, MutexGuard};

use crate::loader::NoLoader;
use crate::vocabulary::ArcIri;
Expand Down Expand Up @@ -68,8 +67,8 @@ impl<L> JsonLdOptions<L> {
/// The [`documentLoader`] is used to retrieve remote documents and contexts.
///
/// [`documentLoader`]: https://www.w3.org/TR/json-ld11-api/#dom-jsonldoptions-documentloader
pub fn document_loader(&self) -> LockResult<MutexGuard<L>> {
self.loader.lock()
pub async fn document_loader(&self) -> MutexGuard<L> {
self.loader.lock().await
}

/// [`expandContext`] is a context that is used to initialize the active context when expanding a document.
Expand Down
71 changes: 47 additions & 24 deletions jsonld/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ mod test;
///
/// * the generic parameter `L` is the type of the [document loader](`json_ld::Loader`)
/// (determined by the `options` parameters)
///
/// ## WebAssembly
///
/// In some situations,
/// the methods from the [`QuadParser`] trait panic when executed as WebAssembly
/// (this is not the case when using the [`NoLoader`],
/// but occurs when using the [`HttpLoader`](crate::loader::HttpLoader)).
/// Those panics can be avoided by using the [`JsonLdParser::async_parse_str`] method.
pub struct JsonLdParser<L = NoLoader> {
options: JsonLdOptions<L>,
}
Expand Down Expand Up @@ -61,7 +69,7 @@ impl<L> JsonLdParser<L> {
}

/// Parse (as RDF) a pre-parsed (as JSON) document
pub fn parse_json(&self, data: &RemoteDocument<ArcIri>) -> JsonLdQuadSource
pub async fn parse_json(&self, data: &RemoteDocument<ArcIri>) -> JsonLdQuadSource
where
L: Loader<ArcIri, Location<ArcIri>>
+ json_ld::ContextLoader<ArcIri, Location<ArcIri>>
Expand All @@ -77,18 +85,14 @@ impl<L> JsonLdParser<L> {
Span::default(),
);
let mut generator = rdf_types::generator::Blank::new().with_metadata(gen_loc);
let mut g_loader = match self.options.document_loader() {
Ok(g) => g,
Err(err) => return JsonLdQuadSource::from_err(err),
};
let mut g_loader = self.options.document_loader().await;
let loader = g_loader.deref_mut();
let mut vocab = ArcVoc {};
let options = self.options.inner().clone();
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("Could not build tokio runtime");
match rt.block_on(data.to_rdf_with_using(&mut vocab, &mut generator, loader, options)) {
match data
.to_rdf_with_using(&mut vocab, &mut generator, loader, options)
.await
{
Err(ToRdfError::Expand(err)) => JsonLdQuadSource::from_err(err),
Ok(mut to_rdf) => JsonLdQuadSource::Quads(
to_rdf
Expand All @@ -99,6 +103,34 @@ impl<L> JsonLdParser<L> {
),
}
}

/// Parse (as RDF) a JSON-LD string, asynchronously
pub async fn async_parse_str<'t>(&self, txt: &'t str) -> JsonLdQuadSource
where
L: Loader<ArcIri, Location<ArcIri>>
+ json_ld::ContextLoader<ArcIri, Location<ArcIri>>
+ Send
+ Sync,
L::Output: Into<Value<Location<ArcIri>>>,
L::Error: Display + Send,
L::Context: Into<json_ld::syntax::context::Value<Location<ArcIri>>>,
L::ContextError: Display + Send,
{
let base = self
.options()
.base()
.unwrap_or(Iri::new_unchecked_const("x-string://"))
.map_unchecked(Arc::from);
let json_res = Value::parse_str(txt, |span| Location::new(base.clone(), span));
let json = match json_res {
Ok(json) => json,
Err(err) => {
return JsonLdQuadSource::from_err(err);
}
};
let doc = RemoteDocument::new(Some(base), None, json);
self.parse_json(&doc).await
}
}

impl<B: BufRead, L> QuadParser<B> for JsonLdParser<L>
Expand Down Expand Up @@ -130,20 +162,11 @@ where
where
&'t str: sophia_api::parser::IntoParsable<Target = B>,
{
let base = self
.options()
.base()
.unwrap_or(Iri::new_unchecked_const("x-string://"))
.map_unchecked(Arc::from);
let json_res = Value::parse_str(txt, |span| Location::new(base.clone(), span));
let json = match json_res {
Ok(json) => json,
Err(err) => {
return JsonLdQuadSource::from_err(err);
}
};
let doc = RemoteDocument::new(Some(base), None, json);
self.parse_json(&doc)
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("Could not build tokio runtime");
rt.block_on(self.async_parse_str(txt))
}
}

Expand Down

0 comments on commit 376c6f1

Please sign in to comment.