oxigraph::io

Struct RdfParser

Source
pub struct RdfParser { /* private fields */ }
Expand description

Parsers for RDF serialization formats.

It currently supports the following formats:

Note the useful options:

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

Source

pub fn from_format(format: RdfFormat) -> RdfParser

Builds a parser for the given format.

Source

pub fn format(&self) -> RdfFormat

The format the parser uses.

use oxrdfio::{RdfFormat, RdfParser};

assert_eq!(
    RdfParser::from_format(RdfFormat::Turtle).format(),
    RdfFormat::Turtle
);
Source

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>");
Source

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>");
Source

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());
Source

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);
Source

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.

Source

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>");
Source

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>");

Trait Implementations§

Source§

impl Clone for RdfParser

Source§

fn clone(&self) -> RdfParser

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl From<DatasetFormat> for RdfParser

Source§

fn from(format: DatasetFormat) -> Self

Converts to this type from the input type.
Source§

impl From<GraphFormat> for RdfParser

Source§

fn from(format: GraphFormat) -> Self

Converts to this type from the input type.
Source§

impl From<RdfFormat> for RdfParser

Source§

fn from(format: RdfFormat) -> RdfParser

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V