Struct NTriplesParser

Source
pub struct NTriplesParser<R: BufRead> { /* private fields */ }
Expand description

A N-Triples and N-Triples-star streaming parser.

It implements the TriplesParser trait.

Its memory consumption is linear in the size of the longest line of the file. It does not do any allocation during parsing except buffer resizing if a line significantly longer than the previous is encountered, or if a line uses deeply nested triples.

Count the number of people using the TriplesParser API:

use rio_turtle::{NTriplesParser, TurtleError};
use rio_api::parser::TriplesParser;
use rio_api::model::NamedNode;

let file = b"<http://example.com/foo> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .
<http://example.com/foo> <http://schema.org/name> \"Foo\" .
<http://example.com/bar> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://schema.org/Person> .
<http://example.com/bar> <http://schema.org/name> \"Bar\" .";

let rdf_type = NamedNode { iri: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" };
let schema_person = NamedNode { iri: "http://schema.org/Person" };
let mut count = 0;
NTriplesParser::new(file.as_ref()).parse_all(&mut |t| {
    if t.predicate == rdf_type && t.object == schema_person.into() {
        count += 1;
    }
    Ok(()) as Result<(), TurtleError>
})?;
assert_eq!(2, count);

Implementations§

Source§

impl<R: BufRead> NTriplesParser<R>

Source

pub fn new(reader: R) -> Self

Trait Implementations§

Source§

impl<R: BufRead> TriplesParser for NTriplesParser<R>

Source§

type Error = TurtleError

Source§

fn parse_step<E: From<TurtleError>>( &mut self, on_triple: &mut impl FnMut(Triple<'_>) -> Result<(), E>, ) -> Result<(), E>

Parses a small chunk of the file and calls on_triple each time a new triple is read. (A “small chunk” could be a line for an N-Triples parser.) Read more
Source§

fn is_end(&self) -> bool

Returns true if the file has been completely consumed by the parser.
Source§

fn parse_all<E>( &mut self, on_triple: &mut impl FnMut(Triple<'_>) -> Result<(), E>, ) -> Result<(), E>
where E: From<Self::Error>,

Parses the complete file and calls on_triple each time a new triple is read. Read more
Source§

fn into_iter<T, E, F>( self, convert_triple: F, ) -> TriplesParserIterator<T, E, F, Self>
where E: From<Self::Error>, F: FnMut(Triple<'_>) -> Result<T, E>,

Converts the parser into a Result<T, E> iterator. Read more

Auto Trait Implementations§

§

impl<R> Freeze for NTriplesParser<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for NTriplesParser<R>
where R: RefUnwindSafe,

§

impl<R> Send for NTriplesParser<R>
where R: Send,

§

impl<R> Sync for NTriplesParser<R>
where R: Sync,

§

impl<R> Unpin for NTriplesParser<R>
where R: Unpin,

§

impl<R> UnwindSafe for NTriplesParser<R>
where R: UnwindSafe,

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> 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, 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.