Repository navigation
Expand file tree
/
Copy pathnodes.rs
More file actions
186 lines (149 loc) · 4.53 KB
/
Copy pathnodes.rs
File metadata and controls
186 lines (149 loc) · 4.53 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use proxy_wasm::traits::*;
use serde_json::Value;
use std::any::Any;
use std::collections::BTreeMap;
use std::sync::{Mutex, OnceLock};
use crate::data::{Input, State, State::*};
pub mod call;
pub mod exit;
pub mod handlebars;
pub mod jq;
pub mod property;
pub type NodeVec = Vec<Box<dyn Node>>;
#[derive(Clone, Debug)]
pub struct PortConfig {
pub defaults: Option<Vec<String>>,
pub user_defined_ports: bool,
}
impl PortConfig {
pub fn names(list: &[&str]) -> Vec<String> {
list.iter().map(|&s| str::to_owned(s)).collect()
}
/// Combine defaults and user-given ports
/// into the final ordered list of ports.
pub fn into_port_list(self: PortConfig, given: &[String]) -> Vec<String> {
let mut list = self.defaults.unwrap_or_default();
if self.user_defined_ports {
for port in given {
if !list.iter().any(|p| p == port) {
list.push(port.to_string());
}
}
}
list
}
}
pub trait Node {
fn run(&self, _ctx: &dyn HttpContext, _input: &Input) -> State {
Done(vec![None])
}
fn resume(&self, _ctx: &dyn HttpContext, _input: &Input) -> State {
Done(vec![None])
}
}
pub struct NodeDefaultLink {
pub this_port: String,
pub other_node: String,
pub other_port: String,
}
pub trait NodeConfig {
fn as_any(&self) -> &dyn Any;
fn default_inputs(&self) -> Option<Vec<NodeDefaultLink>> {
None
}
fn default_outputs(&self) -> Option<Vec<NodeDefaultLink>> {
None
}
}
pub trait NodeFactory: Send {
fn new_config(
&self,
name: &str,
inputs: &[String],
outputs: &[String],
bt: &BTreeMap<String, Value>,
) -> Result<Box<dyn NodeConfig>, String>;
fn new_node(&self, config: &dyn NodeConfig) -> Box<dyn Node>;
fn default_input_ports(&self) -> PortConfig;
fn default_output_ports(&self) -> PortConfig;
}
type NodeTypeMap = BTreeMap<String, Box<dyn NodeFactory>>;
fn node_types() -> &'static Mutex<NodeTypeMap> {
static NODE_TYPES: OnceLock<Mutex<NodeTypeMap>> = OnceLock::new();
NODE_TYPES.get_or_init(|| Mutex::new(BTreeMap::new()))
}
pub fn register_node(name: &str, factory: Box<dyn NodeFactory>) {
node_types().lock().unwrap().insert(name.into(), factory);
}
fn with_node_type<T>(node_type: &str, f: impl Fn(&Box<dyn NodeFactory>) -> T) -> Option<T>
where
T: Sized,
{
node_types().lock().unwrap().get(node_type).map(f)
}
pub fn is_valid_type(node_type: &str) -> bool {
with_node_type(node_type, |_| true).unwrap_or(false)
}
pub fn default_input_ports(node_type: &str) -> Option<PortConfig> {
with_node_type(node_type, |nf| nf.default_input_ports())
}
pub fn default_output_ports(node_type: &str) -> Option<PortConfig> {
with_node_type(node_type, |nf| nf.default_output_ports())
}
pub fn new_config(
node_type: &str,
name: &str,
inputs: &[String],
outputs: &[String],
bt: &BTreeMap<String, Value>,
) -> Result<Box<dyn NodeConfig>, String> {
match with_node_type(node_type, |nf| nf.new_config(name, inputs, outputs, bt)) {
Some(Ok(ok)) => Ok(ok),
Some(Err(e)) => Err(e),
None => Err(format!("no such node type: {node_type}")),
}
}
pub fn new_node(node_type: &str, config: &dyn NodeConfig) -> Result<Box<dyn Node>, String> {
with_node_type(node_type, |nf| nf.new_node(config))
.ok_or(format!("no such node type: {node_type}"))
}
pub mod implicit {
use super::*;
#[derive(Clone)]
pub struct Implicit {}
impl Node for Implicit {}
pub struct ImplicitFactory {}
#[derive(Debug)]
pub struct ImplicitConfig {}
impl NodeConfig for ImplicitConfig {
fn as_any(&self) -> &dyn Any {
self
}
}
impl NodeFactory for ImplicitFactory {
fn default_input_ports(&self) -> PortConfig {
PortConfig {
defaults: None,
user_defined_ports: true,
}
}
fn default_output_ports(&self) -> PortConfig {
PortConfig {
defaults: None,
user_defined_ports: true,
}
}
fn new_config(
&self,
_name: &str,
_inputs: &[String],
_outputs: &[String],
_bt: &BTreeMap<String, Value>,
) -> Result<Box<dyn NodeConfig>, String> {
Ok(Box::new(ImplicitConfig {}))
}
fn new_node(&self, _config: &dyn NodeConfig) -> Box<dyn Node> {
Box::new(Implicit {})
}
}
}