use std::any::Any;
use std::sync::Arc;
use streamweave::graph;
use streamweave::graph::Graph;
use streamweave::nodes::string::StringSliceNode;
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 (start_tx, start_rx) = mpsc::channel(10);
let (end_tx, end_rx) = mpsc::channel(10);
let (output_tx, mut output_rx) = mpsc::channel::<Arc<dyn Any + Send + Sync>>(10);
let (error_tx, mut error_rx) = mpsc::channel::<Arc<dyn Any + Send + Sync>>(10);
let mut graph: Graph = graph! {
slice: StringSliceNode::new("slice".to_string()),
graph.configuration => slice.configuration,
graph.input => slice.in,
graph.start => slice.start,
graph.end => slice.end,
slice.out => graph.output,
slice.error => graph.error
};
graph.connect_input_channel("configuration", config_rx)?;
graph.connect_input_channel("input", input_rx)?;
graph.connect_input_channel("start", start_rx)?;
graph.connect_input_channel("end", end_rx)?;
graph.connect_output_channel("output", output_tx)?;
graph.connect_output_channel("error", error_tx)?;
println!("✓ Graph built with StringSliceNode using graph! macro");
let _ = config_tx
.send(Arc::new(()) as Arc<dyn Any + Send + Sync>)
.await;
println!("📥 Sending strings, start indices, and end indices to slice");
let test_input_strings = [
"Hello World".to_string(),
"Rust Programming".to_string(),
"Test String".to_string(),
"Unicode: 🚀".to_string(),
"Single".to_string(),
"Empty".to_string(),
"Bounds".to_string(),
];
let test_start_indices = [0, 5, 0, 9, 0, 0, 3];
let test_end_indices = [5, 16, 4, 10, 6, 0, 6];
for i in 0..test_input_strings.len() {
let input_str = &test_input_strings[i];
let start_idx = test_start_indices[i];
let end_idx = test_end_indices[i];
let expected = if start_idx <= end_idx {
let char_count = input_str.chars().count();
if end_idx <= char_count {
let mut start_byte = 0;
let mut end_byte = input_str.len();
for (char_idx, (byte_idx, _)) in input_str.char_indices().enumerate() {
if char_idx == start_idx {
start_byte = byte_idx;
}
if char_idx == end_idx {
end_byte = byte_idx;
break;
}
}
if end_idx == char_count {
end_byte = input_str.len();
}
&input_str[start_byte..end_byte]
} else {
"<invalid range>"
}
} else {
"<invalid range>"
};
println!(
" Slicing '{}' from {} to {} -> expected '{}'",
input_str, start_idx, end_idx, expected
);
input_tx
.send(Arc::new(input_str.clone()) as Arc<dyn Any + Send + Sync>)
.await
.unwrap();
start_tx
.send(Arc::new(start_idx) as Arc<dyn Any + Send + Sync>)
.await
.unwrap();
end_tx
.send(Arc::new(end_idx) as Arc<dyn Any + Send + Sync>)
.await
.unwrap();
}
println!("✓ Test data sent to input channels");
drop(config_tx);
drop(input_tx);
drop(start_tx);
drop(end_tx);
println!("Executing graph with StringSliceNode...");
let start = std::time::Instant::now();
graph
.execute()
.await
.map_err(|e| format!("Graph execution failed: {:?}", e))?;
println!("✓ Graph execution completed in {:?}", start.elapsed());
println!("Reading results from output channels...");
let mut output_results = Vec::new();
let mut error_count = 0;
loop {
let output_result =
tokio::time::timeout(tokio::time::Duration::from_millis(500), output_rx.recv()).await;
let error_result =
tokio::time::timeout(tokio::time::Duration::from_millis(500), error_rx.recv()).await;
let mut has_data = false;
if let Ok(Some(item)) = output_result
&& let Ok(result_str) = item.downcast::<String>()
{
output_results.push((*result_str).clone());
println!(" Output: '{}'", *result_str);
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 {} results via output channel",
output_results.len()
);
println!("✓ Received {} errors via error channel", error_count);
println!("✓ Total completed in {:?}", start.elapsed());
let expected_results = vec![
"Hello".to_string(),
"Programming".to_string(),
"Test".to_string(),
"🚀".to_string(),
"Single".to_string(),
"".to_string(),
"nds".to_string(), ];
if output_results == expected_results && error_count == 0 {
println!("✓ StringSliceNode correctly extracted substrings");
println!(" Examples:");
for (i, result) in output_results.iter().enumerate() {
println!(
" '{}'[{}-{}] -> '{}'",
test_input_strings[i], test_start_indices[i], test_end_indices[i], result
);
}
} else {
println!(
"⚠ StringSliceNode behavior may be unexpected (outputs: {:?}, expected: {:?}, errors: {})",
output_results, expected_results, error_count
);
}
Ok(())
}