Lines
0 %
Functions
use json_ld::{NoLoader, RemoteDocument, RemoteDocumentReference};
use json_ld_syntax::context::Value;
use locspan::{Location, Meta, Span};
use sophia::{
api::{
graph::CollectibleGraph,
parser::QuadParser,
serializer::{Stringifier, TripleSerializer},
source::QuadSource,
},
inmem::graph::LightGraph,
iri::Iri,
jsonld::{
loader_factory::{DefaultLoaderFactory, LoaderFactory},
vocabulary::ArcIri,
ContextRef, JsonLdOptions, JsonLdParser,
turtle::serializer::turtle::{TurtleConfig, TurtleSerializer},
};
use std::sync::Arc;
fn main() {
let mut args = std::env::args();
let json_ld_path = args.nth(1).expect("Missing jsonld file.");
let context_path = args.next();
if let Some(context_path) = &context_path {
println!(
"Loading {} with @context from {}",
json_ld_path, context_path
);
} else {
println!("Loading {}", json_ld_path);
}
let json_str = std::fs::read_to_string(&json_ld_path)
.expect(&format!("Could not read file {}.", json_ld_path));
let mut options = create_options();
if let Some(context_path) = context_path {
let context_str = std::fs::read_to_string(&context_path)
.expect(&format!("Could not read file {}.", context_path));
let context = create_context(&context_str);
options = options.with_expand_context::<()>(context);
let parser = create_parser(options);
parse(&json_str, parser);
// ArcIri, Location<ArcIri, Span>, Value<Location<ArcIri, Span>>
fn create_context(context_str: &str) -> ContextRef {
use json_ld::syntax::Parse;
let vdoc = json_ld::syntax::Value::parse_str(context_str, |span| span).unwrap();
let mdoc: Meta<_, _> = todo!();
let rdoc: RemoteDocument<ArcIri, Location<ArcIri, Span>, Value<Location<ArcIri, Span>>> =
RemoteDocument::new(None, None, mdoc);
RemoteDocumentReference::Loaded(rdoc)
fn create_options<I, M, T>() -> JsonLdOptions<DefaultLoaderFactory<NoLoader<I, M, T>>> {
JsonLdOptions::<DefaultLoaderFactory<NoLoader<I, M, T>>>::default()
fn create_parser<LF>(options: JsonLdOptions<LF>) -> JsonLdParser<LF> {
JsonLdParser::<LF>::new_with_options(options)
fn parse<LF>(json_str: &str, parser: JsonLdParser<LF>)
where
LF: LoaderFactory,
{
let graph = LightGraph::from_triple_source(parser.parse_str(json_str).to_triples()).unwrap();
let mut stringifier =
TurtleSerializer::new_stringifier_with_config(TurtleConfig::new().with_pretty(true));
let rdf = stringifier.serialize_graph(&graph).unwrap().as_str();
println!("{}", rdf);