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
//! Sophia adapter for Turtle.

use crate::{TurtleError, TurtleParser};
use rio_api::parser::ParseError;
use sophia_api::parser::{Location, WithLocation};

impl WithLocation for TurtleError {
    fn location(&self) -> Location {
        match self.textual_position() {
            None => Location::Unknown,
            Some(pos) => Location::from_lico(
                (pos.line_number() + 1) as usize,
                (pos.byte_number() + 1) as usize,
            ),
        }
    }
}

impl_triple_source!(TurtleParser);

#[cfg(test)]
mod test {
    use super::*;
    use oxiri::Iri;
    use rio_api::model::{Literal, NamedNode};
    use sophia_api::graph::Graph;
    use sophia_api::ns::rdf;
    use sophia_api::term::matcher::ANY;
    use sophia_api::term::test::TestTerm;
    use sophia_api::triple::stream::TripleSource;

    #[test]
    fn test_simple_turtle_string() -> Result<(), Box<dyn std::error::Error>> {
        let turtle = br#"
            @prefix : <http://example.org/ns/> .

            <#me> :knows [ a :Person ; :name "Alice" ].
        "#;

        let p = TurtleParser::new(
            turtle.as_ref(),
            Some(Iri::parse("http://localhost/ex".to_owned())?),
        );

        let g: Vec<[TestTerm<String>; 3]> = p.collect_triples()?;
        assert_eq!(g.len(), 3);
        assert!(g
            .triples_matching(
                &NamedNode {
                    iri: "http://localhost/ex#me"
                },
                &NamedNode {
                    iri: "http://example.org/ns/knows"
                },
                &ANY,
            )
            .next()
            .is_some());
        assert!(g
            .triples_matching(
                &ANY,
                &rdf::type_,
                &NamedNode {
                    iri: "http://example.org/ns/Person"
                },
            )
            .next()
            .is_some());
        assert!(g
            .triples_matching(
                &ANY,
                &NamedNode {
                    iri: "http://example.org/ns/name"
                },
                &Literal::Simple { value: "Alice" },
            )
            .next()
            .is_some());
        Ok(())
    }
}