oxttl::turtle

Struct LowLevelTurtleParser

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

Parses a Turtle file by using a low-level API.

Can be built using TurtleParser::low_level.

Count the number of people:

use oxrdf::vocab::rdf;
use oxrdf::NamedNodeRef;
use oxttl::TurtleParser;

let file: [&[u8]; 5] = [
    b"@base <http://example.com/>",
    b". @prefix schema: <http://schema.org/> .",
    b"<foo> a schema:Person",
    b" ; schema:name \"Foo\" . <bar>",
    b" a schema:Person ; schema:name \"Bar\" .",
];

let schema_person = NamedNodeRef::new("http://schema.org/Person")?;
let mut count = 0;
let mut parser = TurtleParser::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 triples from the parser as possible
    while let Some(triple) = parser.parse_next() {
        let triple = triple?;
        if triple.predicate == rdf::TYPE && triple.object == schema_person.into() {
            count += 1;
        }
    }
}
assert_eq!(2, count);

Implementations§

Source§

impl LowLevelTurtleParser

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<Triple, TurtleSyntaxError>>

Attempt to parse a new triple 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.

Source

pub fn prefixes(&self) -> TurtlePrefixesIter<'_>

The list of IRI prefixes considered at the current step of the parsing.

This method returns (prefix name, prefix value) tuples. It is empty at the beginning of the parsing and gets updated when prefixes are encountered. It should be full at the end of the parsing (but if a prefix is overridden, only the latest version will be returned).

use oxttl::TurtleParser;

let file = br#"@base <http://example.com/> .
@prefix schema: <http://schema.org/> .
<foo> a schema:Person ;
    schema:name "Foo" ."#;

let mut parser = TurtleParser::new().low_level();
parser.extend_from_slice(file);
assert_eq!(parser.prefixes().collect::<Vec<_>>(), []); // No prefix at the beginning

parser.parse_next().unwrap()?; // We read the first triple
assert_eq!(
    parser.prefixes().collect::<Vec<_>>(),
    [("schema", "http://schema.org/")]
); // There are now prefixes
Source

pub fn base_iri(&self) -> Option<&str>

The base IRI considered at the current step of the parsing.

use oxttl::TurtleParser;

let file = br#"@base <http://example.com/> .
@prefix schema: <http://schema.org/> .
<foo> a schema:Person ;
    schema:name "Foo" ."#;

let mut parser = TurtleParser::new().low_level();
parser.extend_from_slice(file);
assert!(parser.base_iri().is_none()); // No base IRI at the beginning

parser.parse_next().unwrap()?; // We read the first triple
assert_eq!(parser.base_iri(), Some("http://example.com/")); // There is now a base IRI

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