pub(crate) mod helpers;
mod jsonld;
mod ntriples;
mod rdfxml;
mod turtle;
use super::error::ParseResult;
use super::format::RdfFormat;
use crate::model::Quad;
use std::io::Read;
pub type QuadParseResult = ParseResult<Quad>;
pub struct ReaderQuadParser<'a, R: Read> {
inner: Box<dyn Iterator<Item = QuadParseResult> + Send + 'a>,
_phantom: std::marker::PhantomData<R>,
}
impl<'a, R: Read> ReaderQuadParser<'a, R> {
pub fn new(iter: Box<dyn Iterator<Item = QuadParseResult> + Send + 'a>) -> Self {
Self {
inner: iter,
_phantom: std::marker::PhantomData,
}
}
}
impl<'a, R: Read> Iterator for ReaderQuadParser<'a, R> {
type Item = QuadParseResult;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
pub struct SliceQuadParser<'a> {
inner: Box<dyn Iterator<Item = QuadParseResult> + 'a>,
}
impl<'a> SliceQuadParser<'a> {
pub fn new(iter: Box<dyn Iterator<Item = QuadParseResult> + 'a>) -> Self {
Self { inner: iter }
}
}
impl<'a> Iterator for SliceQuadParser<'a> {
type Item = QuadParseResult;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
#[derive(Debug, Clone)]
pub struct RdfParser {
format: RdfFormat,
base_iri: Option<String>,
prefixes: std::collections::HashMap<String, String>,
lenient: bool,
}
impl RdfParser {
pub fn new(format: RdfFormat) -> Self {
Self {
format,
base_iri: None,
prefixes: std::collections::HashMap::new(),
lenient: false,
}
}
pub fn with_base_iri(mut self, base_iri: impl Into<String>) -> Self {
self.base_iri = Some(base_iri.into());
self
}
pub fn with_prefix(mut self, prefix: impl Into<String>, iri: impl Into<String>) -> Self {
self.prefixes.insert(prefix.into(), iri.into());
self
}
pub fn lenient(mut self) -> Self {
self.lenient = true;
self
}
pub fn for_reader<R: Read + Send + 'static>(self, reader: R) -> ReaderQuadParser<'static, R> {
match self.format {
RdfFormat::Turtle => turtle::parse_reader(self, reader),
RdfFormat::NTriples => ntriples::parse_ntriples_reader(self, reader),
RdfFormat::NQuads => ntriples::parse_nquads_reader(self, reader),
RdfFormat::TriG => ntriples::parse_trig_reader(self, reader),
RdfFormat::RdfXml => rdfxml::parse_reader(self, reader),
RdfFormat::JsonLd { .. } => jsonld::parse_reader(self, reader),
RdfFormat::N3 => ntriples::parse_n3_reader(self, reader),
}
}
pub fn for_slice<'a>(self, slice: &'a [u8]) -> SliceQuadParser<'a> {
match self.format {
RdfFormat::Turtle => turtle::parse_slice(self, slice),
RdfFormat::NTriples => ntriples::parse_ntriples_slice(self, slice),
RdfFormat::NQuads => ntriples::parse_nquads_slice(self, slice),
RdfFormat::TriG => ntriples::parse_trig_slice(self, slice),
RdfFormat::RdfXml => rdfxml::parse_slice(self, slice),
RdfFormat::JsonLd { .. } => jsonld::parse_slice(self, slice),
RdfFormat::N3 => ntriples::parse_n3_slice(self, slice),
}
}
pub fn format(&self) -> RdfFormat {
self.format.clone()
}
pub fn base_iri(&self) -> Option<&str> {
self.base_iri.as_deref()
}
pub fn prefixes(&self) -> &std::collections::HashMap<String, String> {
&self.prefixes
}
pub fn is_lenient(&self) -> bool {
self.lenient
}
}
impl Default for RdfParser {
fn default() -> Self {
Self::new(RdfFormat::Turtle)
}
}