pub struct RdfParser { /* private fields */ }Expand description
Parsers for RDF serialization formats.
It currently supports the following formats:
- JSON-LD (
RdfFormat::JsonLd) - N3 (
RdfFormat::N3) - N-Quads (
RdfFormat::NQuads) - N-Triples (
RdfFormat::NTriples) - RDF/XML (
RdfFormat::RdfXml) - TriG (
RdfFormat::TriG) - Turtle (
RdfFormat::Turtle)
Note the useful options:
with_base_irito resolve the relative IRIs.rename_blank_nodesto rename the blank nodes to auto-generated numbers to avoid conflicts when merging RDF graphs together.without_named_graphsto parse a single graph.uncheckedto skip some validations if the file is already known to be valid.
use oxrdfio::{RdfFormat, RdfParser};
let file = "<http://example.com/s> <http://example.com/p> <http://example.com/o> .";
let quads = RdfParser::from_format(RdfFormat::NTriples)
.for_reader(file.as_bytes())
.collect::<Result<Vec<_>, _>>()?;
assert_eq!(quads.len(), 1);
assert_eq!(quads[0].subject.to_string(), "<http://example.com/s>");Implementations§
Source§impl RdfParser
impl RdfParser
Sourcepub fn from_format(format: RdfFormat) -> RdfParser
pub fn from_format(format: RdfFormat) -> RdfParser
Builds a parser for the given format.
Sourcepub fn format(&self) -> RdfFormat
pub fn format(&self) -> RdfFormat
The format the parser uses.
use oxrdfio::{RdfFormat, RdfParser};
assert_eq!(
RdfParser::from_format(RdfFormat::Turtle).format(),
RdfFormat::Turtle
);Sourcepub fn with_base_iri(
self,
base_iri: impl Into<String>,
) -> Result<RdfParser, IriParseError>
pub fn with_base_iri( self, base_iri: impl Into<String>, ) -> Result<RdfParser, IriParseError>
Provides an IRI that could be used to resolve the file relative IRIs.
use oxrdfio::{RdfFormat, RdfParser};
let file = "</s> </p> </o> .";
let quads = RdfParser::from_format(RdfFormat::Turtle)
.with_base_iri("http://example.com")?
.for_reader(file.as_bytes())
.collect::<Result<Vec<_>, _>>()?;
assert_eq!(quads.len(), 1);
assert_eq!(quads[0].subject.to_string(), "<http://example.com/s>");Sourcepub fn with_default_graph(
self,
default_graph: impl Into<GraphName>,
) -> RdfParser
pub fn with_default_graph( self, default_graph: impl Into<GraphName>, ) -> RdfParser
Provides the name graph name that should replace the default graph in the returned quads.
use oxrdf::NamedNode;
use oxrdfio::{RdfFormat, RdfParser};
let file = "<http://example.com/s> <http://example.com/p> <http://example.com/o> .";
let quads = RdfParser::from_format(RdfFormat::Turtle)
.with_default_graph(NamedNode::new("http://example.com/g")?)
.for_reader(file.as_bytes())
.collect::<Result<Vec<_>, _>>()?;
assert_eq!(quads.len(), 1);
assert_eq!(quads[0].graph_name.to_string(), "<http://example.com/g>");Sourcepub fn without_named_graphs(self) -> RdfParser
pub fn without_named_graphs(self) -> RdfParser
Sets that the parser must fail if parsing a named graph.
This function restricts the parser to only parse a single RDF graph and not an RDF dataset.
use oxrdfio::{RdfFormat, RdfParser};
let file = "<http://example.com/s> <http://example.com/p> <http://example.com/o> <http://example.com/g> .";
let parser = RdfParser::from_format(RdfFormat::NQuads).without_named_graphs();
assert!(parser.for_reader(file.as_bytes()).next().unwrap().is_err());Sourcepub fn rename_blank_nodes(self) -> RdfParser
pub fn rename_blank_nodes(self) -> RdfParser
Renames the blank nodes ids from the ones set in the serialization to random ids.
This allows to avoid id conflicts when merging graphs together.
use oxrdfio::{RdfFormat, RdfParser};
let file = "_:a <http://example.com/p> <http://example.com/o> .";
let result1 = RdfParser::from_format(RdfFormat::NQuads)
.rename_blank_nodes()
.for_reader(file.as_bytes())
.collect::<Result<Vec<_>, _>>()?;
let result2 = RdfParser::from_format(RdfFormat::NQuads)
.rename_blank_nodes()
.for_reader(file.as_bytes())
.collect::<Result<Vec<_>, _>>()?;
assert_ne!(result1, result2);Sourcepub fn lenient(self) -> RdfParser
pub fn lenient(self) -> RdfParser
Assumes the file is valid to make parsing faster.
It will skip some validations.
Note that if the file is actually not valid, the parser might emit broken RDF.
pub fn unchecked(self) -> RdfParser
Use lenient() instead
Sourcepub fn for_reader<R>(self, reader: R) -> ReaderQuadParser<R> ⓘwhere
R: Read,
pub fn for_reader<R>(self, reader: R) -> ReaderQuadParser<R> ⓘwhere
R: Read,
Parses from a Read implementation and returns an iterator of quads.
Reads are buffered.
use oxrdfio::{RdfFormat, RdfParser};
let file = "<http://example.com/s> <http://example.com/p> <http://example.com/o> .";
let quads = RdfParser::from_format(RdfFormat::NTriples)
.for_reader(file.as_bytes())
.collect::<Result<Vec<_>, _>>()?;
assert_eq!(quads.len(), 1);
assert_eq!(quads[0].subject.to_string(), "<http://example.com/s>");Sourcepub fn for_slice(
self,
slice: &(impl AsRef<[u8]> + ?Sized),
) -> SliceQuadParser<'_> ⓘ
pub fn for_slice( self, slice: &(impl AsRef<[u8]> + ?Sized), ) -> SliceQuadParser<'_> ⓘ
Parses from a byte slice and returns an iterator of quads.
use oxrdfio::{RdfFormat, RdfParser};
let file = "<http://example.com/s> <http://example.com/p> <http://example.com/o> .";
let quads = RdfParser::from_format(RdfFormat::NTriples)
.for_slice(file)
.collect::<Result<Vec<_>, _>>()?;
assert_eq!(quads.len(), 1);
assert_eq!(quads[0].subject.to_string(), "<http://example.com/s>");Sourcepub fn split_slice_for_parallel_parsing(
self,
slice: &(impl AsRef<[u8]> + ?Sized),
target_parallelism: usize,
) -> Vec<SliceQuadParser<'_>>
pub fn split_slice_for_parallel_parsing( self, slice: &(impl AsRef<[u8]> + ?Sized), target_parallelism: usize, ) -> Vec<SliceQuadParser<'_>>
Creates a vector of parsers that may be used to parse the document slice in parallel.
To dynamically specify target_parallelism, use e.g. std::thread::available_parallelism.
This only works for N-Triples and N-Quads and is only interesting on large documents.
use oxrdfio::{RdfFormat, RdfParser};
let file = "<http://example.com/s> <http://example.com/p> <http://example.com/o> .";
let quads = RdfParser::from_format(RdfFormat::NTriples)
.split_slice_for_parallel_parsing(file, 4)
.into_iter()
.flatten()
.collect::<Result<Vec<_>, _>>()?;
assert_eq!(quads.len(), 1);
assert_eq!(quads[0].subject.to_string(), "<http://example.com/s>");Sourcepub fn split_file_for_parallel_parsing(
self,
path: impl AsRef<Path>,
target_parallelism: usize,
) -> Result<Vec<ReaderQuadParser<Take<File>>>, Error>
pub fn split_file_for_parallel_parsing( self, path: impl AsRef<Path>, target_parallelism: usize, ) -> Result<Vec<ReaderQuadParser<Take<File>>>, Error>
Creates a vector of parsers that may be used to parse the file in parallel.
To dynamically specify target_parallelism, use e.g. std::thread::available_parallelism.
This only works for N-Triples and N-Quads and is only interesting on large documents.
use oxrdfio::{RdfFormat, RdfParser};
let quads = RdfParser::from_format(RdfFormat::NTriples)
.split_file_for_parallel_parsing(&path, 4)?
.into_iter()
.flatten()
.collect::<Result<Vec<_>, _>>()?;
assert_eq!(quads.len(), 1);
assert_eq!(quads[0].subject.to_string(), "<http://example.com/s>");