Struct TriGParser

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

A TriG and TriG-star streaming parser.

It implements the QuadsParser trait.

Count the number of people using the QuadsParser API:

use rio_turtle::{TriGParser, TurtleError};
use rio_api::parser::QuadsParser;
use rio_api::model::NamedNode;

let file = b"@prefix schema: <http://schema.org/> .
<http://example/> {
    <http://example.com/foo> a schema:Person ;
        schema:name  \"Foo\" .
    <http://example.com/bar> a schema:Person ;
        schema: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;
TriGParser::new(file.as_ref(), None).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> TriGParser<R>

Source

pub fn new(reader: R, base_iri: Option<Iri<String>>) -> Self

Builds the parser from a BufRead implementation, and a base IRI for relative IRI resolution.

Source

pub fn prefixes(&self) -> &HashMap<String, String>

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

This method returns the mapping from prefix name to prefix value. 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 std::collections::HashMap;
use rio_api::model::NamedNode;
use rio_api::parser::QuadsParser;
use rio_turtle::{TurtleError, TriGParser};

let file = b"@prefix schema: <http://schema.org/> .
@prefix ex: <http://example.com/> .
ex: a schema:WebSite .
@prefix ex: <http://example.org/> .
ex: a schema:WebSite .";

let mut parser = TriGParser::new(file.as_ref(), None);
assert_eq!(parser.prefixes(), &HashMap::new()); // No prefix at the beginning

let _: Result<_, TurtleError> = parser.parse_step(&mut |_| panic!("We read the first prefix"));
assert_eq!(parser.prefixes().len(), 1);
assert_eq!(parser.prefixes()["schema"], "http://schema.org/");

let _: Result<_, TurtleError> = parser.parse_step(&mut |_| panic!("We read the second prefix"));
assert_eq!(parser.prefixes().len(), 2);
assert_eq!(parser.prefixes()["schema"], "http://schema.org/");
assert_eq!(parser.prefixes()["ex"], "http://example.com/");

let _: Result<_, TurtleError> = parser.parse_step(&mut |t| {
    assert_eq!(t.subject, NamedNode { iri: "http://example.com/" }.into()); // We read the first triple
    Ok(())
});

let _: Result<_, TurtleError> = parser.parse_step(&mut |_| panic!("We read the new version of the ex: prefix"));
assert_eq!(parser.prefixes().len(), 2);
assert_eq!(parser.prefixes()["schema"], "http://schema.org/");
assert_eq!(parser.prefixes()["ex"], "http://example.org/");  

let _: Result<_, TurtleError> = parser.parse_step(&mut |t| {
    assert_eq!(t.subject, NamedNode { iri: "http://example.org/" }.into()); // We read the second triple
   Ok(())
});

Trait Implementations§

Source§

impl<R: BufRead> QuadsParser for TriGParser<R>

Source§

type Error = TurtleError

Source§

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

Parses a small chunk of the file and calls on_quad each time a new quad is read. (A “small chunk” could be a line for an N-Quads 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_quad: &mut impl FnMut(Quad<'_>) -> Result<(), E>, ) -> Result<(), E>
where E: From<Self::Error>,

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

fn into_iter<T, E, F>( self, convert_quad: F, ) -> QuadsParserIterator<T, E, F, Self>
where E: From<Self::Error>, F: FnMut(Quad<'_>) -> Result<T, E>,

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<R> UnwindSafe for TriGParser<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.