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
/*!
# Dataflow-rs
A lightweight, rule-driven workflow engine for building powerful data processing pipelines and nanoservices in Rust.
## Overview
Dataflow-rs provides a flexible and extensible framework for processing data through a series of tasks organized in workflows.
The engine automatically routes messages through appropriate workflows based on configurable rules, and each workflow can
contain multiple tasks that transform, validate, or enrich the data.
## Key Components
* **Engine**: The central component that processes messages through workflows
* **Workflow**: A collection of tasks with conditions that determine when they should be applied
* **Task**: An individual processing unit that performs a specific function on a message
* **AsyncFunctionHandler**: A trait implemented by task handlers to define custom async processing logic
* **Message**: The data structure that flows through the engine, containing payload, metadata, and processing results
## Built-in Functions
The engine comes with several pre-registered functions:
* **map**: Maps and transforms data between different parts of a message
* **validate**: Validates message data against rules using JSONLogic expressions
## Usage Example
```rust,no_run
use dataflow_rs::{Engine, Workflow};
use dataflow_rs::engine::message::Message;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define a workflow in JSON
let workflow_json = r#"
{
"id": "data_processor",
"name": "Data Processor",
"priority": 0,
"tasks": [
{
"id": "transform_data",
"name": "Transform Data",
"function": {
"name": "map",
"input": {
"mappings": [
{
"path": "data.result",
"logic": { "var": "temp_data.value" }
}
]
}
}
}
]
}
"#;
// Parse the workflow
let workflow = Workflow::from_json(workflow_json)?;
// Create the workflow engine with the workflow (built-in functions are auto-registered by default)
let engine = Engine::new(vec![workflow], None);
// Create a message to process
let mut message = Message::from_value(&json!({}));
// Process the message through the workflow
match engine.process_message(&mut message).await {
Ok(_) => {
println!("Processed result: {}", message.context["data"]["result"]);
}
Err(e) => {
println!("Error in workflow: {:?}", e);
}
}
Ok(())
}
```
## Error Handling
The library provides a comprehensive error handling system:
```rust,no_run
use dataflow_rs::{Engine, Result, DataflowError};
use dataflow_rs::engine::message::Message;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<()> {
// ... setup workflows ...
let engine = Engine::new(vec![/* workflows */], None);
let mut message = Message::from_value(&json!({}));
// Process the message, errors will be collected but not halt execution
engine.process_message(&mut message).await?;
// Check if there were any errors during processing
if message.has_errors() {
for error in &message.errors {
println!("Error in workflow: {:?}, task: {:?}: {:?}",
error.workflow_id, error.task_id, error.message);
}
}
Ok(())
}
```
## Extending with Custom Functions
You can extend the engine with your own custom function handlers:
```rust,no_run
use dataflow_rs::{Engine, AsyncFunctionHandler, Result, Workflow};
use dataflow_rs::engine::{FunctionConfig, message::{Change, Message}, error::DataflowError};
use datalogic_rs::DataLogic;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::sync::Arc;
use async_trait::async_trait;
struct CustomFunction;
#[async_trait]
impl AsyncFunctionHandler for CustomFunction {
async fn execute(
&self,
message: &mut Message,
config: &FunctionConfig,
datalogic: Arc<DataLogic>,
) -> Result<(usize, Vec<Change>)> {
// Implement your custom logic here
// Extract the custom configuration from config
let input = match config {
FunctionConfig::Custom { input, .. } => input,
_ => return Err(DataflowError::Validation("Invalid configuration type".to_string())),
};
// Validate input
let required_field = input.get("field")
.ok_or_else(|| DataflowError::Validation("Missing required field".to_string()))?
.as_str()
.ok_or_else(|| DataflowError::Validation("Field must be a string".to_string()))?;
// Record changes for audit trail
let changes = vec![
Change {
path: Arc::from("data.custom_field"),
old_value: Arc::new(Value::Null),
new_value: Arc::new(json!("custom value")),
}
];
// Return success code (200) and changes
Ok((200, changes))
}
}
#[tokio::main]
async fn main() -> Result<()> {
// Create custom functions
let mut custom_functions = HashMap::new();
custom_functions.insert(
"custom".to_string(),
Box::new(CustomFunction) as Box<dyn AsyncFunctionHandler + Send + Sync>
);
// Create engine with workflows and custom functions
let engine = Engine::new(vec![/* workflows */], Some(custom_functions));
// Now it can be used in workflows...
Ok(())
}
```
*/
// Re-export all public APIs for easier access
pub use ;
pub use ;
pub use ;
pub use ;