pub struct TriGParser { /* private fields */ }Expand description
A TriG streaming parser.
Count the number of people:
use oxrdf::NamedNodeRef;
use oxrdf::vocab::rdf;
use oxttl::TriGParser;
let file = r#"@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_bytes()) {
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 lenient(self) -> Self
pub fn lenient(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, the parser might emit broken RDF.
pub fn unchecked(self) -> Self
👎Deprecated since 0.2.0:
Use lenient() instead
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 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::NamedNodeRef;
use oxrdf::vocab::rdf;
use oxttl::TriGParser;
let file = r#"@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_bytes()) {
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: &(impl AsRef<[u8]> + ?Sized),
) -> SliceTriGParser<'_> ⓘ
pub fn for_slice( self, slice: &(impl AsRef<[u8]> + ?Sized), ) -> SliceTriGParser<'_> ⓘ
Parses a TriG file from a byte slice.
Count the number of people:
use oxrdf::NamedNodeRef;
use oxrdf::vocab::rdf;
use oxttl::TriGParser;
let file = r#"@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::NamedNodeRef;
use oxrdf::vocab::rdf;
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 duplicate 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 UnsafeUnpin 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