diff --git a/gemla/src/bracket/mod.rs b/gemla/src/bracket/mod.rs index a3f1ca9..275e835 100644 --- a/gemla/src/bracket/mod.rs +++ b/gemla/src/bracket/mod.rs @@ -1,12 +1,15 @@ mod tree; - -use uuid::Uuid; +mod state; fn build_tree(h: u32) -> Option> { let mut result: Option> = None; if h != 0 { - result = Some(tree::concat_trees(Uuid::new_v4(), build_tree(h - 1), build_tree(h - 1))); + result = Some(tree::concat_trees(state::create(), build_tree(h - 1), build_tree(h - 1))); + match &result { + Some(r) => (*r).run_simulation(), + _ => () + } } result @@ -19,8 +22,8 @@ pub fn run_bracket() { loop { println!("========================================="); println!("Running bracket..."); - println!("{}", tree); height += 1; - tree = *tree::concat_trees(Uuid::new_v4(), Some(Box::new(tree)), build_tree(height)); + tree = *tree::concat_trees(state::create(), Some(Box::new(tree)), build_tree(height)); + tree.run_simulation(); } } \ No newline at end of file diff --git a/gemla/src/bracket/state.rs b/gemla/src/bracket/state.rs new file mode 100644 index 0000000..6ccf5e9 --- /dev/null +++ b/gemla/src/bracket/state.rs @@ -0,0 +1,18 @@ +use uuid::Uuid; +use std::fmt; + +pub struct State { + id: Uuid +} + +impl fmt::Display for State { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.id) + } +} + +pub fn create() -> State { + State { + id: Uuid::new_v4() + } +} \ No newline at end of file diff --git a/gemla/src/bracket/tree.rs b/gemla/src/bracket/tree.rs index fb35e80..a4bfe58 100644 --- a/gemla/src/bracket/tree.rs +++ b/gemla/src/bracket/tree.rs @@ -1,15 +1,15 @@ use std::fmt; -use uuid::Uuid; +use super::state; pub struct Tree { - id: Uuid, + state: state::State, left: Option>, right: Option> } -pub fn concat_trees(v: Uuid, l: Option>, r: Option>) -> Box { +pub fn concat_trees(s: state::State, l: Option>, r: Option>) -> Box { Box::new(Tree { - id: v, + state: s, left: l, right: r }) @@ -24,6 +24,21 @@ impl fmt::Display for Tree { } }; - write!(f, "({} :{}|{})", self.id, node_str(&self.left), node_str(&self.right)) + write!(f, "({} :{}|{})", self.state, node_str(&self.left), node_str(&self.right)) + } +} + +fn fmt_node(t: &Option>) -> String { + match t { + Some(n) => format!("{}", (*n).state), + _ => String::from("_") + } +} + +impl Tree { + pub fn run_simulation(&self) { + println!("================================"); + println!("Running simulation for node: {}", self.state); + println!("With children {} and {}", fmt_node(&self.left), fmt_node(&self.right)); } } \ No newline at end of file