pub struct RdfParser { /* private fields */ }
Expand description
Parsers for RDF serialization formats.
It currently supports the following formats:
- 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_iri
to resolve the relative IRIs.rename_blank_nodes
to rename the blank nodes to auto-generated numbers to avoid conflicts when merging RDF graphs together.without_named_graphs
to parse a single graph.unchecked
to 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 unchecked(self) -> RdfParser
pub fn unchecked(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, broken RDF might be emitted by the parser.
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: &[u8]) -> SliceQuadParser<'_> ⓘ
pub fn for_slice(self, slice: &[u8]) -> SliceQuadParser<'_> ⓘ
Parses from a byte slice and returns an iterator of quads.
use oxrdfio::{RdfFormat, RdfParser};
let file = b"<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>");