oxigraph 0.1.0.rc2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +17 -0
- data/Cargo.lock +836 -0
- data/Cargo.toml +13 -0
- data/LICENSE +21 -0
- data/README.md +180 -0
- data/ext/oxigraph/Cargo.toml +24 -0
- data/ext/oxigraph/build.rs +5 -0
- data/ext/oxigraph/extconf.rb +6 -0
- data/ext/oxigraph/src/error.rs +45 -0
- data/ext/oxigraph/src/lib.rs +15 -0
- data/ext/oxigraph/src/load.rs +181 -0
- data/ext/oxigraph/src/model.rs +367 -0
- data/ext/oxigraph/src/query.rs +491 -0
- data/ext/oxigraph/src/store.rs +54 -0
- data/lib/oxigraph/errors.rb +29 -0
- data/lib/oxigraph/load_summary.rb +8 -0
- data/lib/oxigraph/query_results.rb +30 -0
- data/lib/oxigraph/store.rb +27 -0
- data/lib/oxigraph/version.rb +5 -0
- data/lib/oxigraph.rb +22 -0
- metadata +80 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
use std::fmt::Display;
|
|
2
|
+
use std::hash::{BuildHasher, Hash, RandomState};
|
|
3
|
+
use std::sync::OnceLock;
|
|
4
|
+
|
|
5
|
+
use magnus::{
|
|
6
|
+
function, method, prelude::*, scan_args, typed_data::Obj, Error, RClass, RHash, RModule, Ruby,
|
|
7
|
+
TryConvert, TypedData, Value,
|
|
8
|
+
};
|
|
9
|
+
use oxigraph::model as rdf;
|
|
10
|
+
|
|
11
|
+
use crate::error::argument_error;
|
|
12
|
+
|
|
13
|
+
/// An IRI, exposed to Ruby as `Oxigraph::NamedNode`.
|
|
14
|
+
#[derive(PartialEq, Eq, Hash)]
|
|
15
|
+
#[magnus::wrap(class = "Oxigraph::NamedNode", free_immediately, size)]
|
|
16
|
+
pub struct NamedNode {
|
|
17
|
+
inner: rdf::NamedNode,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
impl NamedNode {
|
|
21
|
+
fn new(ruby: &Ruby, iri: String) -> Result<Obj<Self>, Error> {
|
|
22
|
+
let inner = rdf::NamedNode::new(iri).map_err(|cause| argument_error(ruby, cause))?;
|
|
23
|
+
Ok(frozen(ruby, Self { inner }))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fn value(&self) -> String {
|
|
27
|
+
self.inner.as_str().to_owned()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
fn to_s(&self) -> String {
|
|
31
|
+
self.inner.to_string()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
fn inspect(&self) -> String {
|
|
35
|
+
inspect("NamedNode", &self.inner)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
fn equals(&self, other: Value) -> bool {
|
|
39
|
+
equals(self, other)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
fn hash(&self) -> u64 {
|
|
43
|
+
hash(self)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// A blank node, exposed to Ruby as `Oxigraph::BlankNode`.
|
|
48
|
+
#[derive(PartialEq, Eq, Hash)]
|
|
49
|
+
#[magnus::wrap(class = "Oxigraph::BlankNode", free_immediately, size)]
|
|
50
|
+
pub struct BlankNode {
|
|
51
|
+
inner: rdf::BlankNode,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
impl BlankNode {
|
|
55
|
+
fn new(ruby: &Ruby, args: &[Value]) -> Result<Obj<Self>, Error> {
|
|
56
|
+
let args = scan_args::scan_args::<(), (Option<String>,), (), (), (), ()>(args)?;
|
|
57
|
+
let inner = match args.optional.0 {
|
|
58
|
+
Some(id) => rdf::BlankNode::new(id).map_err(|cause| argument_error(ruby, cause))?,
|
|
59
|
+
None => rdf::BlankNode::default(),
|
|
60
|
+
};
|
|
61
|
+
Ok(frozen(ruby, Self { inner }))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
fn value(&self) -> String {
|
|
65
|
+
self.inner.as_str().to_owned()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fn to_s(&self) -> String {
|
|
69
|
+
self.inner.to_string()
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
fn inspect(&self) -> String {
|
|
73
|
+
inspect("BlankNode", &self.inner)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
fn equals(&self, other: Value) -> bool {
|
|
77
|
+
equals(self, other)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
fn hash(&self) -> u64 {
|
|
81
|
+
hash(self)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/// A literal, exposed to Ruby as `Oxigraph::Literal`.
|
|
86
|
+
#[derive(PartialEq, Eq, Hash)]
|
|
87
|
+
#[magnus::wrap(class = "Oxigraph::Literal", free_immediately, size)]
|
|
88
|
+
pub struct Literal {
|
|
89
|
+
inner: rdf::Literal,
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
impl Literal {
|
|
93
|
+
fn new(ruby: &Ruby, args: &[Value]) -> Result<Obj<Self>, Error> {
|
|
94
|
+
let args = scan_args::scan_args::<(String,), (), (), (), RHash, ()>(args)?;
|
|
95
|
+
let keywords = scan_args::get_kwargs::<
|
|
96
|
+
_,
|
|
97
|
+
(),
|
|
98
|
+
(Option<Option<Value>>, Option<Option<String>>),
|
|
99
|
+
(),
|
|
100
|
+
>(args.keywords, &[], &["datatype", "language"])?;
|
|
101
|
+
let (value,) = args.required;
|
|
102
|
+
let (datatype, language) = keywords.optional;
|
|
103
|
+
let inner = match (datatype.flatten(), language.flatten()) {
|
|
104
|
+
(Some(_), Some(_)) => {
|
|
105
|
+
return Err(argument_error(
|
|
106
|
+
ruby,
|
|
107
|
+
"a literal takes a datatype or a language, not both",
|
|
108
|
+
))
|
|
109
|
+
}
|
|
110
|
+
(Some(datatype), None) => {
|
|
111
|
+
let datatype = <&NamedNode>::try_convert(datatype)?;
|
|
112
|
+
rdf::Literal::new_typed_literal(value, datatype.inner.clone())
|
|
113
|
+
}
|
|
114
|
+
(None, Some(language)) => rdf::Literal::new_language_tagged_literal(value, language)
|
|
115
|
+
.map_err(|cause| argument_error(ruby, cause))?,
|
|
116
|
+
(None, None) => rdf::Literal::new_simple_literal(value),
|
|
117
|
+
};
|
|
118
|
+
Ok(frozen(ruby, Self { inner }))
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
fn value(&self) -> String {
|
|
122
|
+
self.inner.value().to_owned()
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
fn datatype(ruby: &Ruby, rb_self: &Self) -> Obj<NamedNode> {
|
|
126
|
+
let inner = rb_self.inner.datatype().into_owned();
|
|
127
|
+
frozen(ruby, NamedNode { inner })
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
fn language(&self) -> Option<String> {
|
|
131
|
+
self.inner.language().map(str::to_owned)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
fn to_s(&self) -> String {
|
|
135
|
+
self.inner.to_string()
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
fn inspect(&self) -> String {
|
|
139
|
+
inspect("Literal", &self.inner)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
fn equals(&self, other: Value) -> bool {
|
|
143
|
+
equals(self, other)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
fn hash(&self) -> u64 {
|
|
147
|
+
hash(self)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/// A subject, predicate, and object, exposed to Ruby as `Oxigraph::Triple`.
|
|
152
|
+
#[derive(PartialEq, Eq, Hash)]
|
|
153
|
+
#[magnus::wrap(class = "Oxigraph::Triple", free_immediately, size)]
|
|
154
|
+
pub struct Triple {
|
|
155
|
+
inner: rdf::Triple,
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
impl Triple {
|
|
159
|
+
fn new(
|
|
160
|
+
ruby: &Ruby,
|
|
161
|
+
subject: Value,
|
|
162
|
+
predicate: Value,
|
|
163
|
+
object: Value,
|
|
164
|
+
) -> Result<Obj<Self>, Error> {
|
|
165
|
+
let inner = rdf::Triple::new(
|
|
166
|
+
subject_from(ruby, subject)?,
|
|
167
|
+
<&NamedNode>::try_convert(predicate)?.inner.clone(),
|
|
168
|
+
term_from(ruby, object)?,
|
|
169
|
+
);
|
|
170
|
+
Ok(frozen(ruby, Self { inner }))
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
fn subject(ruby: &Ruby, rb_self: &Self) -> Value {
|
|
174
|
+
match rb_self.inner.subject.clone() {
|
|
175
|
+
rdf::NamedOrBlankNode::NamedNode(inner) => frozen(ruby, NamedNode { inner }).as_value(),
|
|
176
|
+
rdf::NamedOrBlankNode::BlankNode(inner) => frozen(ruby, BlankNode { inner }).as_value(),
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
fn predicate(ruby: &Ruby, rb_self: &Self) -> Obj<NamedNode> {
|
|
181
|
+
let inner = rb_self.inner.predicate.clone();
|
|
182
|
+
frozen(ruby, NamedNode { inner })
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
fn object(ruby: &Ruby, rb_self: &Self) -> Value {
|
|
186
|
+
term_to_value(ruby, rb_self.inner.object.clone())
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
fn to_s(&self) -> String {
|
|
190
|
+
self.inner.to_string()
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
fn inspect(&self) -> String {
|
|
194
|
+
inspect("Triple", &self.inner)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
fn equals(&self, other: Value) -> bool {
|
|
198
|
+
equals(self, other)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
fn hash(&self) -> u64 {
|
|
202
|
+
hash(self)
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/// Wraps a term from a query result in the Ruby class for its kind.
|
|
207
|
+
pub fn term_to_value(ruby: &Ruby, term: rdf::Term) -> Value {
|
|
208
|
+
match term {
|
|
209
|
+
rdf::Term::NamedNode(inner) => frozen(ruby, NamedNode { inner }).as_value(),
|
|
210
|
+
rdf::Term::BlankNode(inner) => frozen(ruby, BlankNode { inner }).as_value(),
|
|
211
|
+
rdf::Term::Literal(inner) => frozen(ruby, Literal { inner }).as_value(),
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/// Wraps a triple from a query result as an `Oxigraph::Triple`.
|
|
216
|
+
pub fn triple_to_value(ruby: &Ruby, inner: rdf::Triple) -> Value {
|
|
217
|
+
frozen(ruby, Triple { inner }).as_value()
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/// Wraps a term for Ruby and freezes it: terms have no mutating methods, and a
|
|
221
|
+
/// frozen object says so.
|
|
222
|
+
pub fn frozen<T: TypedData>(ruby: &Ruby, term: T) -> Obj<T> {
|
|
223
|
+
let object = ruby.obj_wrap(term);
|
|
224
|
+
object.freeze();
|
|
225
|
+
object
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/// Copying a term returns the term itself, because terms are frozen values.
|
|
229
|
+
/// Without these methods `dup` and `clone` raise, because the class has no
|
|
230
|
+
/// allocator. They take the arguments that copying an `Integer` takes.
|
|
231
|
+
fn dup(term: Value) -> Value {
|
|
232
|
+
term
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
fn clone(ruby: &Ruby, term: Value, arguments: &[Value]) -> Result<Value, Error> {
|
|
236
|
+
let arguments = scan_args::scan_args::<(), (), (), (), RHash, ()>(arguments)?;
|
|
237
|
+
let keywords =
|
|
238
|
+
scan_args::get_kwargs::<_, (), (Option<Value>,), ()>(arguments.keywords, &[], &["freeze"])?;
|
|
239
|
+
match keywords.optional.0 {
|
|
240
|
+
Some(freeze) if freeze.is_kind_of(ruby.class_false_class()) => Err(argument_error(
|
|
241
|
+
ruby,
|
|
242
|
+
format!("can't unfreeze {}", term.class()),
|
|
243
|
+
)),
|
|
244
|
+
Some(freeze) if !freeze.is_nil() && !freeze.is_kind_of(ruby.class_true_class()) => {
|
|
245
|
+
Err(argument_error(
|
|
246
|
+
ruby,
|
|
247
|
+
format!("unexpected value for freeze: {}", freeze.class()),
|
|
248
|
+
))
|
|
249
|
+
}
|
|
250
|
+
_ => Ok(term),
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
fn define_copying(class: RClass) -> Result<(), Error> {
|
|
255
|
+
class.define_method("dup", method!(dup, 0))?;
|
|
256
|
+
class.define_method("clone", method!(clone, -1))
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
fn inspect(class: &str, term: &impl Display) -> String {
|
|
260
|
+
format!("#<Oxigraph::{class} {term}>")
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/// A term equals another object only when that object is the same kind of term
|
|
264
|
+
/// with the same content.
|
|
265
|
+
fn equals<T: TypedData + PartialEq>(term: &T, other: Value) -> bool {
|
|
266
|
+
<&T>::try_convert(other).is_ok_and(|other| term == other)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/// Hashes with keys chosen once per process, as Ruby does for strings, so that
|
|
270
|
+
/// terms built from untrusted input cannot be made to collide as `Hash` keys.
|
|
271
|
+
/// The result is kept within Ruby's small-integer range.
|
|
272
|
+
fn hash(term: &impl Hash) -> u64 {
|
|
273
|
+
static KEYS: OnceLock<RandomState> = OnceLock::new();
|
|
274
|
+
KEYS.get_or_init(RandomState::new).hash_one(term) >> 2
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
fn subject_from(ruby: &Ruby, value: Value) -> Result<rdf::NamedOrBlankNode, Error> {
|
|
278
|
+
if let Ok(node) = <&NamedNode>::try_convert(value) {
|
|
279
|
+
Ok(node.inner.clone().into())
|
|
280
|
+
} else if let Ok(node) = <&BlankNode>::try_convert(value) {
|
|
281
|
+
Ok(node.inner.clone().into())
|
|
282
|
+
} else {
|
|
283
|
+
Err(type_error(
|
|
284
|
+
ruby,
|
|
285
|
+
value,
|
|
286
|
+
"Oxigraph::NamedNode or Oxigraph::BlankNode",
|
|
287
|
+
))
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
fn term_from(ruby: &Ruby, value: Value) -> Result<rdf::Term, Error> {
|
|
292
|
+
if let Ok(node) = <&NamedNode>::try_convert(value) {
|
|
293
|
+
Ok(node.inner.clone().into())
|
|
294
|
+
} else if let Ok(node) = <&BlankNode>::try_convert(value) {
|
|
295
|
+
Ok(node.inner.clone().into())
|
|
296
|
+
} else if let Ok(literal) = <&Literal>::try_convert(value) {
|
|
297
|
+
Ok(literal.inner.clone().into())
|
|
298
|
+
} else {
|
|
299
|
+
Err(type_error(
|
|
300
|
+
ruby,
|
|
301
|
+
value,
|
|
302
|
+
"Oxigraph::NamedNode, Oxigraph::BlankNode, or Oxigraph::Literal",
|
|
303
|
+
))
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
fn type_error(ruby: &Ruby, value: Value, expected: &str) -> Error {
|
|
308
|
+
Error::new(
|
|
309
|
+
ruby.exception_type_error(),
|
|
310
|
+
format!(
|
|
311
|
+
"wrong argument type {} (expected {expected})",
|
|
312
|
+
value.class()
|
|
313
|
+
),
|
|
314
|
+
)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
pub fn init(ruby: &Ruby, module: RModule) -> Result<(), Error> {
|
|
318
|
+
let class = module.define_class("NamedNode", ruby.class_object())?;
|
|
319
|
+
class.undef_default_alloc_func();
|
|
320
|
+
class.define_singleton_method("new", function!(NamedNode::new, 1))?;
|
|
321
|
+
class.define_method("value", method!(NamedNode::value, 0))?;
|
|
322
|
+
class.define_method("to_s", method!(NamedNode::to_s, 0))?;
|
|
323
|
+
class.define_method("inspect", method!(NamedNode::inspect, 0))?;
|
|
324
|
+
class.define_method("==", method!(NamedNode::equals, 1))?;
|
|
325
|
+
class.define_method("eql?", method!(NamedNode::equals, 1))?;
|
|
326
|
+
class.define_method("hash", method!(NamedNode::hash, 0))?;
|
|
327
|
+
define_copying(class)?;
|
|
328
|
+
|
|
329
|
+
let class = module.define_class("BlankNode", ruby.class_object())?;
|
|
330
|
+
class.undef_default_alloc_func();
|
|
331
|
+
class.define_singleton_method("new", function!(BlankNode::new, -1))?;
|
|
332
|
+
class.define_method("value", method!(BlankNode::value, 0))?;
|
|
333
|
+
class.define_method("to_s", method!(BlankNode::to_s, 0))?;
|
|
334
|
+
class.define_method("inspect", method!(BlankNode::inspect, 0))?;
|
|
335
|
+
class.define_method("==", method!(BlankNode::equals, 1))?;
|
|
336
|
+
class.define_method("eql?", method!(BlankNode::equals, 1))?;
|
|
337
|
+
class.define_method("hash", method!(BlankNode::hash, 0))?;
|
|
338
|
+
define_copying(class)?;
|
|
339
|
+
|
|
340
|
+
let class = module.define_class("Literal", ruby.class_object())?;
|
|
341
|
+
class.undef_default_alloc_func();
|
|
342
|
+
class.define_singleton_method("new", function!(Literal::new, -1))?;
|
|
343
|
+
class.define_method("value", method!(Literal::value, 0))?;
|
|
344
|
+
class.define_method("datatype", method!(Literal::datatype, 0))?;
|
|
345
|
+
class.define_method("language", method!(Literal::language, 0))?;
|
|
346
|
+
class.define_method("to_s", method!(Literal::to_s, 0))?;
|
|
347
|
+
class.define_method("inspect", method!(Literal::inspect, 0))?;
|
|
348
|
+
class.define_method("==", method!(Literal::equals, 1))?;
|
|
349
|
+
class.define_method("eql?", method!(Literal::equals, 1))?;
|
|
350
|
+
class.define_method("hash", method!(Literal::hash, 0))?;
|
|
351
|
+
define_copying(class)?;
|
|
352
|
+
|
|
353
|
+
let class = module.define_class("Triple", ruby.class_object())?;
|
|
354
|
+
class.undef_default_alloc_func();
|
|
355
|
+
class.define_singleton_method("new", function!(Triple::new, 3))?;
|
|
356
|
+
class.define_method("subject", method!(Triple::subject, 0))?;
|
|
357
|
+
class.define_method("predicate", method!(Triple::predicate, 0))?;
|
|
358
|
+
class.define_method("object", method!(Triple::object, 0))?;
|
|
359
|
+
class.define_method("to_s", method!(Triple::to_s, 0))?;
|
|
360
|
+
class.define_method("inspect", method!(Triple::inspect, 0))?;
|
|
361
|
+
class.define_method("==", method!(Triple::equals, 1))?;
|
|
362
|
+
class.define_method("eql?", method!(Triple::equals, 1))?;
|
|
363
|
+
class.define_method("hash", method!(Triple::hash, 0))?;
|
|
364
|
+
define_copying(class)?;
|
|
365
|
+
|
|
366
|
+
Ok(())
|
|
367
|
+
}
|