phlow_sdk/
context.rs

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 from_input(input: Value) -> Self {
43        Self {
44            main: None,
45            steps: HashMap::new(),
46            payload: None,
47            input: Some(input),
48        }
49    }
50
51    pub fn add_module_input(&self, output: Value) -> Self {
52        Self {
53            main: self.main.clone(),
54            steps: self.steps.clone(),
55            payload: self.payload.clone(),
56            input: Some(output),
57        }
58    }
59
60    pub fn add_module_output(&self, output: Value) -> Self {
61        Self {
62            main: self.main.clone(),
63            steps: self.steps.clone(),
64            payload: Some(output),
65            input: self.input.clone(),
66        }
67    }
68
69    pub fn add_step_payload(&mut self, output: Option<Value>) {
70        self.payload = output;
71    }
72
73    pub fn add_step_id_output(&mut self, id: ID, output: Value) {
74        self.steps.insert(id, output.clone());
75    }
76
77    pub fn get_step_output(&self, id: &ID) -> Option<&Value> {
78        self.steps.get(&id)
79    }
80}