read_tree/lib.rs
1//! This crate provides a library for creating and then querying trees. The
2//! trees are not intended to be modified after their initial creation.
3//!
4//! Internally trees are represented by a [`Vec`]`<`[`Vertex`]`<T>>` where each
5//! vertex carries the payload of that node in the tree and the number of its
6//! descendants. In addition the vertices are sorted depth first; meaning every
7//! vertex is followed by the vertex of its first child. Using the length of a
8//! vertex we can easily skip a nodes entire subtree and can instantly access
9//! its sibling.
10//!
11//! Slicing a tree into a node is as simple as slicing the trees vertices buffer
12//! into a `&[`[`Vertex`]`<T>]`. We wrap this slice in a [`Node`]`<T>`.
13//!
14//! # Examples
15//!
16//! Trees are created using [`Sapling`]s. Nodes can be attached to a sapling by
17//! using [`push`]. When a node is added to a sapling it is also selected as the
18//! parent for nodes that are added later. To finish a node and reselect its
19//! parent call [`pop`]. When adding a node with no children use [`push_leaf`].
20//! There are more methods to push other saplings, trees or even nodes. See
21//! [`Sapling`] for more information.
22//!
23//! When the sapling is complete, you can [`build`] it into a [`Tree`]`<T>`. The
24//! resulting tree can no longer be modified. Navigating trees is done by using
25//! slices of trees called [`Node`]`<T>`. To get started use [`as_node`] on a
26//! tree to get its root node which represents the entire tree.
27//!
28//! Nodes support various iterators to navigate their contents.
29//!
30//! ```rust
31//! fn main() -> Result<(), Box<dyn std::error::Error>> {
32//! use read_tree::Sapling;
33//!
34//! let mut sap = Sapling::new();
35//! sap.push(1);
36//! sap.pop();
37//!
38//! let tree = sap.build()?;
39//! let root = tree.as_node();
40//!
41//! assert_eq!(root.data(), &1);
42//! Ok(())
43//! }
44//! ```
45//!
46//! [`as_node`]: Tree::as_node
47//! [`build`]: Sapling::build
48//! [`pop`]: Sapling::pop
49//! [`push_leaf`]: Sapling::push_leaf
50//! [`push`]: Sapling::push
51
52mod tree;
53
54#[cfg(test)]
55mod test;
56
57pub use tree::{
58 Ancestors, BuildError, Children, Descendants, Node, PolyTree, Sapling, Tree, ValidationError,
59 Vertex,
60};