pub struct TokioAsyncReaderTurtleParser<R: AsyncRead + Unpin> { /* private fields */ }Available on crate feature
async-tokio only.Expand description
Parses a Turtle file from a AsyncRead implementation.
Can be built using TurtleParser::for_tokio_async_reader.
Count the number of people:
use oxrdf::NamedNodeRef;
use oxrdf::vocab::rdf;
use oxttl::TurtleParser;
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;
let mut parser = TurtleParser::new().for_tokio_async_reader(file.as_bytes());
while let Some(triple) = parser.next().await {
let triple = triple?;
if triple.predicate == rdf::TYPE && triple.object == schema_person.into() {
count += 1;
}
}
assert_eq!(2, count);Implementations§
Source§impl<R: AsyncRead + Unpin> TokioAsyncReaderTurtleParser<R>
impl<R: AsyncRead + Unpin> TokioAsyncReaderTurtleParser<R>
Sourcepub async fn next(&mut self) -> Option<Result<Triple, TurtleParseError>>
pub async fn next(&mut self) -> Option<Result<Triple, TurtleParseError>>
Reads the next triple or returns None if the file is finished.
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 = r#"@base <http://example.com/> .
@prefix schema: <http://schema.org/> .
<foo> a schema:Person ;
schema:name "Foo" ."#;
let mut parser = TurtleParser::new().for_tokio_async_reader(file.as_bytes());
assert_eq!(parser.prefixes().collect::<Vec<_>>(), []); // No prefix at the beginning
parser.next().await.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 = r#"@base <http://example.com/> .
@prefix schema: <http://schema.org/> .
<foo> a schema:Person ;
schema:name "Foo" ."#;
let mut parser = TurtleParser::new().for_tokio_async_reader(file.as_bytes());
assert!(parser.base_iri().is_none()); // No base IRI at the beginning
parser.next().await.unwrap()?; // We read the first triple
assert_eq!(parser.base_iri(), Some("http://example.com/")); // There is now a base IRI
//Auto Trait Implementations§
impl<R> Freeze for TokioAsyncReaderTurtleParser<R>where
R: Freeze,
impl<R> RefUnwindSafe for TokioAsyncReaderTurtleParser<R>where
R: RefUnwindSafe,
impl<R> Send for TokioAsyncReaderTurtleParser<R>where
R: Send,
impl<R> Sync for TokioAsyncReaderTurtleParser<R>where
R: Sync,
impl<R> Unpin for TokioAsyncReaderTurtleParser<R>
impl<R> UnsafeUnpin for TokioAsyncReaderTurtleParser<R>where
R: UnsafeUnpin,
impl<R> UnwindSafe for TokioAsyncReaderTurtleParser<R>where
R: UnwindSafe,
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