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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
mod array;
mod element;
mod expanded;
mod iri;
mod literal;
mod node;
mod value;
use crate::{
context::{Loader, ProcessingOptions},
ContextMut, Error, Id, Indexed, Object, ProcessingMode,
};
use futures::Future;
use iref::{Iri, IriBuf};
use json::JsonValue;
use std::cmp::{Ord, Ordering};
use std::collections::HashSet;
pub use array::*;
pub use element::*;
pub use expanded::*;
pub use iri::*;
pub use literal::*;
pub use node::*;
pub use value::*;
#[derive(Clone, Copy, Default)]
pub struct Options {
pub processing_mode: ProcessingMode,
pub policy: Policy,
pub ordered: bool,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Policy {
Relaxed,
Standard,
Strict,
Strictest,
}
impl Policy {
pub fn is_strict(&self) -> bool {
matches!(self, Self::Strict | Self::Strictest)
}
}
impl Default for Policy {
fn default() -> Self {
Self::Standard
}
}
impl From<Options> for ProcessingOptions {
fn from(options: Options) -> ProcessingOptions {
ProcessingOptions {
processing_mode: options.processing_mode,
..Default::default()
}
}
}
impl From<crate::compaction::Options> for Options {
fn from(options: crate::compaction::Options) -> Options {
Options {
processing_mode: options.processing_mode,
ordered: options.ordered,
..Options::default()
}
}
}
#[derive(PartialEq, Eq)]
pub struct Entry<'a, T>(T, &'a JsonValue);
impl<'a, T: PartialOrd> PartialOrd for Entry<'a, T> {
fn partial_cmp(&self, other: &Entry<'a, T>) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
}
}
impl<'a, T: Ord> Ord for Entry<'a, T> {
fn cmp(&self, other: &Entry<'a, T>) -> Ordering {
self.0.cmp(&other.0)
}
}
fn filter_top_level_item<T: Id>(item: &Indexed<Object<T>>) -> bool {
!matches!(item.inner(), Object::Value(_))
}
pub fn expand<'a, T: Send + Sync + Id, C: Send + Sync + ContextMut<T>, L: Send + Sync + Loader>(
active_context: &'a C,
element: &'a JsonValue,
base_url: Option<Iri>,
loader: &'a mut L,
options: Options,
) -> impl 'a + Send + Future<Output = Result<HashSet<Indexed<Object<T>>>, Error>>
where
C::LocalContext: Send + Sync + From<L::Output> + From<JsonValue>,
L::Output: Into<JsonValue>,
{
let base_url = base_url.map(IriBuf::from);
async move {
let base_url = base_url.as_ref().map(|url| url.as_iri());
let expanded = expand_element(
active_context,
None,
element,
base_url,
loader,
options,
false,
)
.await?;
if expanded.len() == 1 {
match expanded.into_iter().next().unwrap().into_unnamed_graph() {
Ok(graph) => Ok(graph),
Err(obj) => {
let mut set = HashSet::new();
if filter_top_level_item(&obj) {
set.insert(obj);
}
Ok(set)
}
}
} else {
Ok(expanded.into_iter().filter(filter_top_level_item).collect())
}
}
}