Quick Start
Build your first rule in minutes.
Create a Simple Rule
Rules are defined in JSON and consist of actions (tasks) that process data sequentially.
use dataflow_rs::{Engine, Workflow};
use dataflow_rs::engine::message::Message;
use serde_json::json;
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define a rule that transforms data
let rule_json = r#"{
"id": "greeting_rule",
"name": "Greeting Rule",
"tasks": [
{
"id": "create_greeting",
"name": "Create Greeting",
"function": {
"name": "map",
"input": {
"mappings": [
{
"path": "data.greeting",
"logic": { "cat": ["Hello, ", {"var": "data.name"}, "!"] }
}
]
}
}
}
]
}"#;
// Parse the rule
let rule = Workflow::from_json(rule_json)?;
// Create the engine (compiles all logic at startup)
let engine = Engine::new(vec![rule], None);
// Create a message with payload
let payload = Arc::new(json!({"name": "World"}));
let mut message = Message::new(payload);
// Load payload into data context
message.context["data"]["name"] = json!("World");
// Process the message
engine.process_message(&mut message).await?;
// Print the result
println!("Greeting: {}", message.data()["greeting"]);
Ok(())
}
Try It Interactively
Want more features? Try the Full Debugger UI with step-by-step execution and rule visualization.
Understanding the Code
- Rule Definition - JSON structure defining actions (tasks) to execute
- Engine Creation - Compiles all JSONLogic expressions at startup
- Message Creation - Input data wrapped in a Message structure
- Processing - Engine evaluates each rule’s condition and executes matching actions
- Result - Modified message with transformed data and audit trail
Add Validation
Extend your rule with data validation:
{
"id": "validated_rule",
"name": "Validated Rule",
"tasks": [
{
"id": "validate_input",
"name": "Validate Input",
"function": {
"name": "validation",
"input": {
"rules": [
{
"logic": { "!!": {"var": "data.name"} },
"message": "Name is required"
}
]
}
}
},
{
"id": "create_greeting",
"name": "Create Greeting",
"function": {
"name": "map",
"input": {
"mappings": [
{
"path": "data.greeting",
"logic": { "cat": ["Hello, ", {"var": "data.name"}, "!"] }
}
]
}
}
}
]
}
Next Steps
- Basic Concepts - Understand the core architecture
- Map Function - Learn about data transformation
- Validation - Learn about data validation