lemon_graph/
lib.rs

1//! Async directed computation graphs, using [petgraph](https://crates.io/crates/petgraph).
2//!
3//! # Usage
4//!
5//! ```
6//! use lemon_graph::{Graph, Executor, nodes::{NodeWrapper, LogNode}};
7//!
8//! #[tokio::main]
9//! async fn main() {
10//!     let mut graph = Graph::new();
11//!
12//!     // Create a log node and set its message.
13//!     let log = LogNode::new(&mut graph);
14//!     let message = log.message(&graph).unwrap();
15//!     message.set_value(&mut graph, "Hello, world!".to_string().into());
16//!
17//!     // Create a second log node to run after the first.
18//!     let log_2 = LogNode::new(&mut graph);
19//!     log_2.run_after(&mut graph, log.0);
20//!
21//!     // Use the first log's message as input to the second log's message.
22//!     let message_2 = log_2.message(&graph).unwrap();
23//!     message_2.set_input(&mut graph, Some(message));
24//!
25//!     // Execute the graph.
26//!     Executor::execute(&mut graph, log.0).await.unwrap();
27//! }
28//! ```
29
30use nodes::{AsyncNode, SyncNode};
31use petgraph::graph::DiGraph;
32
33mod execution;
34pub mod nodes;
35mod value;
36
37pub use execution::*;
38pub use value::Value;
39
40#[derive(Debug, Clone, Copy)]
41pub enum GraphEdge {
42    /// Execution flow between nodes.
43    ExecutionFlow,
44    /// Data flow between stores.
45    DataFlow,
46    /// Data map from node -> store, or store -> node.
47    /// The usize is the index of the data in the node.
48    DataMap(usize),
49}
50
51pub enum GraphNode {
52    /// Executable async node.
53    AsyncNode(Box<dyn AsyncNode>),
54    /// Executable sync node.
55    SyncNode(Box<dyn SyncNode>),
56    /// Used as an intermediary store for data between nodes.
57    Store(Value),
58}
59
60pub type Graph = DiGraph<GraphNode, GraphEdge>;