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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Node registry — maps node type keys (e.g., "image-compress") to NodeProcessor
// instances. Decouples the executor from specific node types; consumers
// register only the processors they need (all 6 for WASM, subset for CLI/tests).
use crate::metadata::NodeMetadata;
use crate::processor::NodeProcessor;
use std::collections::HashMap;
// =============================================================================
// Node Registry
// =============================================================================
/// A registry that maps node type keys (e.g., "image-compress") to node processors.
///
/// Uses `Box<dyn NodeProcessor>` for dynamic dispatch -- the registry
/// holds heterogeneous processor types behind a single trait object.
pub struct NodeRegistry {
/// Maps node type keys (e.g., "image-compress") to processor instances.
processors: HashMap<String, Box<dyn NodeProcessor>>,
}
impl NodeRegistry {
/// Create a new empty registry.
pub fn new() -> Self {
Self {
processors: HashMap::new(),
}
}
/// Register a processor under a node type key.
///
/// The key should be the node type name
/// (e.g., "image-compress", "spreadsheet-clean").
///
/// If a processor is already registered under this key, it will
/// be replaced (last registration wins).
///
/// # Arguments
/// - `key` — the node type key (e.g., "image-compress")
/// - `processor` — the processor instance to register
pub fn register(&mut self, key: &str, processor: Box<dyn NodeProcessor>) {
self.processors.insert(key.to_string(), processor);
}
/// Look up the processor for a given node type.
///
/// Does a direct lookup by `node_type` key (e.g., "image-compress").
///
/// # Arguments
/// - `node_type` — the node's type field (e.g., "image-compress", "spreadsheet-clean")
/// - `_params` — the node's params map (reserved for future use)
///
/// # Returns
/// - `Some(&dyn NodeProcessor)` — if a matching processor was found
/// - `None` — if no processor matches the key
pub fn resolve(
&self,
node_type: &str,
_params: &serde_json::Map<String, serde_json::Value>,
) -> Option<&dyn NodeProcessor> {
self.processors.get(node_type).map(|b| b.as_ref())
}
/// Check how many processors are registered (useful for tests).
pub fn len(&self) -> usize {
self.processors.len()
}
/// Check if the registry is empty.
pub fn is_empty(&self) -> bool {
self.processors.is_empty()
}
/// Collect metadata from ALL registered processors into a catalog.
///
/// This returns a `Vec<NodeMetadata>` — one entry per registered processor.
/// The order is determined by the HashMap's iteration order (not guaranteed,
/// but deterministic for a given build). The `node_catalog()` WASM function
/// sorts the output by node type for stable snapshots.
pub fn catalog(&self) -> Vec<NodeMetadata> {
self.processors.values().map(|p| p.metadata()).collect()
}
}
impl Default for NodeRegistry {
fn default() -> Self {
Self::new()
}
}
// =============================================================================
// Tests
// =============================================================================
#[cfg(test)]
mod tests {
use super::*;
use crate::context::ProcessContext;
use crate::errors::BntoError;
use crate::processor::{NodeInput, NodeOutput, OutputFile};
use crate::progress::ProgressReporter;
// --- Mock Processor for Testing ---
/// A simple mock processor that just echoes input back.
struct MockProcessor {
/// A name to identify this mock in tests.
mock_name: String,
}
impl MockProcessor {
fn new(name: &str) -> Self {
Self {
mock_name: name.to_string(),
}
}
}
impl NodeProcessor for MockProcessor {
fn name(&self) -> &str {
&self.mock_name
}
fn process(
&self,
input: NodeInput,
_progress: &ProgressReporter,
_ctx: &dyn ProcessContext,
) -> Result<NodeOutput, BntoError> {
Ok(NodeOutput {
files: vec![OutputFile {
data: input.data,
filename: input.filename,
mime_type: input
.mime_type
.unwrap_or_else(|| "application/octet-stream".to_string()),
metadata: serde_json::Map::new(),
}],
metadata: serde_json::Map::new(),
})
}
}
// --- Tests ---
#[test]
fn test_new_registry_is_empty() {
let registry = NodeRegistry::new();
assert!(registry.is_empty());
assert_eq!(registry.len(), 0);
}
#[test]
fn test_register_and_resolve() {
let mut registry = NodeRegistry::new();
registry.register("image-compress", Box::new(MockProcessor::new("compress")));
let params = serde_json::Map::new();
// Resolve should find the processor by node type key.
let processor = registry.resolve("image-compress", ¶ms);
assert!(processor.is_some());
assert_eq!(processor.unwrap().name(), "compress");
}
#[test]
fn test_resolve_unknown_type_returns_none() {
let registry = NodeRegistry::new();
let params = serde_json::Map::new();
// Empty registry → None.
let processor = registry.resolve("unknown-type", ¶ms);
assert!(processor.is_none());
}
#[test]
fn test_register_multiple_processors() {
let mut registry = NodeRegistry::new();
registry.register("image-compress", Box::new(MockProcessor::new("compress")));
registry.register("image-resize", Box::new(MockProcessor::new("resize")));
registry.register(
"spreadsheet-clean",
Box::new(MockProcessor::new("clean-csv")),
);
assert_eq!(registry.len(), 3);
let params = serde_json::Map::new();
// Each resolves to the correct processor by type key.
assert_eq!(
registry.resolve("image-compress", ¶ms).unwrap().name(),
"compress"
);
assert_eq!(
registry.resolve("image-resize", ¶ms).unwrap().name(),
"resize"
);
assert_eq!(
registry
.resolve("spreadsheet-clean", ¶ms)
.unwrap()
.name(),
"clean-csv"
);
}
#[test]
fn test_register_overwrites_existing() {
let mut registry = NodeRegistry::new();
registry.register("image-compress", Box::new(MockProcessor::new("old")));
registry.register("image-compress", Box::new(MockProcessor::new("new")));
let params = serde_json::Map::new();
// Last registration wins.
assert_eq!(
registry.resolve("image-compress", ¶ms).unwrap().name(),
"new"
);
}
}