Crates.io | iter-tree |
lib.rs | iter-tree |
version | 0.1.10 |
source | src |
created_at | 2023-09-26 01:05:11.349703 |
updated_at | 2024-06-27 23:53:33.519512 |
description | Convert between iterators and tree structures in both directions |
homepage | |
repository | https://github.com/ThibSrb/iter-tree |
max_upload_size | |
id | 983192 |
size | 25,406 |
This crate provides an easy way to convert between iterators and tree structures. This can be useful when building parsers to convert a stream of token into a tree of token.
It extends iterators with two functions :
into_tree
that transforms an iterator into a Tree
.
into_tree_deque
that transforms an iterator into a TreeDeque
.
To get this one, you have to activate the deque
feature flag.
Both type of trees implement the IntoIterator
trait.
The creation of a tree is controlled with the Nesting
enum.
This enum has three variants :
Nesting::Increase
Nesting::Maintain
Nesting::Decrease
If you want to check for these kind of situations, you can use a trick such as the depth counter showed in the below example.
use iter_tree::*;
let mut depth = 0;
let before = String::from("a+(b+c)+d");
let tree: Tree<char> = before.chars().into_tree(|&item: &char| match item {
'(' => {
depth += 1;
Nesting::Increase
}
')' => {
depth -= 1;
Nesting::Decrease
}
_ => Nesting::Maintain,
});
assert!(depth == 0);
println!("{tree:#?}");
let after: String = tree.into_iter().collect();
assert_eq!(before, after);
Node(
[
Leaf(
'a',
),
Leaf(
'+',
),
Node(
[
Leaf(
'(',
),
Leaf(
'b',
),
Leaf(
'+',
),
Leaf(
'c',
),
Leaf(
')',
),
],
),
Leaf(
'+',
),
Leaf(
'd',
),
],
)
NestingFunction
sAdditionally you can create a struct that implements the NestingFunction
trait to replace the closure from the previous example.
Here is an example of how this can be applied :
use iter_tree::*;
#[derive(Debug, Default)]
struct StackController<T> {
stack: Vec<T>,
}
impl<T> StackController<T> {
pub fn is_empty(&self) -> bool {
self.stack.is_empty()
}
}
impl NestingFunction<char> for &mut StackController<char> {
fn direction(&mut self, item: &char) -> Nesting {
let &c = item;
match c {
'<' | '(' => {
self.stack.push(c);
Nesting::Increase
}
'>' => {
if !self.stack.is_empty() && self.stack.last().unwrap() == &'<' {
self.stack.pop();
Nesting::Decrease
} else {
Nesting::Maintain
}
}
')' => {
if !self.stack.is_empty() && self.stack.last().unwrap() == &'(' {
self.stack.pop();
Nesting::Decrease
} else {
Nesting::Maintain
}
}
_ => Nesting::Maintain,
}
}
}
let mut parser = StackController::default();
let td = "< ( < > ) >"
.chars()
.filter(|c| !c.is_whitespace())
.into_tree(&mut parser);
assert!(parser.is_empty());
println!("{td:#?}");
let mut parser = StackController::default();
let td = "<(>)".chars().into_tree(&mut parser);
assert!(!parser.is_empty());
println!("{td:#?}");
The goals for the future of this crate includes but are not limited to :
Adding more methods to build Trees such as for example a tree_map
and tree_deque_map
method that would map the item before including it in the Tree.
Providing other types of Trees, notably some that separate the item that inited and terminated a branch.