pub struct LowLevelNTriplesParser { /* private fields */ }
Expand description
Parses a N-Triples file by using a low-level API.
Can be built using NTriplesParser::low_level
.
Count the number of people:
use oxrdf::{NamedNodeRef, vocab::rdf};
use oxttl::NTriplesParser;
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 = NTriplesParser::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 LowLevelNTriplesParser
impl LowLevelNTriplesParser
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<Triple, TurtleSyntaxError>>
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
.
Auto Trait Implementations§
impl Freeze for LowLevelNTriplesParser
impl RefUnwindSafe for LowLevelNTriplesParser
impl Send for LowLevelNTriplesParser
impl Sync for LowLevelNTriplesParser
impl Unpin for LowLevelNTriplesParser
impl UnwindSafe for LowLevelNTriplesParser
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