-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.rs
More file actions
148 lines (125 loc) · 4.66 KB
/
Copy pathcontext.rs
File metadata and controls
148 lines (125 loc) · 4.66 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use std::collections::HashMap;
use tucana::shared::{InputType, ReferenceValue, Value, value::Kind};
use crate::runtime::error::RuntimeError;
#[derive(Clone)]
pub enum ContextResult {
Error(RuntimeError),
Success(Value),
NotFound,
}
#[derive(Default)]
pub struct Context {
results: HashMap<i64, ContextResult>,
input_types: HashMap<InputType, Value>,
flow_input: Value,
current_node_id: i64,
runtime_trace_labels: Vec<String>,
}
impl Context {
pub fn new(flow_input: Value) -> Self {
Context {
results: HashMap::new(),
input_types: HashMap::new(),
flow_input,
current_node_id: 0,
runtime_trace_labels: Vec::new(),
}
}
pub fn get_current_node_id(&mut self) -> i64 {
self.current_node_id
}
pub fn set_current_node_id(&mut self, node_id: i64) {
self.current_node_id = node_id;
}
pub fn get(&mut self, reference: ReferenceValue) -> ContextResult {
let target = match reference.target {
Some(t) => t,
None => return ContextResult::NotFound,
};
let res = match target {
tucana::shared::reference_value::Target::FlowInput(_) => self.get_flow_input(),
tucana::shared::reference_value::Target::NodeId(i) => self.get_result(i),
tucana::shared::reference_value::Target::InputType(input_type) => {
self.get_input_type(input_type)
}
};
if reference.paths.is_empty() {
return res;
}
if let ContextResult::Success(value) = res {
let mut curr = value;
log::debug!("Tracing down value: {:?}", curr);
for path in reference.paths {
if let Some(index) = path.array_index {
match curr.kind {
Some(ref kind) => match kind {
Kind::ListValue(list) => match list.values.get(index as usize) {
Some(x) => {
curr = x.clone();
}
None => return ContextResult::NotFound,
},
_ => return ContextResult::NotFound,
},
None => return ContextResult::NotFound,
}
}
if let Some(field_name) = path.path {
match curr.kind {
Some(ref kind) => {
if let Kind::StructValue(struct_value) = &kind {
match struct_value.fields.get(&field_name) {
Some(x) => {
log::debug!("Updating trace value to: {:?}", x);
curr = x.clone();
}
None => return ContextResult::NotFound,
}
}
}
None => return ContextResult::NotFound,
}
}
}
ContextResult::Success(curr)
} else {
res
}
}
fn get_result(&mut self, id: i64) -> ContextResult {
match self.results.get(&id) {
None => ContextResult::NotFound,
Some(result) => result.clone(),
}
}
fn get_flow_input(&mut self) -> ContextResult {
ContextResult::Success(self.flow_input.clone())
}
fn get_input_type(&mut self, input_type: InputType) -> ContextResult {
match self.input_types.get(&input_type) {
Some(v) => ContextResult::Success(v.clone()),
None => ContextResult::NotFound,
}
}
pub fn clear_input_type(&mut self, input_type: InputType) {
self.input_types.remove(&input_type);
}
pub fn insert_input_type(&mut self, input_type: InputType, value: Value) {
self.input_types.insert(input_type, value);
}
pub fn insert_flow_input(&mut self, value: Value) {
self.flow_input = value;
}
pub fn insert_success(&mut self, id: i64, value: Value) {
self.results.insert(id, ContextResult::Success(value));
}
pub fn insert_error(&mut self, id: i64, runtime_error: RuntimeError) {
self.results.insert(id, ContextResult::Error(runtime_error));
}
pub fn push_runtime_trace_label(&mut self, label: String) {
self.runtime_trace_labels.push(label);
}
pub fn pop_runtime_trace_label(&mut self) -> Option<String> {
self.runtime_trace_labels.pop()
}
}