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
use super::condition_matcher::ConditionMatcher;
use crate::{
  shared_state::AstroRunSharedState, Error, ExecutionContext, GithubAuthorization, Runner,
  WorkflowEvent,
};
use std::sync::Arc;

pub struct ExecutionContextBuilder {
  runner: Option<Arc<Box<dyn Runner>>>,
  shared_state: Option<AstroRunSharedState>,
  event: Option<WorkflowEvent>,
  github_auth: Option<GithubAuthorization>,
}

impl ExecutionContextBuilder {
  pub fn new() -> Self {
    ExecutionContextBuilder {
      runner: None,
      shared_state: None,
      event: None,
      github_auth: None,
    }
  }

  pub fn runner(mut self, runner: Arc<Box<dyn Runner>>) -> Self {
    self.runner = Some(runner);
    self
  }

  pub fn shared_state(mut self, shared_state: AstroRunSharedState) -> Self {
    self.shared_state = Some(shared_state);
    self
  }

  pub fn event(mut self, event: WorkflowEvent) -> Self {
    self.event = Some(event);
    self
  }

  pub fn github_auth(mut self, github_auth: GithubAuthorization) -> Self {
    self.github_auth = Some(github_auth);
    self
  }

  pub fn build(self) -> ExecutionContext {
    let runner = self
      .runner
      .ok_or(Error::init_error(
        "Runner is not set in execution context builder",
      ))
      .unwrap();

    let shared_state = self
      .shared_state
      .unwrap_or_else(|| AstroRunSharedState::new());

    let ctx = ExecutionContext {
      runner,
      shared_state,
      condition_matcher: ConditionMatcher::new(self.event, self.github_auth),
    };

    ctx
  }
}