use std::any::Any;
use std::sync::Arc;
use streamweave::graph;
use streamweave::graph::Graph;
use streamweave::nodes::time::FormatTimeNode;
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 (format_tx, format_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! {
format_time: FormatTimeNode::new("format_time".to_string()),
graph.configuration => format_time.configuration,
graph.input => format_time.in,
graph.format => format_time.format,
format_time.out => graph.output,
format_time.error => graph.error
};
graph.connect_input_channel("configuration", config_rx)?;
graph.connect_input_channel("input", input_rx)?;
graph.connect_input_channel("format", format_rx)?;
graph.connect_output_channel("output", output_tx)?;
graph.connect_output_channel("error", error_tx)?;
println!("✓ Graph built with FormatTimeNode using graph! macro");
let _ = config_tx
.send(Arc::new(()) as Arc<dyn Any + Send + Sync>)
.await;
println!("📥 Sending timestamps and format strings");
println!(" Test 1: Custom format for timestamp 1705312245123");
format_tx
.send(Arc::new("%Y-%m-%d %H:%M:%S".to_string()) as Arc<dyn Any + Send + Sync>)
.await
.unwrap();
input_tx
.send(Arc::new(1705312245123i64) as Arc<dyn Any + Send + Sync>)
.await
.unwrap();
println!(" Test 2: Default format for timestamp 1609459200000");
input_tx
.send(Arc::new(1609459200000i64) as Arc<dyn Any + Send + Sync>)
.await
.unwrap();
println!(" Test 3: Custom format for timestamp 946684800000");
format_tx
.send(Arc::new("%A, %B %e, %Y".to_string()) as Arc<dyn Any + Send + Sync>)
.await
.unwrap();
input_tx
.send(Arc::new(946684800000i64) as Arc<dyn Any + Send + Sync>)
.await
.unwrap();
println!(" Test 4: Custom format for timestamp 1704067200000");
format_tx
.send(Arc::new("%Y/%m/%d".to_string()) as Arc<dyn Any + Send + Sync>)
.await
.unwrap();
input_tx
.send(Arc::new(1704067200000i64) as Arc<dyn Any + Send + Sync>)
.await
.unwrap();
println!("✓ All test cases sent");
drop(config_tx);
drop(input_tx);
drop(format_tx);
println!("Executing graph with FormatTimeNode...");
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(arc_formatted) = item.downcast::<String>()
{
output_results.push((*arc_formatted).clone());
println!(" Output: '{}'", *arc_formatted);
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 {} formatted timestamps via output channel",
output_results.len()
);
println!("✓ Received {} errors via error channel", error_count);
println!("✓ Total completed in {:?}", start.elapsed());
let expected_results = vec![
"2024-01-15 09:50:45".to_string(), "2024-01-15 09:50:45".to_string(), "Saturday, January 1, 2000".to_string(), "2024/01/01".to_string(), ];
if output_results == expected_results && error_count == 0 {
println!("✓ FormatTimeNode correctly formatted timestamps");
println!(" Examples:");
println!(
" Test 1: Timestamp 1705312245123 with format '%Y-%m-%d %H:%M:%S' -> '2024-01-15 09:50:45'"
);
println!(" Test 2: Timestamp 1609459200000 with same format -> '2024-01-15 09:50:45'");
println!(
" Test 3: Timestamp 946684800000 with format '%A, %B %e, %Y' -> 'Saturday, January 1, 2000'"
);
println!(" Test 4: Timestamp 1704067200000 with format '%Y/%m/%d' -> '2024/01/01'");
} else {
println!(
"⚠ FormatTimeNode behavior may be unexpected (received: {:?}, expected: {:?}, errors: {})",
output_results, expected_results, error_count
);
}
Ok(())
}