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
impl LowLevelNQuadsParser
Sourcepub fn extend_from_slice(&mut self, other: &[u8])
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.
Sourcepub fn end(&mut self)
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.
Sourcepub fn is_end(&self) -> bool
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
.
Sourcepub fn parse_next(&mut self) -> Option<Result<Quad, TurtleSyntaxError>>
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§
impl Freeze for LowLevelNQuadsParser
impl RefUnwindSafe for LowLevelNQuadsParser
impl Send for LowLevelNQuadsParser
impl Sync for LowLevelNQuadsParser
impl Unpin for LowLevelNQuadsParser
impl UnwindSafe for LowLevelNQuadsParser
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more