use rust_langgraph::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
struct CounterState {
count: i32,
}
impl State for CounterState {
fn merge(&mut self, other: Self) -> Result<()> {
self.count += other.count;
Ok(())
}
}
#[tokio::main]
async fn main() -> Result<()> {
println!("=== Simple Graph Example ===\n");
let mut graph = StateGraph::new();
graph.add_node("add_five", |mut state: CounterState, _config: &Config| async move {
println!("Adding 5 to count: {}", state.count);
state.count += 5;
Ok(state)
});
graph.add_node("multiply_two", |mut state: CounterState, _config: &Config| async move {
println!("Multiplying count by 2: {}", state.count);
state.count *= 2;
Ok(state)
});
graph.set_entry_point("add_five");
graph.add_edge("add_five", "multiply_two");
graph.set_finish_point("multiply_two");
let mut app = graph.compile(None)?;
let initial_state = CounterState { count: 10 };
println!("Initial count: {}\n", initial_state.count);
let result = app.invoke(initial_state, Config::default()).await?;
println!("\nFinal count: {}", result.count);
println!("Calculation: (10 + 5) * 2 = {}", result.count);
Ok(())
}