rio_turtle/
lib.rs

1//! Implementation of [N-Triples](https://www.w3.org/TR/n-triples/), [N-Quads](https://www.w3.org/TR/n-quads/), [Turtle](https://www.w3.org/TR/turtle/) and [TriG](https://www.w3.org/TR/trig/) parsers.
2//!
3//! <strong style="font-size: 150%">
4//!
5//! This library is going to be deprecated.
6//! [oxttl](https://crates.io/crates/oxttl) is currently in development to replace it.
7//! </strong>
8//!
9//! [RDF-star](https://w3c.github.io/rdf-star/cg-spec/) syntaxes are also supported, i.e. [Turtle-star](https://w3c.github.io/rdf-star/cg-spec/#turtle-star), [TriG-star](https://w3c.github.io/rdf-star/cg-spec/#trig-star), [N-Triples-star](https://w3c.github.io/rdf-star/cg-spec/#n-triples-star) and [N-Quads-star](https://w3c.github.io/rdf-star/cg-spec/#n-quads-star).
10//!
11//! All the provided parsers work in streaming from a `BufRead` implementation.
12//! They do not rely on any dependencies outside of Rust standard library.
13//! The parsers are not protected against memory overflows.
14//! For example if the parsed content contains a literal string of 16 GB, 16 GB of memory will be allocated.
15//!
16//! How to read a file `foo.ttl` and count the number of `rdf:type` triples:
17//! ```no_run
18//! use rio_turtle::{TurtleParser, TurtleError};
19//! use rio_api::parser::TriplesParser;
20//! use rio_api::model::NamedNode;
21//! use std::io::BufReader;
22//! use std::fs::File;
23//! use oxiri::Iri;
24//!
25//! let rdf_type = NamedNode { iri: "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" };
26//! let mut count = 0;
27//! TurtleParser::new(BufReader::new(File::open("foo.ttl")?), Some(Iri::parse("file:foo.ttl".to_owned()).unwrap())).parse_all(&mut |t| {
28//!     if t.predicate == rdf_type {
29//!         count += 1;
30//!     }
31//!     Ok(()) as Result<(), TurtleError>
32//! })?;
33//! # Result::<_,TurtleError>::Ok(())
34//! ```
35//!
36//! Replace `TurtleParser` by `NTriplesParser`, `NQuadsParser` or `TriGParser` to read an N-Triples, N-Quads or TriG file instead.
37//!
38//! `NTriplesParser` and `NQuadsParser` do not use the second argument of the `new` function that is the IRI of the file.
39#![deny(unsafe_code)]
40#![cfg_attr(docsrs, feature(doc_auto_cfg))]
41#![doc(test(attr(deny(warnings))))]
42
43mod error;
44mod formatters;
45mod ntriples;
46mod shared;
47mod triple_allocator;
48mod turtle;
49mod utils;
50
51#[cfg(feature = "generalized")]
52mod gnquads;
53#[cfg(feature = "generalized")]
54mod gtrig;
55#[cfg(feature = "generalized")]
56mod gtriple_allocator;
57
58pub use error::TurtleError;
59pub use formatters::NQuadsFormatter;
60pub use formatters::NTriplesFormatter;
61pub use formatters::TriGFormatter;
62pub use formatters::TurtleFormatter;
63pub use ntriples::NQuadsParser;
64pub use ntriples::NTriplesParser;
65pub use turtle::TriGParser;
66pub use turtle::TurtleParser;
67
68#[cfg(feature = "generalized")]
69pub use gnquads::GeneralizedNQuadsParser;
70
71#[cfg(feature = "generalized")]
72pub use gtrig::GTriGParser;
73
74/// Maximal number of nested structures (collections, blank node, quoted triples...).
75///
76/// This limit is set in order to avoid stack overflow error when parsing such structures due to too many recursive calls.
77/// The actual limit value is a wet finger compromise between not failing to parse valid files and avoiding to trigger stack overflow errors.
78const MAX_STACK_SIZE: usize = 128;