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    steps: HashMap<ID, Value>,
9    main: Option<Value>,
10    payload: Option<Value>,
11    input: Option<Value>,
12    setup: Option<Value>,
13}
14
15impl Context {
16    pub fn new() -> Self {
17        Self {
18            ..Default::default()
19        }
20    }
21
22    pub fn from_main(main: Value) -> Self {
23        Self {
24            main: Some(main),
25            ..Default::default()
26        }
27    }
28
29    pub fn from_setup(setup: Value) -> Self {
30        Self {
31            setup: Some(setup),
32            ..Default::default()
33        }
34    }
35
36    pub fn add_module_input(&self, output: Value) -> Self {
37        Self {
38            main: self.main.clone(),
39            steps: self.steps.clone(),
40            payload: self.payload.clone(),
41            input: Some(output),
42            setup: self.setup.clone(),
43        }
44    }
45
46    pub fn add_module_output(&self, output: Value) -> Self {
47        Self {
48            main: self.main.clone(),
49            steps: self.steps.clone(),
50            payload: Some(output),
51            input: self.input.clone(),
52            setup: self.setup.clone(),
53        }
54    }
55
56    pub fn add_step_payload(&mut self, output: Option<Value>) {
57        if let Some(output) = output {
58            self.payload = Some(output);
59        }
60    }
61
62    pub fn get_payload(&self) -> Option<Value> {
63        self.payload.clone()
64    }
65
66    pub fn get_main(&self) -> Option<Value> {
67        self.main.clone()
68    }
69
70    pub fn get_setup(&self) -> Option<Value> {
71        self.setup.clone()
72    }
73
74    pub fn get_input(&self) -> Option<Value> {
75        self.input.clone()
76    }
77
78    pub fn get_steps(&self) -> &HashMap<ID, Value> {
79        &self.steps
80    }
81
82    pub fn set_main(&mut self, main: Value) {
83        self.main = Some(main);
84    }
85
86    pub fn add_step_id_output(&mut self, id: ID, output: Value) {
87        self.steps.insert(id, output.clone());
88    }
89
90    pub fn get_step_output(&self, id: &ID) -> Option<&Value> {
91        self.steps.get(&id)
92    }
93}