rio_turtle 0.8.5

RDF Turtle, Trig, N-Triples and N-Quads parsers and serializers
Documentation
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! I define [`GeneralizedTripleAllocator`]

#![allow(unsafe_code)]
use crate::utils::StringBufferStack;
use rio_api::model::*;
use std::mem::transmute;

/// A stack allocator for storing generalized RDF and RDF-star triples.
///
/// # Implementation
/// This type uses `&'static str` internally to reference text that it allocates.
/// It therefore contains unsafe code to cheat the borrow checker,
/// but ensures that the referenced data lives as long as the referencing struct.
pub struct GeneralizedTripleAllocator {
    incomplete_stack: Vec<[GeneralizedTerm<'static>; 3]>,
    incomplete_len: usize,
    #[allow(clippy::vec_box)]
    complete_stack: Vec<Box<[GeneralizedTerm<'static>; 3]>>,
    complete_len: usize,
    string_stack: StringBufferStack,
}

impl GeneralizedTripleAllocator {
    pub fn new() -> GeneralizedTripleAllocator {
        GeneralizedTripleAllocator {
            incomplete_stack: Vec::with_capacity(1),
            incomplete_len: 0,
            complete_stack: Vec::with_capacity(1),
            complete_len: 0,
            string_stack: StringBufferStack::with_capacity(4),
        }
    }

    pub fn complete_len(&self) -> usize {
        self.complete_len
    }

    pub fn incomplete_len(&self) -> usize {
        self.incomplete_len
    }

    /// Return the last completed triple
    pub fn top(&self) -> &[GeneralizedTerm<'_>; 3] {
        debug_assert!(self.complete_len > 0);
        &self.complete_stack[self.complete_len - 1]
    }

    /// Return the subject of the current (in-progress) triple, if any.
    pub fn current_subject(&self) -> Option<GeneralizedTerm> {
        if self.incomplete_len == 0 {
            None
        } else {
            debug_assert!(!dummy(self.incomplete_stack[self.incomplete_len - 1][0]));
            Some(self.incomplete_stack[self.incomplete_len - 1][0])
        }
    }

    /// Generate a quad from the top completed triple,
    /// with the given graph name.
    pub fn top_quad<'s>(&'s self, graph_name: Option<GeneralizedTerm<'s>>) -> GeneralizedQuad<'s> {
        debug_assert!(self.complete_len > 0);
        let [subject, predicate, object] = *self.top();
        GeneralizedQuad {
            subject,
            predicate,
            object,
            graph_name,
        }
    }

    pub fn push_triple_start(&mut self) {
        let dummy: GeneralizedTerm = DUMMY_IRI.into();
        if self.incomplete_len == self.incomplete_stack.len() {
            self.incomplete_stack.push([dummy, dummy, dummy])
        }
        #[cfg(debug_assertions)] // to ensure that dummy() assertions work
        {
            self.incomplete_stack[self.incomplete_len] = [dummy, dummy, dummy];
        }
        self.incomplete_len += 1;
    }

    /// Push an atomic term,
    /// produced by `subject_factory`,
    /// at the position `pos` of the current triple.
    ///
    /// # Pre-condition
    /// In standard RDF, any term is acceptable.
    /// In RDF-star embedded triples [`GeneralizedTerm::Triple`] are *not allowed*.
    /// For adding an embedded triple, use [`TripleAllocator::push_quoted_triple`] instead.
    pub fn try_push_atom<E, F>(&mut self, pos: usize, term_factory: F) -> Result<(), E>
    where
        F: for<'x> FnOnce(&'x mut String, &'x mut String) -> Result<GeneralizedTerm<'x>, E>,
    {
        debug_assert!(pos < 3);
        debug_assert!(pos == 0 || !dummy(self.current()[pos - 1]));
        debug_assert!(dummy(self.current()[pos]));
        let buffers = self.string_stack.push2();
        let atom = term_factory(buffers.0, buffers.1)?;
        debug_assert!(!matches!(atom, GeneralizedTerm::Triple(_)));
        let atom: GeneralizedTerm<'static> = unsafe { transmute(atom) };
        // The unsafe code above changes the lifetime parameter of atom to `'static`.
        // This is ok because:
        // * we will only expose it with a shorter lifetime, and
        // * this implementation guarantees that the pointed `str` lives as long as the atom
        if pos < 2 {
            self.current()[pos] = atom;
        } else {
            self.complete_triple(atom);
        }
        Ok(())
    }

    /// Use the [top](TripleAllocator::top) triple of this stash at the position `pos` of the current triple.
    pub fn push_quoted_triple(&mut self, pos: usize) {
        debug_assert!(pos < 3);
        debug_assert!(dummy(self.current()[pos]));
        debug_assert!(self.complete_len > 0);
        let triple = &*self.complete_stack[self.complete_len - 1];

        debug_assert!((0..pos).all(
            |i| match self.incomplete_stack[self.incomplete_len - 1][i] {
                GeneralizedTerm::Triple(s) => {
                    let ptr_s: *const _ = s;
                    ptr_s != triple
                }
                _ => true,
            }
        ));

        let triple: &'static [GeneralizedTerm<'static>; 3] = unsafe { transmute(triple) };
        // The unsafe code above changes the lifetime of the ref to `'static`.
        // This is ok because:
        // * we will only expose it with a shorter lifetime, and
        // * this implementation guarantees that the pointed `Triple` lives as long as the `Term` embedding it
        let quoted = GeneralizedTerm::Triple(triple);
        if pos < 2 {
            self.current()[pos] = quoted;
        } else {
            self.complete_triple(quoted);
        }
    }

    pub fn pop_term(&mut self, pos: usize) {
        debug_assert!(pos < 3);
        if pos == 2 {
            debug_assert!(self.complete_len > 0);
            self.complete_len -= 1;
            let inc_triple = *self.complete_stack[self.complete_len];
            if self.incomplete_len == self.incomplete_stack.len() {
                self.incomplete_stack.push(inc_triple)
            } else {
                self.incomplete_stack[self.incomplete_len] = inc_triple;
            }
            self.incomplete_len += 1;
        } else {
            debug_assert!(dummy(self.current()[pos + 1]));
        };

        match self.current()[pos] {
            GeneralizedTerm::Triple(_) => self.pop_top_triple(),
            _ => {
                // we allocate two buffers for any atomic object, not just literals
                self.string_stack.pop();
                self.string_stack.pop()
            }
        }
        #[cfg(debug_assertions)] // to ensure that dummy() assertions work
        {
            self.current()[pos] = DUMMY_IRI.into();
        }
    }

    #[inline(always)]
    /// Pops the latest complete triple, and recursively pops all its constituent triples.
    /// Equivalent to pop_object, pop_predicate, pop_subject, pop_empty_triple
    pub fn pop_top_triple(&mut self) {
        self.pop_term(2);
        self.pop_term(1);
        self.pop_term(0);
        self.incomplete_len -= 1;
    }

    /// Pops the top-most empty triple, created with push_triple_start,
    /// but with all its components having been popped (or never pushed)
    #[inline(always)]
    pub fn pop_top_empty_triple(&mut self) {
        debug_assert!(self.incomplete_len > 0);
        debug_assert!(dummy(self.current()[1]));
        debug_assert!(dummy(self.current()[0]));
        self.incomplete_len -= 1;
    }

    /// Pops the top-most annotation triple, i.e.
    /// a triple on the incomplete stack with only its subject pushed,
    /// and that subject is an embedded triple.
    ///
    /// The goal is to remove this triple *without* freeing the subject triple.
    #[inline(always)]
    pub fn pop_annotation_triple(&mut self) {
        debug_assert!(self.incomplete_len > 0);
        debug_assert!(dummy(self.current()[1]));
        debug_assert!(!dummy(self.current()[0]));
        debug_assert!(matches!(self.current()[0], GeneralizedTerm::Triple(_)));
        self.incomplete_len -= 1;
    }

    pub fn clear(&mut self) {
        self.incomplete_len = 0;
        self.incomplete_stack.clear();
        self.complete_len = 0;
        self.complete_stack.clear();
        self.string_stack.clear();
    }

    fn complete_triple(&mut self, object: GeneralizedTerm<'static>) {
        self.incomplete_len -= 1;
        let mut triple = self.incomplete_stack[self.incomplete_len];
        triple[2] = object;
        if self.complete_len == self.complete_stack.len() {
            self.complete_stack.push(Box::new(triple))
        } else {
            *self.complete_stack[self.complete_len] = triple;
        }
        self.complete_len += 1;
    }

    fn current(&mut self) -> &mut [GeneralizedTerm<'static>; 3] {
        debug_assert!(self.incomplete_len > 0);
        &mut self.incomplete_stack[self.incomplete_len - 1]
    }
}

#[cfg(debug_assertions)] // debug assertions need DUMMY to have a static address
static DUMMY_IRI: NamedNode<'static> = NamedNode { iri: "" };
#[cfg(not(debug_assertions))] // otherwise, a const is sufficient
const DUMMY_IRI: NamedNode<'static> = NamedNode { iri: "" };

fn dummy<'a, T: std::fmt::Debug + Into<GeneralizedTerm<'a>>>(t: T) -> bool {
    match t.into() {
        GeneralizedTerm::NamedNode(n) => {
            let ptr_iri: *const str = n.iri;
            ptr_iri == DUMMY_IRI.iri
        }
        _ => false,
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::convert::Infallible;

    #[cfg(debug_assertions)]
    #[test]
    fn dummy_works() {
        assert!(dummy(DUMMY_IRI));
        let b = "foo".to_string();
        let n = NamedNode { iri: &b[..0] };
        assert!(DUMMY_IRI.iri == n.iri);
        // and yet:
        assert!(!dummy(n));
    }

    fn iri<'a, T: From<NamedNode<'a>>>(
        buffer: &'a mut String,
        value: &str,
    ) -> Result<T, Infallible> {
        buffer.push_str(value);
        Ok(NamedNode { iri: &*buffer }.into())
    }

    fn bn<'a, T: From<BlankNode<'a>>>(
        buffer: &'a mut String,
        value: &str,
    ) -> Result<T, Infallible> {
        buffer.push_str(value);
        Ok(BlankNode { id: &*buffer }.into())
    }

    fn sl<'a, T: From<Literal<'a>>>(buffer: &'a mut String, value: &str) -> Result<T, Infallible> {
        buffer.push_str(value);
        Ok(Literal::Simple { value: &*buffer }.into())
    }

    fn lt<'a, T: From<Literal<'a>>>(
        buffer1: &'a mut String,
        buffer2: &'a mut String,
        value: &str,
        tag: &str,
    ) -> Result<T, Infallible> {
        buffer1.push_str(value);
        buffer2.push_str(tag);
        Ok(Literal::LanguageTaggedString {
            value: &*buffer1,
            language: &*buffer2,
        }
        .into())
    }

    fn dt<'a, T: From<Literal<'a>>>(
        buffer1: &'a mut String,
        buffer2: &'a mut String,
        value: &str,
        dt: &str,
    ) -> Result<T, Infallible> {
        buffer1.push_str(value);
        buffer2.push_str(dt);
        Ok(Literal::Typed {
            value: &*buffer1,
            datatype: NamedNode { iri: &*buffer2 },
        }
        .into())
    }

    #[test]
    fn simple_triple_w_named() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        ta.try_push_atom(0, |b, _| iri(b, "a"))?;
        ta.try_push_atom(1, |b, _| iri(b, "b"))?;
        ta.try_push_atom(2, |b, _| iri(b, "c"))?;
        assert_eq!(display(ta.top()), r#"<a> <b> <c>"#);
        Ok(())
    }

    #[test]
    fn simple_triple_w_blank() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        ta.try_push_atom(0, |b, _| bn(b, "a"))?;
        ta.try_push_atom(1, |b, _| bn(b, "b"))?;
        ta.try_push_atom(2, |b, _| bn(b, "c"))?;
        assert_eq!(display(ta.top()), r#"_:a _:b _:c"#);
        Ok(())
    }

    #[test]
    fn simple_triple_w_simple_lit() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        ta.try_push_atom(0, |b, _| sl(b, "a"))?;
        ta.try_push_atom(1, |b, _| sl(b, "b"))?;
        ta.try_push_atom(2, |b, _| sl(b, "c"))?;
        assert_eq!(display(ta.top()), r#""a" "b" "c""#);
        Ok(())
    }

    #[test]
    fn simple_triple_w_lang_lit() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        ta.try_push_atom(0, |b1, b2| lt(b1, b2, "a", "en"))?;
        ta.try_push_atom(1, |b1, b2| lt(b1, b2, "b", "fr"))?;
        ta.try_push_atom(2, |b1, b2| lt(b1, b2, "c", "ge"))?;
        assert_eq!(display(ta.top()), r#""a"@en "b"@fr "c"@ge"#);
        Ok(())
    }

    #[test]
    fn simple_triple_w_typed_lit() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        ta.try_push_atom(0, |b1, b2| dt(b1, b2, "a", "b"))?;
        ta.try_push_atom(1, |b1, b2| dt(b1, b2, "c", "d"))?;
        ta.try_push_atom(2, |b1, b2| dt(b1, b2, "e", "f"))?;
        assert_eq!(display(ta.top()), r#""a"^^<b> "c"^^<d> "e"^^<f>"#);
        Ok(())
    }

    #[test]
    fn simple_triples_pop() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        ta.try_push_atom(0, |b, _| iri(b, "a"))?;
        ta.try_push_atom(1, |b, _| iri(b, "b"))?;
        ta.try_push_atom(2, |b, _| iri(b, "c"))?;
        assert_eq!(display(ta.top()), r#"<a> <b> <c>"#);
        ta.pop_term(2);
        ta.try_push_atom(2, |b, _| iri(b, "d"))?;
        assert_eq!(display(ta.top()), r#"<a> <b> <d>"#);
        ta.pop_term(2);
        ta.pop_term(1);
        ta.try_push_atom(1, |b, _| iri(b, "e"))?;
        ta.try_push_atom(2, |b, _| iri(b, "f"))?;
        assert_eq!(display(ta.top()), r#"<a> <e> <f>"#);
        ta.pop_term(2);
        ta.try_push_atom(2, |b, _| iri(b, "g"))?;
        assert_eq!(display(ta.top()), r#"<a> <e> <g>"#);
        ta.pop_term(2);
        ta.pop_term(1);
        ta.pop_term(0);
        ta.try_push_atom(0, |b, _| iri(b, "h"))?;
        ta.try_push_atom(1, |b, _| iri(b, "i"))?;
        ta.try_push_atom(2, |b, _| iri(b, "j"))?;
        assert_eq!(display(ta.top()), r#"<h> <i> <j>"#);
        Ok(())
    }

    #[test]
    fn simple_triples_stacked() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        ta.try_push_atom(0, |b, _| iri(b, "a"))?;
        ta.try_push_atom(1, |b, _| iri(b, "b"))?;
        ta.try_push_atom(2, |b, _| iri(b, "c"))?;
        assert_eq!(display(ta.top()), r#"<a> <b> <c>"#);
        ta.push_triple_start();
        ta.try_push_atom(0, |b, _| iri(b, "d"))?;
        ta.try_push_atom(1, |b, _| iri(b, "e"))?;
        ta.try_push_atom(2, |b, _| iri(b, "f"))?;
        assert_eq!(display(ta.top()), r#"<d> <e> <f>"#);
        ta.pop_top_triple();
        assert_eq!(display(ta.top()), r#"<a> <b> <c>"#);
        ta.pop_top_triple();
        assert_eq!(ta.complete_len, 0);
        assert_eq!(ta.incomplete_len, 0);
        Ok(())
    }

    #[test]
    fn quoted_triple_as_subject() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        {
            ta.push_triple_start();
            ta.try_push_atom(0, |b, _| bn(b, "a"))?;
            ta.try_push_atom(1, |b, _| iri(b, "b"))?;
            ta.try_push_atom(2, |b, _| sl(b, "c"))?;
            assert_eq!(display(ta.top()), r#"_:a <b> "c""#);
        }
        ta.push_quoted_triple(0);
        ta.try_push_atom(1, |b, _| iri(b, "d"))?;
        ta.try_push_atom(2, |b, _| sl(b, "e"))?;
        assert_eq!(display(ta.top()), r#"<< _:a <b> "c" >> <d> "e""#);
        Ok(())
    }

    #[test]
    fn quoted_triple_as_predicate() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        ta.try_push_atom(0, |b, _| bn(b, "a"))?;
        {
            ta.push_triple_start();
            ta.try_push_atom(0, |b, _| bn(b, "b"))?;
            ta.try_push_atom(1, |b, _| iri(b, "c"))?;
            ta.try_push_atom(2, |b, _| sl(b, "d"))?;
            assert_eq!(display(ta.top()), r#"_:b <c> "d""#);
        }
        ta.push_quoted_triple(1);
        ta.try_push_atom(2, |b, _| bn(b, "e"))?;
        assert_eq!(display(ta.top()), r#"_:a << _:b <c> "d" >> _:e"#);
        Ok(())
    }

    #[test]
    fn quoted_triple_as_object() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        ta.try_push_atom(0, |b, _| bn(b, "a"))?;
        ta.try_push_atom(1, |b, _| iri(b, "b"))?;
        {
            ta.push_triple_start();
            ta.try_push_atom(0, |b, _| bn(b, "c"))?;
            ta.try_push_atom(1, |b, _| iri(b, "d"))?;
            ta.try_push_atom(2, |b, _| sl(b, "e"))?;
            assert_eq!(display(ta.top()), r#"_:c <d> "e""#);
        }
        ta.push_quoted_triple(2);
        assert_eq!(display(ta.top()), r#"_:a <b> << _:c <d> "e" >>"#);
        Ok(())
    }

    #[test]
    fn quoted_triple_as_both() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        {
            ta.push_triple_start();
            ta.try_push_atom(0, |b, _| bn(b, "a"))?;
            ta.try_push_atom(1, |b, _| iri(b, "b"))?;
            ta.try_push_atom(2, |b, _| sl(b, "c"))?;
            assert_eq!(display(ta.top()), r#"_:a <b> "c""#);
        }
        ta.push_quoted_triple(0);
        ta.try_push_atom(1, |b, _| iri(b, "d"))?;
        {
            ta.push_triple_start();
            ta.try_push_atom(0, |b, _| bn(b, "e"))?;
            ta.try_push_atom(1, |b, _| iri(b, "f"))?;
            ta.try_push_atom(2, |b, _| sl(b, "g"))?;
            assert_eq!(display(ta.top()), r#"_:e <f> "g""#);
        }
        ta.push_quoted_triple(2);
        assert_eq!(
            display(ta.top()),
            r#"<< _:a <b> "c" >> <d> << _:e <f> "g" >>"#
        );
        Ok(())
    }

    #[test]
    fn quoted_triple_deep() -> Result<(), Infallible> {
        let mut ta = GeneralizedTripleAllocator::new();
        ta.push_triple_start();
        {
            ta.push_triple_start();
            ta.try_push_atom(0, |b, _| bn(b, "a"))?;
            ta.try_push_atom(1, |b, _| iri(b, "b"))?;
            {
                ta.push_triple_start();
                ta.try_push_atom(0, |b, _| bn(b, "c"))?;
                ta.try_push_atom(1, |b, _| iri(b, "d"))?;
                ta.try_push_atom(2, |b, _| sl(b, "e"))?;
                assert_eq!(display(ta.top()), r#"_:c <d> "e""#);
            }
            ta.push_quoted_triple(2);
            assert_eq!(display(ta.top()), r#"_:a <b> << _:c <d> "e" >>"#);
        }
        ta.push_quoted_triple(0);
        ta.try_push_atom(1, |b, _| iri(b, "f"))?;
        {
            ta.push_triple_start();
            {
                ta.push_triple_start();
                ta.try_push_atom(0, |b, _| bn(b, "g"))?;
                ta.try_push_atom(1, |b, _| iri(b, "h"))?;
                ta.try_push_atom(2, |b, _| sl(b, "i"))?;
                assert_eq!(display(ta.top()), r#"_:g <h> "i""#);
            }
            ta.push_quoted_triple(0);
            ta.try_push_atom(1, |b, _| iri(b, "j"))?;
            ta.try_push_atom(2, |b, _| sl(b, "k"))?;
            assert_eq!(display(ta.top()), r#"<< _:g <h> "i" >> <j> "k""#);
        }
        ta.push_quoted_triple(2);
        assert_eq!(
            display(ta.top()),
            r#"<< _:a <b> << _:c <d> "e" >> >> <f> << << _:g <h> "i" >> <j> "k" >>"#
        );

        ta.pop_top_triple();
        assert_eq!(ta.complete_len, 0);
        assert_eq!(ta.incomplete_len, 0);
        Ok(())
    }

    fn display(triple: &[GeneralizedTerm<'_>; 3]) -> String {
        format!("{} {} {}", triple[0], triple[1], triple[2])
    }
}