use std::any::Any;
use std::sync::Arc;
use streamweave::graph;
use streamweave::graph::Graph;
use streamweave::nodes::map_node::{MapConfig, MapNode, map_config};
use tokio::sync::mpsc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (config_tx, config_rx) = mpsc::channel(1);
let (input_tx, input_rx) = mpsc::channel(5);
let (output_tx, mut output_rx) = mpsc::channel::<Arc<dyn Any + Send + Sync>>(10);
let square_config: MapConfig = map_config(|value| async move {
if let Ok(arc_i32) = value.downcast::<i32>() {
let num = *arc_i32;
Ok(Arc::new(num * num) as Arc<dyn Any + Send + Sync>)
} else {
Err("Expected i32".to_string())
}
});
let mut graph: Graph = graph! {
map: MapNode::new("map".to_string()),
graph.configuration => map.configuration,
graph.input => map.in,
map.out => graph.output
};
graph.connect_input_channel("configuration", config_rx)?;
graph.connect_input_channel("input", input_rx)?;
graph.connect_output_channel("output", output_tx)?;
println!("✓ Graph built with graph! macro");
let _ = config_tx
.send(Arc::new(square_config) as Arc<dyn Any + Send + Sync>)
.await;
for num in 1..=3 {
let _ = input_tx
.send(Arc::new(num) as Arc<dyn Any + Send + Sync>)
.await;
}
println!("✓ Data sent to channels");
println!("Executing graph...");
let start = std::time::Instant::now();
graph
.execute()
.await
.map_err(|e| format!("Graph execution failed: {:?}", e))?;
println!("✓ Graph execution completed in {:?}", start.elapsed());
drop(config_tx);
drop(input_tx);
println!("Reading results from output channel...");
let mut count = 0;
loop {
match tokio::time::timeout(tokio::time::Duration::from_millis(100), output_rx.recv()).await {
Ok(Some(item)) => {
if let Ok(arc_i32) = item.downcast::<i32>() {
let result = *arc_i32;
println!(" {}² = {}", count + 1, result);
count += 1;
if count >= 3 {
break;
}
}
}
Ok(None) => {
println!("Output channel closed");
break;
}
Err(_) => {
}
}
}
println!("✓ Received {} results via connected channels", count);
println!("✓ Total completed in {:?}", start.elapsed());
Ok(())
}