1use crate::{Arg, Target, Node, NodeResult, Path};
2
3pub struct Runtime {
4 nodes: Vec<Box<dyn Node>>,
5 path: Path,
6}
7
8impl Runtime {
9 pub fn new() -> Self {
10 Self {
11 nodes: Vec::new(),
12 path: Path::new(),
13 }
14 }
15
16 pub fn add_node(&mut self, node: Box<dyn Node>) {
17 self.nodes.push(node);
18 }
19
20 pub fn get(&self, path: &Path) -> Option<&dyn Node> {
21 let key = path.to_string();
22 self.nodes.iter().find(|n| n.path().to_string() == key).map(|n| &**n)
23 }
24
25 pub fn run(&mut self, initial_arg: Box<dyn Arg>, start_path: Path) -> Option<Box<dyn Target>> {
26 let mut current_arg = initial_arg;
27 let mut current_path = start_path;
28
29 loop {
30 self.path.push(¤t_path.to_string());
31
32 let node = match self.get(¤t_path) {
33 Some(n) => n,
34 None => return None,
35 };
36
37 match node.process(&*current_arg) {
38 NodeResult::Done(target) => return Some(target),
39 NodeResult::Next(next_arg, next_path) => {
40 current_arg = next_arg;
41 current_path = next_path;
42 }
43 }
44 }
45 }
46
47 pub fn path(&self) -> &Path {
48 &self.path
49 }
50
51 pub fn reset(&mut self) {
52 self.path = Path::new();
53 }
54
55 pub fn iter_nodes(&self) -> impl Iterator<Item = &dyn Node> {
56 self.nodes.iter().map(|n| &**n)
57 }
58}
59
60impl Default for Runtime {
61 fn default() -> Self {
62 Self::new()
63 }
64}