use super::expansion::JsonLdExpansionConverter;
use super::profile::{JsonLdProcessingMode, JsonLdProfile, JsonLdProfileSet};
use super::to_rdf_converter::{JsonLdToRdfConverter, JsonLdToRdfState};
#[cfg(feature = "async")]
use super::to_rdf_readers::TokioAsyncReaderJsonLdParser;
use super::to_rdf_readers::{InternalJsonLdParser, ReaderJsonLdParser, SliceJsonLdParser};
use crate::model::*;
#[cfg(feature = "async")]
use json_event_parser::TokioAsyncReaderJsonParser;
use json_event_parser::{ReaderJsonParser, SliceJsonParser};
use oxiri::{Iri, IriParseError};
use std::io::Read;
#[cfg(feature = "async")]
use tokio::io::AsyncRead;
#[derive(Default, Clone)]
#[must_use]
pub struct JsonLdParser {
processing_mode: JsonLdProcessingMode,
lenient: bool,
profile: JsonLdProfileSet,
base: Option<Iri<String>>,
}
impl JsonLdParser {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn lenient(mut self) -> Self {
self.lenient = true;
self
}
#[inline]
pub fn with_profile(mut self, profile: impl Into<JsonLdProfileSet>) -> Self {
self.profile = profile.into();
self
}
#[inline]
#[doc(hidden)] pub fn with_processing_mode(mut self, processing_mode: JsonLdProcessingMode) -> Self {
self.processing_mode = processing_mode;
self
}
#[inline]
pub fn with_base_iri(mut self, base_iri: impl Into<String>) -> Result<Self, IriParseError> {
self.base = Some(Iri::parse(base_iri.into())?);
Ok(self)
}
pub fn for_reader<R: Read>(self, reader: R) -> ReaderJsonLdParser<R> {
ReaderJsonLdParser {
results: Vec::new(),
errors: Vec::new(),
inner: self.into_inner(),
json_parser: ReaderJsonParser::new(reader),
}
}
#[cfg(feature = "async")]
pub fn for_tokio_async_reader<R: AsyncRead + Unpin>(
self,
reader: R,
) -> TokioAsyncReaderJsonLdParser<R> {
TokioAsyncReaderJsonLdParser {
results: Vec::new(),
errors: Vec::new(),
inner: self.into_inner(),
json_parser: TokioAsyncReaderJsonParser::new(reader),
}
}
pub fn for_slice(self, slice: &[u8]) -> SliceJsonLdParser<'_> {
SliceJsonLdParser {
results: Vec::new(),
errors: Vec::new(),
inner: self.into_inner(),
json_parser: SliceJsonParser::new(slice),
}
}
fn into_inner(self) -> InternalJsonLdParser {
InternalJsonLdParser {
expansion: JsonLdExpansionConverter::new(
self.base,
self.profile.contains(JsonLdProfile::Streaming),
self.lenient,
self.processing_mode,
),
expended_events: Vec::new(),
to_rdf: JsonLdToRdfConverter {
state: vec![JsonLdToRdfState::Graph(Some(GraphName::DefaultGraph))],
lenient: self.lenient,
},
json_error: false,
}
}
}