Crates.io | chain_reaction |
lib.rs | chain_reaction |
version | 0.2.50 |
source | src |
created_at | 2024-08-26 00:01:55.583797 |
updated_at | 2024-08-26 00:11:56.697904 |
description | simple beautiful timed function chaining |
homepage | |
repository | https://github.com/incredimo/chain_reaction |
max_upload_size | |
id | 1351552 |
size | 26,373 |
let increment = |x| Ok(x + 1);
let double = |x| Ok(x * 2);
let stringify = |x| Ok(x.to_string());
let result = TimedReactor::input(5)
.then(increment)
.then(double)
.then(stringify)
.run();
println!("{:?}", result); // Output: Ok("12")
Welcome to ChainReaction – a powerful Rust crate designed to simplify complex data processing pipelines, workflows, and real-time systems by providing seamless chaining of operations with built-in error handling and precise timing.
ChainReaction allows you to compose sequences of operations (or "actions") that can be chained together to form complex workflows. Each operation can handle errors gracefully, and the timing of each operation is automatically tracked and recorded, enabling detailed performance analysis.
Add ChainReaction to your Cargo.toml
:
[dependencies]
chain_reaction = "0.1.0"
Here's how you can get started with ChainReaction:
use chain_reaction::{TimedReactor, Failure};
let increment = |x| Ok(x + 1);
let double = |x| Ok(x * 2);
let stringify = |x| Ok(x.to_string());
let result = TimedReactor::input(5)
.then(increment)
.then(double)
.then(stringify)
.run();
match result {
Ok(value) => println!("Final result: {}", value),
Err(e) => println!("An error occurred: {:?}", e),
}
ChainReaction allows you to execute different operations based on conditions using the if_else
method.
let (result, timings) = TimedReactor::input(4)
.if_else(
|x| *x % 2 == 0,
|x| Ok(x * 2),
|x| Ok(x + 1),
)
.run();
match result {
Ok(value) => println!("Conditional result: {}", value),
Err(e) => println!("An error occurred: {:?}", e),
}
println!("Timings: {:?}", timings);
You can process collections of items using the for_each
method.
let square = |x| Ok(x * x);
let (result, timings) = TimedReactor::input(vec![1, 2, 3, 4])
.for_each(square)
.run();
match result {
Ok(values) => println!("Squared values: {:?}", values),
Err(e) => println!("An error occurred: {:?}", e),
}
println!("Timings: {:?}", timings);
ChainReaction supports merging operations from a collection, such as summing pairs of elements and then applying further transformations.
let sum = |a, b| a + b;
let square = |x| Ok(x * x);
let (result, timings) = TimedReactor::input(vec![10, 20, 30, 40])
.merge(sum)
.then(square)
.run();
match result {
Ok(value) => println!("Merge and square result: {}", value),
Err(e) => println!("An error occurred: {:?}", e),
}
println!("Timings: {:?}", timings);
ChainReaction offers a powerful combination of ease of use, error resilience, performance monitoring, and flexibility. It stands out by simplifying the construction of complex workflows and allowing you to focus on business logic without getting bogged down in error handling or performance issues.
Whether you're building data processing pipelines, complex business workflows, or real-time event processing systems, ChainReaction provides the tools you need to create efficient, maintainable, and robust systems.