|
|
@ -23,7 +23,7 @@ use rio_api::formatter::{QuadsFormatter, TriplesFormatter}; |
|
|
|
use rio_api::parser::{QuadsParser, TriplesParser}; |
|
|
|
use rio_api::parser::{QuadsParser, TriplesParser}; |
|
|
|
use rio_turtle::{ |
|
|
|
use rio_turtle::{ |
|
|
|
NQuadsFormatter, NQuadsParser, NTriplesFormatter, NTriplesParser, TriGFormatter, TriGParser, |
|
|
|
NQuadsFormatter, NQuadsParser, NTriplesFormatter, NTriplesParser, TriGFormatter, TriGParser, |
|
|
|
TurtleFormatter, TurtleParser, |
|
|
|
TurtleError, TurtleFormatter, TurtleParser, |
|
|
|
}; |
|
|
|
}; |
|
|
|
use rio_xml::{RdfXmlError, RdfXmlFormatter, RdfXmlParser}; |
|
|
|
use rio_xml::{RdfXmlError, RdfXmlFormatter, RdfXmlParser}; |
|
|
|
use std::collections::HashMap; |
|
|
|
use std::collections::HashMap; |
|
|
@ -66,45 +66,45 @@ fn load_graph<S: WritableEncodedStore>( |
|
|
|
syntax: GraphSyntax, |
|
|
|
syntax: GraphSyntax, |
|
|
|
to_graph_name: &GraphName, |
|
|
|
to_graph_name: &GraphName, |
|
|
|
base_iri: Option<&str>, |
|
|
|
base_iri: Option<&str>, |
|
|
|
) -> Result<(), crate::Error> |
|
|
|
) -> Result<(), io::Error> { |
|
|
|
where |
|
|
|
|
|
|
|
crate::Error: From<<S as WritableEncodedStore>::Error> + From<<S as StrContainer>::Error>, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
let base_iri = base_iri.unwrap_or(""); |
|
|
|
let base_iri = base_iri.unwrap_or(""); |
|
|
|
match syntax { |
|
|
|
match syntax { |
|
|
|
GraphSyntax::NTriples => { |
|
|
|
GraphSyntax::NTriples => { |
|
|
|
load_from_triple_parser(store, NTriplesParser::new(reader)?, to_graph_name) |
|
|
|
load_from_triple_parser(store, NTriplesParser::new(reader), to_graph_name) |
|
|
|
} |
|
|
|
} |
|
|
|
GraphSyntax::Turtle => { |
|
|
|
GraphSyntax::Turtle => { |
|
|
|
load_from_triple_parser(store, TurtleParser::new(reader, base_iri)?, to_graph_name) |
|
|
|
load_from_triple_parser(store, TurtleParser::new(reader, base_iri), to_graph_name) |
|
|
|
} |
|
|
|
} |
|
|
|
GraphSyntax::RdfXml => { |
|
|
|
GraphSyntax::RdfXml => { |
|
|
|
load_from_triple_parser(store, RdfXmlParser::new(reader, base_iri)?, to_graph_name) |
|
|
|
load_from_triple_parser(store, RdfXmlParser::new(reader, base_iri), to_graph_name) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn load_from_triple_parser<S: WritableEncodedStore, P: TriplesParser>( |
|
|
|
fn load_from_triple_parser<S: WritableEncodedStore, P: TriplesParser>( |
|
|
|
store: &mut S, |
|
|
|
store: &mut S, |
|
|
|
mut parser: P, |
|
|
|
parser: Result<P, P::Error>, |
|
|
|
to_graph_name: &GraphName, |
|
|
|
to_graph_name: &GraphName, |
|
|
|
) -> Result<(), crate::Error> |
|
|
|
) -> Result<(), io::Error> |
|
|
|
where |
|
|
|
where |
|
|
|
crate::Error: From<P::Error> |
|
|
|
IoOrParseError<P::Error>: From<P::Error>, |
|
|
|
+ From<<S as WritableEncodedStore>::Error> |
|
|
|
P::Error: Send + Sync + 'static, |
|
|
|
+ From<<S as StrContainer>::Error>, |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
let mut parser = parser.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; |
|
|
|
let mut bnode_map = HashMap::default(); |
|
|
|
let mut bnode_map = HashMap::default(); |
|
|
|
let to_graph_name = store |
|
|
|
let to_graph_name = store |
|
|
|
.encode_graph_name(to_graph_name) |
|
|
|
.encode_graph_name(to_graph_name) |
|
|
|
.map_err(|e| e.into())?; |
|
|
|
.map_err(|e| e.into())?; |
|
|
|
parser.parse_all(&mut move |t| { |
|
|
|
let result: Result<(), IoOrParseError<_>> = parser.parse_all(&mut move |t| { |
|
|
|
let quad = store |
|
|
|
let quad = store |
|
|
|
.encode_rio_triple_in_graph(t, to_graph_name, &mut bnode_map) |
|
|
|
.encode_rio_triple_in_graph(t, to_graph_name, &mut bnode_map) |
|
|
|
.map_err(crate::Error::from)?; |
|
|
|
.map_err(|e| IoOrParseError::Io(e.into()))?; |
|
|
|
store.insert_encoded(&quad).map_err(crate::Error::from)?; |
|
|
|
store |
|
|
|
|
|
|
|
.insert_encoded(&quad) |
|
|
|
|
|
|
|
.map_err(|e| IoOrParseError::Io(e.into()))?; |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
}) |
|
|
|
}); |
|
|
|
|
|
|
|
Ok(result?) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn dump_graph( |
|
|
|
fn dump_graph( |
|
|
@ -147,34 +147,34 @@ fn load_dataset<S: WritableEncodedStore>( |
|
|
|
reader: impl BufRead, |
|
|
|
reader: impl BufRead, |
|
|
|
syntax: DatasetSyntax, |
|
|
|
syntax: DatasetSyntax, |
|
|
|
base_iri: Option<&str>, |
|
|
|
base_iri: Option<&str>, |
|
|
|
) -> Result<(), crate::Error> |
|
|
|
) -> Result<(), io::Error> { |
|
|
|
where |
|
|
|
|
|
|
|
crate::Error: From<<S as WritableEncodedStore>::Error> + From<<S as StrContainer>::Error>, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
let base_iri = base_iri.unwrap_or(""); |
|
|
|
let base_iri = base_iri.unwrap_or(""); |
|
|
|
match syntax { |
|
|
|
match syntax { |
|
|
|
DatasetSyntax::NQuads => load_from_quad_parser(store, NQuadsParser::new(reader)?), |
|
|
|
DatasetSyntax::NQuads => load_from_quad_parser(store, NQuadsParser::new(reader)), |
|
|
|
DatasetSyntax::TriG => load_from_quad_parser(store, TriGParser::new(reader, base_iri)?), |
|
|
|
DatasetSyntax::TriG => load_from_quad_parser(store, TriGParser::new(reader, base_iri)), |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn load_from_quad_parser<S: WritableEncodedStore, P: QuadsParser>( |
|
|
|
fn load_from_quad_parser<S: WritableEncodedStore, P: QuadsParser>( |
|
|
|
store: &mut S, |
|
|
|
store: &mut S, |
|
|
|
mut parser: P, |
|
|
|
parser: Result<P, P::Error>, |
|
|
|
) -> Result<(), crate::Error> |
|
|
|
) -> Result<(), io::Error> |
|
|
|
where |
|
|
|
where |
|
|
|
crate::Error: From<P::Error> |
|
|
|
IoOrParseError<P::Error>: From<P::Error>, |
|
|
|
+ From<<S as WritableEncodedStore>::Error> |
|
|
|
P::Error: Send + Sync + 'static, |
|
|
|
+ From<<S as StrContainer>::Error>, |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
let mut parser = parser.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; |
|
|
|
let mut bnode_map = HashMap::default(); |
|
|
|
let mut bnode_map = HashMap::default(); |
|
|
|
parser.parse_all(&mut move |q| { |
|
|
|
let result: Result<(), IoOrParseError<_>> = parser.parse_all(&mut move |q| { |
|
|
|
let quad = store |
|
|
|
let quad = store |
|
|
|
.encode_rio_quad(q, &mut bnode_map) |
|
|
|
.encode_rio_quad(q, &mut bnode_map) |
|
|
|
.map_err(crate::Error::from)?; |
|
|
|
.map_err(|e| IoOrParseError::Io(e.into()))?; |
|
|
|
store.insert_encoded(&quad).map_err(crate::Error::from)?; |
|
|
|
store |
|
|
|
|
|
|
|
.insert_encoded(&quad) |
|
|
|
|
|
|
|
.map_err(|e| IoOrParseError::Io(e.into()))?; |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
}) |
|
|
|
}); |
|
|
|
|
|
|
|
Ok(result?) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn dump_dataset( |
|
|
|
fn dump_dataset( |
|
|
@ -200,3 +200,35 @@ fn dump_dataset( |
|
|
|
} |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum IoOrParseError<E: Sized> { |
|
|
|
|
|
|
|
Io(io::Error), |
|
|
|
|
|
|
|
Parse(E), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<E: Sized> From<io::Error> for IoOrParseError<E> { |
|
|
|
|
|
|
|
fn from(error: io::Error) -> Self { |
|
|
|
|
|
|
|
Self::Io(error) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl From<TurtleError> for IoOrParseError<TurtleError> { |
|
|
|
|
|
|
|
fn from(error: TurtleError) -> Self { |
|
|
|
|
|
|
|
Self::Parse(error) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl From<RdfXmlError> for IoOrParseError<RdfXmlError> { |
|
|
|
|
|
|
|
fn from(error: RdfXmlError) -> Self { |
|
|
|
|
|
|
|
Self::Parse(error) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<E: Sized + Error + Send + Sync + 'static> From<IoOrParseError<E>> for io::Error { |
|
|
|
|
|
|
|
fn from(error: IoOrParseError<E>) -> Self { |
|
|
|
|
|
|
|
match error { |
|
|
|
|
|
|
|
IoOrParseError::Io(error) => error, |
|
|
|
|
|
|
|
IoOrParseError::Parse(error) => io::Error::new(io::ErrorKind::InvalidData, error), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|