pub struct TriGParser { /* private fields */ }
Expand description
A TriG streaming parser.
Support for TriG-star is available behind the rdf-star
feature and the TriGParser::with_quoted_triples
option.
Count the number of people:
use oxrdf::vocab::rdf;
use oxrdf::NamedNodeRef;
use oxttl::TriGParser;
let file = br#"@base <http://example.com/> .
@prefix schema: <http://schema.org/> .
<foo> a schema:Person ;
schema:name "Foo" .
<bar> a schema:Person ;
schema:name "Bar" ."#;
let schema_person = NamedNodeRef::new("http://schema.org/Person")?;
let mut count = 0;
for quad in TriGParser::new().for_reader(file.as_ref()) {
let quad = quad?;
if quad.predicate == rdf::TYPE && quad.object == schema_person.into() {
count += 1;
}
}
assert_eq!(2, count);
Implementations§
Source§impl TriGParser
impl TriGParser
Sourcepub fn new() -> Self
pub fn new() -> Self
Builds a new TriGParser
.
Sourcepub fn unchecked(self) -> Self
pub fn unchecked(self) -> Self
Assumes the file is valid to make parsing faster.
It will skip some validations.
Note that if the file is actually not valid, broken RDF might be emitted by the parser.
pub fn with_base_iri( self, base_iri: impl Into<String>, ) -> Result<Self, IriParseError>
pub fn with_prefix( self, prefix_name: impl Into<String>, prefix_iri: impl Into<String>, ) -> Result<Self, IriParseError>
Sourcepub fn with_quoted_triples(self) -> Self
pub fn with_quoted_triples(self) -> Self
Enables TriG-star.
Sourcepub fn for_reader<R: Read>(self, reader: R) -> ReaderTriGParser<R> ⓘ
pub fn for_reader<R: Read>(self, reader: R) -> ReaderTriGParser<R> ⓘ
Parses a TriG file from a Read
implementation.
Count the number of people:
use oxrdf::vocab::rdf;
use oxrdf::NamedNodeRef;
use oxttl::TriGParser;
let file = br#"@base <http://example.com/> .
@prefix schema: <http://schema.org/> .
<foo> a schema:Person ;
schema:name "Foo" .
<bar> a schema:Person ;
schema:name "Bar" ."#;
let schema_person = NamedNodeRef::new("http://schema.org/Person")?;
let mut count = 0;
for quad in TriGParser::new().for_reader(file.as_ref()) {
let quad = quad?;
if quad.predicate == rdf::TYPE && quad.object == schema_person.into() {
count += 1;
}
}
assert_eq!(2, count);
Sourcepub fn for_slice(self, slice: &[u8]) -> SliceTriGParser<'_> ⓘ
pub fn for_slice(self, slice: &[u8]) -> SliceTriGParser<'_> ⓘ
Parses a TriG file from a byte slice.
Count the number of people:
use oxrdf::vocab::rdf;
use oxrdf::NamedNodeRef;
use oxttl::TriGParser;
let file = br#"@base <http://example.com/> .
@prefix schema: <http://schema.org/> .
<foo> a schema:Person ;
schema:name "Foo" .
<bar> a schema:Person ;
schema:name "Bar" ."#;
let schema_person = NamedNodeRef::new("http://schema.org/Person")?;
let mut count = 0;
for quad in TriGParser::new().for_slice(file) {
let quad = quad?;
if quad.predicate == rdf::TYPE && quad.object == schema_person.into() {
count += 1;
}
}
assert_eq!(2, count);
Sourcepub fn low_level(self) -> LowLevelTriGParser
pub fn low_level(self) -> LowLevelTriGParser
Allows to parse a TriG file by using a low-level API.
Count the number of people:
use oxrdf::vocab::rdf;
use oxrdf::NamedNodeRef;
use oxttl::TriGParser;
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 = TriGParser::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);
Trait Implementations§
Source§impl Clone for TriGParser
impl Clone for TriGParser
Source§fn clone(&self) -> TriGParser
fn clone(&self) -> TriGParser
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Default for TriGParser
impl Default for TriGParser
Source§fn default() -> TriGParser
fn default() -> TriGParser
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for TriGParser
impl RefUnwindSafe for TriGParser
impl Send for TriGParser
impl Sync for TriGParser
impl Unpin for TriGParser
impl UnwindSafe for TriGParser
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