1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use crate::{shared_state::AstroRunSharedState, AstroRunPlugin, ExecutionContext, Runner};
use std::sync::Arc;

pub struct AstroRun {
  runner: Arc<Box<dyn Runner>>,
  shared_state: AstroRunSharedState,
}

impl AstroRun {
  pub fn builder() -> AstroRunBuilder {
    AstroRunBuilder::new()
  }

  pub fn register_plugin(&self, plugin: AstroRunPlugin) -> &Self {
    self.shared_state.register_plugin(plugin);

    self
  }

  pub fn unregister_plugin(&self, plugin_name: &'static str) -> &Self {
    self.shared_state.unregister_plugin(plugin_name);

    self
  }

  pub fn execution_context(&self) -> ExecutionContext {
    let shared_state = self.shared_state.clone();
    ExecutionContext::builder()
      .runner(self.runner.clone())
      .shared_state(shared_state)
      .build()
      .unwrap()
  }
}

pub struct AstroRunBuilder {
  runner: Option<Box<dyn Runner>>,
  shared_state: AstroRunSharedState,
}

impl AstroRunBuilder {
  pub fn new() -> Self {
    AstroRunBuilder {
      runner: None,
      shared_state: AstroRunSharedState::new(),
    }
  }

  pub fn runner<T>(mut self, runner: T) -> Self
  where
    T: Runner + 'static,
  {
    self.runner = Some(Box::new(runner));
    self
  }

  pub fn plugin(self, plugin: AstroRunPlugin) -> Self {
    self.shared_state.register_plugin(plugin);
    self
  }

  pub fn build(self) -> AstroRun {
    let runner = self.runner.unwrap();

    AstroRun {
      runner: Arc::new(runner),
      shared_state: self.shared_state,
    }
  }
}