oxttl::nquads

Struct LowLevelNQuadsParser

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

Parses a N-Quads file by using a low-level API.

Can be built using NQuadsParser::low_level.

Count the number of people:

use oxrdf::{NamedNodeRef, vocab::rdf};
use oxttl::NQuadsParser;

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

let schema_person = NamedNodeRef::new("http://schema.org/Person")?;
let mut count = 0;
let mut parser = NQuadsParser::new().low_level();
let mut file_chunks = file.iter();
while !parser.is_end() {
    // We feed more data to the parser
    if let Some(chunk) = file_chunks.next() {
        parser.extend_from_slice(chunk);    
    } else {
        parser.end(); // It's finished
    }
    // We read as many quads from the parser as possible
    while let Some(quad) = parser.parse_next() {
        let quad = quad?;
        if quad.predicate == rdf::TYPE && quad.object == schema_person.into() {
            count += 1;
        }
    }
}
assert_eq!(2, count);

Implementations§

Source§

impl LowLevelNQuadsParser

Source

pub fn extend_from_slice(&mut self, other: &[u8])

Adds some extra bytes to the parser. Should be called when parse_next returns None and there is still unread data.

Source

pub fn end(&mut self)

Tell the parser that the file is finished.

This triggers the parsing of the final bytes and might lead parse_next to return some extra values.

Source

pub fn is_end(&self) -> bool

Returns if the parsing is finished i.e. end has been called and parse_next is always going to return None.

Source

pub fn parse_next(&mut self) -> Option<Result<Quad, TurtleSyntaxError>>

Attempt to parse a new quad from the already provided data.

Returns None if the parsing is finished or more data is required. If it is the case more data should be fed using extend_from_slice.

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> 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.
Source§

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

Source§

fn vzip(self) -> V