summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-01-06 14:10:10 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-01-06 18:26:28 +0100
commitbd8efaac79d6dbed185dfbf3369a615bac5edc91 (patch)
treedcc543846c459933f644bb4f3d9023938e450176
parentd1a5a7f8a199dd84756f4060f7f9a6d75bd58dce (diff)
Rewrite iterator to return current-caluclate next instead of calculate-next-return
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--lib/domain/libimagmail/src/mailtree.rs111
1 files changed, 56 insertions, 55 deletions
diff --git a/lib/domain/libimagmail/src/mailtree.rs b/lib/domain/libimagmail/src/mailtree.rs
index af58de0f..e2f9c288 100644
--- a/lib/domain/libimagmail/src/mailtree.rs
+++ b/lib/domain/libimagmail/src/mailtree.rs
@@ -81,30 +81,26 @@ fn fill_arena_with<'a>(arena: &mut Arena<String>, store: &'a MailStoreWithConnec
#[derive(Debug)]
pub struct Traverse<'a> {
tree: &'a Mailtree,
- current_node: NodeId,
- current_indent: usize,
+ next_node: Option<NodeId>,
+ next_indent: usize,
}
impl<'a> Traverse<'a> {
fn with(tree: &'a Mailtree) -> Self {
Traverse {
tree,
- current_node: tree.root,
- current_indent: 0,
+ next_node: Some(tree.root),
+ next_indent: 0,
}
}
- fn current(&self) -> Option<(usize, String)> {
- return self
- .tree
- .arena
- .get(self.current_node)
- .map(|node| node.get())
- .map(|mid| (self.current_indent, mid.clone()))
+ fn get_node(&self, nodeid: NodeId) -> Option<String> {
+ self.tree.arena.get(nodeid).map(|n| n.get()).map(ToOwned::to_owned)
}
fn get_current_node(&self) -> Option<&Node<String>> {
- self.tree.arena.get(self.current_node)
+ self.next_node
+ .and_then(|nodeid| self.tree.arena.get(nodeid))
}
fn current_node_next_sibling(&self) -> Option<NodeId> {
@@ -127,54 +123,59 @@ impl<'a> Iterator for Traverse<'a> {
type Item = (usize, String); // (Indention, Message Id)
fn next(&mut self) -> Option<Self::Item> {
- trace!("Iterating: {:?}", self);
-
- if let Some(sibling) = self.current_node_next_sibling() {
- trace!("Sibling found: {:?}", sibling);
- // do not alter indent
- self.current_node = sibling;
- return self.current();
- } else {
- trace!("No sibling found");
- if let Some(child) = self.current_node_first_child() {
- trace!("Child found: {:?}", child);
-
- // We have a child, make that the current node and return it
- self.current_indent += 1;
- self.current_node = child;
- return self.current();
+ if let Some(current_node) = self.next_node.and_then(|n| self.get_node(n)) {
+ let current_indent = self.next_indent;
+ trace!("Iterating: {:?}", self);
+
+ if let Some(sibling) = self.current_node_next_sibling() {
+ trace!("Sibling found: {:?}", sibling);
+ // do not alter indent
+ self.next_node = Some(sibling);
+ return Some((current_indent, current_node));
} else {
- trace!("No child found");
- // We have no siblings and
- // We have no children
- // We might have to go back up one level
-
- if let Some(parent) = self.current_node_parent() {
- trace!("Parent found: {:?}", parent);
- // We have a parent
- if let Some(parents_sibling) = self.tree.arena.get(parent).and_then(|n| n.next_sibling()) {
- trace!("Parents sibling found: {:?}", parents_sibling);
- // And the parent has a sibling
- //
- // Make that the next current node and go from there
- self.current_indent -= 1;
- self.current_node = parents_sibling;
- return self.current();
+ trace!("No sibling found");
+ if let Some(child) = self.current_node_first_child() {
+ trace!("Child found: {:?}", child);
+
+ // We have a child, make that the current node and return it
+ self.next_indent += 1;
+ self.next_node = Some(child);
+ return Some((current_indent, current_node));
+ } else {
+ trace!("No child found");
+ // We have no siblings and
+ // We have no children
+ // We might have to go back up one level
+
+ if let Some(parent) = self.current_node_parent() {
+ trace!("Parent found: {:?}", parent);
+ // We have a parent
+ if let Some(parents_sibling) = self.tree.arena.get(parent).and_then(|n| n.next_sibling()) {
+ trace!("Parents sibling found: {:?}", parents_sibling);
+ // And the parent has a sibling
+ //
+ // Make that the next current node and go from there
+ self.next_indent -= 1;
+ self.next_node = Some(parents_sibling);
+ return Some((current_indent, current_node));
+ } else {
+ trace!("No parents sibling found");
+ // We are the last node:
+ // - We have no sibling
+ // - We have no child
+ // - Our parent has no sibling
+ self.next_node = None;
+ return Some((current_indent, current_node))
+ }
} else {
- trace!("No parents sibling found");
- // We are the last node:
- // - We have no sibling
- // - We have no child
- // - Our parent has no sibling
- return None
+ trace!("No parent found");
+ self.next_node = None;
+ return Some((current_indent, current_node))
}
- } else {
- trace!("No parent found");
- // We have no parent. Huh?
- // TODO: panic?
- return None
}
}
+ } else {
+ return None;
}
}
}