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
impl LowLevelTurtleParser
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
.
Sourcepub fn prefixes(&self) -> TurtlePrefixesIter<'_> ⓘ
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
Sourcepub fn base_iri(&self) -> Option<&str>
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