Skip to main content

enact_core/graph/
edge.rs

1//! Edge types for graph connections
2
3/// Target for an edge - either a specific node or END
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum EdgeTarget {
6    /// Target a specific node by name
7    Node(String),
8    /// End the graph execution
9    End,
10}
11
12impl EdgeTarget {
13    pub fn node(name: impl Into<String>) -> Self {
14        Self::Node(name.into())
15    }
16
17    pub fn end() -> Self {
18        Self::End
19    }
20
21    pub fn is_end(&self) -> bool {
22        matches!(self, Self::End)
23    }
24}
25
26/// Edge in the graph
27#[derive(Debug, Clone)]
28pub struct Edge {
29    pub from: String,
30    pub to: EdgeTarget,
31}
32
33impl Edge {
34    pub fn new(from: impl Into<String>, to: EdgeTarget) -> Self {
35        Self {
36            from: from.into(),
37            to,
38        }
39    }
40}
41
42/// Conditional edge - routes based on a function
43#[derive(Clone)]
44pub struct ConditionalEdge {
45    pub from: String,
46    pub router: ConditionalRouter,
47}
48
49/// Router function type
50pub type ConditionalRouter = std::sync::Arc<dyn Fn(&str) -> EdgeTarget + Send + Sync>;
51
52impl std::fmt::Debug for ConditionalEdge {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        f.debug_struct("ConditionalEdge")
55            .field("from", &self.from)
56            .field("router", &"<fn>")
57            .finish()
58    }
59}