-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expand_context and compact_context can now be added as Iri or as JSON string.
- Loading branch information
Showing
5 changed files
with
106 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//! Utility trait to ease the loading of JSON-LD Contexts | ||
use std::{borrow::Borrow, sync::Arc}; | ||
|
||
use json_ld::{ | ||
syntax::{context::Value as ContextValue, Value}, | ||
ExtractContext, RemoteDocument, RemoteDocumentReference, | ||
}; | ||
use json_syntax::Parse; | ||
use locspan::{Location, Span}; | ||
use sophia_iri::Iri; | ||
|
||
use crate::vocabulary::ArcIri; | ||
|
||
/// Type alias for the context references used by the JSON-LD options. | ||
pub type ContextRef = | ||
RemoteDocumentReference<ArcIri, Location<ArcIri, Span>, ContextValue<Location<ArcIri, Span>>>; | ||
|
||
/// Anything that can be turned into a [`ContextRef`] | ||
pub trait IntoContextRef { | ||
/// Turn it into a [`ContextRef`] | ||
fn into_context_ref(self) -> ContextRef; | ||
} | ||
|
||
impl IntoContextRef for ContextRef { | ||
fn into_context_ref(self) -> ContextRef { | ||
self | ||
} | ||
} | ||
|
||
impl<T: Borrow<str>> IntoContextRef for Iri<T> { | ||
fn into_context_ref(self) -> ContextRef { | ||
RemoteDocumentReference::Iri(self.map_unchecked(|t| Arc::from(t.borrow()))) | ||
} | ||
} | ||
|
||
/// Anything that can be turned into a [`ContextRef`] but may fail | ||
pub trait TryIntoContextRef { | ||
/// Raised when the conversion to [`ContextRef`] fails | ||
type Error; | ||
/// Try to turn it into a [`ContextRef`] | ||
fn try_into_context_ref(self) -> Result<ContextRef, Self::Error>; | ||
} | ||
|
||
impl TryIntoContextRef for &str { | ||
type Error = Box<dyn std::error::Error>; | ||
|
||
fn try_into_context_ref(self) -> Result<ContextRef, Self::Error> { | ||
let iri = ArcIri::new_unchecked("x-string://".into()); | ||
let doc = Value::parse_str(self, |span| locspan::Location::new(iri.clone(), span))?; | ||
let context = Value::extract_context(doc) | ||
.map_err(|e| format!("Could not extract @context: {}", e))?; | ||
let rdoc = RemoteDocument::new(None, None, context); | ||
Ok(RemoteDocumentReference::Loaded(rdoc)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters