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
/*!
Provides the `NTripleReader` implementation of the `GraphReader` trait.

# Example

```rust
use rdftk_core::simple::graph_factory;
use rdftk_io::nt::reader::NTriplesReader;
use rdftk_io::GraphReader;
use std::fs::File;
use std::path::PathBuf;

let file_path = PathBuf::from("tests/w3c/nt/literal.nt");
let mut file = File::open(file_path).unwrap();
let reader = NTriplesReader::default();
assert!(reader.read(&mut file, graph_factory()).is_ok());
```

*/

use crate::nt::parser;
use crate::GraphReader;
use rdftk_core::error::Result;
use rdftk_core::model::graph::{GraphFactoryRef, GraphRef};
use std::io::Read;

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

///
/// An implementation of the GraphReader trait to read resources in the NTriples representation.
///
#[derive(Clone, Debug)]
pub struct NTriplesReader {}

// ------------------------------------------------------------------------------------------------
// Private Types
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Public Functions
// ------------------------------------------------------------------------------------------------

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

impl Default for NTriplesReader {
    fn default() -> Self {
        Self {}
    }
}

impl GraphReader for NTriplesReader {
    fn read(&self, r: &mut impl Read, factory: GraphFactoryRef) -> Result<GraphRef> {
        let mut content: String = String::new();
        let _ = r.read_to_string(&mut content).map_err(io_error)?;
        parser::parse_graph(&content, factory)
    }
}

// ------------------------------------------------------------------------------------------------
// Private Functions
// ------------------------------------------------------------------------------------------------

fn io_error(e: std::io::Error) -> rdftk_core::error::Error {
    use rdftk_core::error::ErrorKind;
    rdftk_core::error::Error::with_chain(e, ErrorKind::ReadWrite(super::NAME.to_string()))
}

// ------------------------------------------------------------------------------------------------
// Modules
// ------------------------------------------------------------------------------------------------