1use crate::id::ID;
2use serde::Serialize;
3use std::collections::HashMap;
4use valu3::prelude::*;
5
6#[derive(Debug, Clone, PartialEq, Serialize, Default)]
7pub struct Context {
8    pub steps: HashMap<ID, Value>,
9    pub main: Option<Value>,
10    pub payload: Option<Value>,
11    pub input: Option<Value>,
12}
13
14impl Context {
15    pub fn new() -> Self {
16        Self {
17            main: None,
18            steps: HashMap::new(),
19            payload: None,
20            input: None,
21        }
22    }
23
24    pub fn from_payload(payload: Value) -> Self {
25        Self {
26            main: None,
27            steps: HashMap::new(),
28            payload: Some(payload),
29            input: None,
30        }
31    }
32
33    pub fn from_main(main: Value) -> Self {
34        Self {
35            main: Some(main),
36            steps: HashMap::new(),
37            payload: None,
38            input: None,
39        }
40    }
41
42    pub fn add_module_input(&self, output: Value) -> Self {
43        Self {
44            main: self.main.clone(),
45            steps: self.steps.clone(),
46            payload: self.payload.clone(),
47            input: Some(output),
48        }
49    }
50
51    pub fn add_module_output(&self, output: Value) -> Self {
52        Self {
53            main: self.main.clone(),
54            steps: self.steps.clone(),
55            payload: Some(output),
56            input: self.input.clone(),
57        }
58    }
59
60    pub fn add_step_payload(&mut self, output: Option<Value>) {
61        self.payload = output;
62    }
63
64    pub fn add_step_id_output(&mut self, id: ID, output: Value) {
65        self.steps.insert(id, output.clone());
66    }
67
68    pub fn get_step_output(&self, id: &ID) -> Option<&Value> {
69        self.steps.get(&id)
70    }
71}