Struct rio_turtle::NQuadsParser
source · pub struct NQuadsParser<R: BufRead> { /* private fields */ }
Expand description
A N-Quads and N-Quads-star streaming parser.
It implements the QuadsParser
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 QuadsParser
API:
use rio_turtle::{NQuadsParser, TurtleError};
use rio_api::parser::QuadsParser;
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/> .
<http://example.com/foo> <http://schema.org/name> \"Foo\" <http://example.com/> .
<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;
NQuadsParser::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§
Trait Implementations§
source§impl<R: BufRead> QuadsParser for NQuadsParser<R>
impl<R: BufRead> QuadsParser for NQuadsParser<R>
type Error = TurtleError
source§fn parse_step<E: From<TurtleError>>(
&mut self,
on_quad: &mut impl FnMut(Quad<'_>) -> Result<(), E>
) -> Result<(), E>
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