1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::{loader, Error, ErrorCode};
use futures::future::{BoxFuture, FutureExt};
use generic_json::Json;
use iref::{Iri, IriBuf};
pub struct RemoteContext<C> {
url: IriBuf,
source: loader::Id,
context: C,
}
impl<C> RemoteContext<C> {
pub fn new(url: Iri, source: loader::Id, context: C) -> RemoteContext<C> {
RemoteContext {
url: IriBuf::from(url),
source,
context,
}
}
pub fn from_parts(url: IriBuf, source: loader::Id, context: C) -> RemoteContext<C> {
RemoteContext {
url,
source,
context,
}
}
pub fn context(&self) -> &C {
&self.context
}
pub fn into_context(self) -> C {
self.context
}
pub fn source(&self) -> loader::Id {
self.source
}
pub fn url(&self) -> Iri {
self.url.as_iri()
}
pub fn cast<D>(self) -> RemoteContext<D>
where
C: Into<D>,
{
RemoteContext {
url: self.url,
source: self.source,
context: self.context.into(),
}
}
}
pub trait Loader {
type Output;
fn id(&self, iri: Iri<'_>) -> Option<crate::loader::Id>;
#[inline(always)]
fn id_opt(&self, iri: Option<Iri<'_>>) -> Option<crate::loader::Id> {
iri.and_then(|iri| self.id(iri))
}
fn iri(&self, id: crate::loader::Id) -> Option<Iri<'_>>;
fn load_context<'a>(
&'a mut self,
url: Iri,
) -> BoxFuture<'a, Result<RemoteContext<Self::Output>, Error>>;
}
impl<L: Send + Sync + crate::Loader> Loader for L
where
<L::Document as Json>::Object: IntoIterator,
{
type Output = L::Document;
fn id(&self, iri: Iri<'_>) -> Option<crate::loader::Id> {
self.id(iri)
}
fn iri(&self, id: crate::loader::Id) -> Option<Iri<'_>> {
self.iri(id)
}
fn load_context<'a>(
&'a mut self,
url: Iri,
) -> BoxFuture<'a, Result<RemoteContext<L::Document>, Error>> {
let url = IriBuf::from(url);
async move {
match self.load(url.as_iri()).await {
Ok(remote_doc) => {
let (doc, source, url) = remote_doc.into_parts();
if let generic_json::Value::Object(obj) = doc.into() {
for (key, value) in obj {
if &*key == "@context" {
return Ok(RemoteContext::from_parts(url, source, value));
}
}
}
Err(ErrorCode::InvalidRemoteContext.into())
}
Err(_) => Err(ErrorCode::LoadingRemoteContextFailed.into()),
}
}
.boxed()
}
}