use std::any::Any;
use std::sync::Arc;
use streamweave::graph;
use streamweave::graph::Graph;
use streamweave::nodes::advanced::switch_node::{SwitchConfig, SwitchNode, switch_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(10);
let (value_tx, value_rx) = mpsc::channel(10);
let (out0_tx, mut out0_rx) = mpsc::channel::<Arc<dyn Any + Send + Sync>>(10);
let (out1_tx, mut out1_rx) = mpsc::channel::<Arc<dyn Any + Send + Sync>>(10);
let (default_tx, mut default_rx) = mpsc::channel::<Arc<dyn Any + Send + Sync>>(10);
let (error_tx, mut error_rx) = mpsc::channel::<Arc<dyn Any + Send + Sync>>(10);
let switch_config: SwitchConfig = switch_config(|_data, value| async move {
if let Ok(value_arc) = value.downcast::<i32>() {
let switch_value = *value_arc;
if (0..10).contains(&switch_value) {
Ok(Some(0))
} else if (10..20).contains(&switch_value) {
Ok(Some(1))
} else {
Ok(None) }
} else {
Err("Expected i32 for switch value".to_string())
}
});
let mut graph: Graph = graph! {
switch: SwitchNode::new("switch".to_string(), 2), graph.configuration => switch.configuration,
graph.input => switch.in,
graph.value => switch.value,
switch.out_0 => graph.out_0,
switch.out_1 => graph.out_1,
switch.default => graph.default,
switch.error => graph.error
};
graph.connect_input_channel("configuration", config_rx)?;
graph.connect_input_channel("input", input_rx)?;
graph.connect_input_channel("value", value_rx)?;
graph.connect_output_channel("out_0", out0_tx)?;
graph.connect_output_channel("out_1", out1_tx)?;
graph.connect_output_channel("default", default_tx)?;
graph.connect_output_channel("error", error_tx)?;
println!("✓ Graph built with SwitchNode using graph! macro");
let _ = config_tx
.send(Arc::new(switch_config) as Arc<dyn Any + Send + Sync>)
.await;
let test_cases = vec![
("data_A", 5), ("data_B", 15), ("data_C", 25), ("data_D", 8), ("data_E", 12), ];
for (data, switch_value) in test_cases {
let _ = input_tx
.send(Arc::new(data.to_string()) as Arc<dyn Any + Send + Sync>)
.await;
let _ = value_tx
.send(Arc::new(switch_value) as Arc<dyn Any + Send + Sync>)
.await;
tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
}
println!("✓ Configuration sent and test data sent to input channels");
println!("Executing graph with SwitchNode...");
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);
drop(value_tx);
println!("Reading results from output channels...");
let mut out0_count = 0;
let mut out1_count = 0;
let mut default_count = 0;
let mut error_count = 0;
loop {
let out0_result =
tokio::time::timeout(tokio::time::Duration::from_millis(100), out0_rx.recv()).await;
let out1_result =
tokio::time::timeout(tokio::time::Duration::from_millis(100), out1_rx.recv()).await;
let default_result =
tokio::time::timeout(tokio::time::Duration::from_millis(100), default_rx.recv()).await;
let error_result =
tokio::time::timeout(tokio::time::Duration::from_millis(100), error_rx.recv()).await;
let mut has_data = false;
if let Ok(Some(item)) = out0_result
&& let Ok(data_arc) = item.downcast::<String>()
{
let data = (**data_arc).to_string();
println!(" Out_0: {}", data);
out0_count += 1;
has_data = true;
}
if let Ok(Some(item)) = out1_result
&& let Ok(data_arc) = item.downcast::<String>()
{
let data = (**data_arc).to_string();
println!(" Out_1: {}", data);
out1_count += 1;
has_data = true;
}
if let Ok(Some(item)) = default_result
&& let Ok(data_arc) = item.downcast::<String>()
{
let data = (**data_arc).to_string();
println!(" Default: {}", data);
default_count += 1;
has_data = true;
}
if let Ok(Some(item)) = error_result
&& let Ok(error_msg) = item.downcast::<String>()
{
let error = (**error_msg).to_string();
println!(" Error: {}", error);
error_count += 1;
has_data = true;
}
if !has_data {
break;
}
}
println!("✓ Received {} items via out_0 channel", out0_count);
println!("✓ Received {} items via out_1 channel", out1_count);
println!("✓ Received {} items via default channel", default_count);
println!("✓ Received {} errors via error channel", error_count);
println!("✓ Total completed in {:?}", start.elapsed());
if out0_count == 2 && out1_count == 2 && default_count == 1 && error_count == 0 {
println!("✓ SwitchNode correctly routed items based on switch values");
} else {
println!(
"⚠ SwitchNode behavior may be unexpected (out0: {}, out1: {}, default: {}, errors: {}, expected out0: 2, out1: 2, default: 1, errors: 0)",
out0_count, out1_count, default_count, error_count
);
}
Ok(())
}