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