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
//! Interfaces for RDF parsers.

use crate::model::Triple;
use std::error::Error;

/// A parser returning [`Triple`](../model/struct.Triple.html).
pub trait TripleParser: Sized {
    type Error: Error;

    /// Parses the complete file and calls `on_triple` each time a new triple is read.
    fn parse_all(&mut self, on_triple: &mut impl FnMut(Triple) -> ()) -> Result<(), Self::Error> {
        while !self.is_end() {
            if let Err(error) = self.parse_step(on_triple) {
                return Err(error);
            }
        }
        Ok(())
    }

    /// Parses a small chunk of the file and calls `on_triple` each time a new triple is read.
    ///
    /// This method should be called as long as `is_end` returns false.
    ///
    /// I could be a line for line based formats like N-Triples...
    fn parse_step(&mut self, on_triple: &mut impl FnMut(Triple) -> ()) -> Result<(), Self::Error>;

    /// Return `true` if the complete file has been consumed by the parser.
    fn is_end(&self) -> bool;

    /// Converts the parser into a `Result<T, E>` iterator.
    ///
    /// `convert_triple` is a function converting Rio [`Triple`](../model/struct.Triple.html) to `T`.
    fn into_iter<T, E: From<Self::Error>, F: FnMut(Triple) -> Result<T, E>>(
        self,
        convert_triple: F,
    ) -> TriplesParserIterator<T, E, F, Self> {
        TriplesParserIterator {
            parser: self,
            buffer: Vec::default(),
            convert_triple,
        }
    }
}

/// Created with the method [`into_iter`](trait.TripleParser.html#method.into_iter).
pub struct TriplesParserIterator<
    T,
    E: From<P::Error>,
    F: FnMut(Triple) -> Result<T, E>,
    P: TripleParser,
> {
    parser: P,
    buffer: Vec<Result<T, E>>,
    convert_triple: F,
}

impl<T, E: From<P::Error>, F: FnMut(Triple) -> Result<T, E>, P: TripleParser> Iterator
    for TriplesParserIterator<T, E, F, P>
{
    type Item = Result<T, E>;

    fn next(&mut self) -> Option<Result<T, E>> {
        loop {
            if let Some(r) = self.buffer.pop() {
                return Some(r);
            }
            if self.parser.is_end() {
                return None;
            }

            let buffer = &mut self.buffer;
            let convert_triple = &mut self.convert_triple;
            if let Err(e) = self
                .parser
                .parse_step(&mut |t| buffer.push(convert_triple(t)))
            {
                return Some(Err(e.into()));
            }
        }
    }
}