Skip to main content

regula_core/
constants.rs

1//! Constants and fundamental types for REGULA framework.
2//!
3//! This module defines the `NodeId` type and special sentinel nodes
4//! `START` and `END` used to mark graph entry and exit points.
5
6use serde::{Deserialize, Serialize};
7use std::fmt;
8
9/// Unique identifier for a node in the graph.
10///
11/// `NodeId` is a newtype wrapper around `String` that provides type safety
12/// and convenient conversions from string-like types.
13///
14/// # Examples
15///
16/// ```
17/// use regula_core::NodeId;
18///
19/// let id: NodeId = "agent".into();
20/// assert_eq!(id.as_str(), "agent");
21/// ```
22#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
23pub struct NodeId(String);
24
25impl NodeId {
26    /// Create a new `NodeId` from a string.
27    pub fn new(name: impl Into<String>) -> Self {
28        Self(name.into())
29    }
30
31    /// Get the node ID as a string slice.
32    pub fn as_str(&self) -> &str {
33        &self.0
34    }
35
36    /// Check if this is the START sentinel node.
37    pub fn is_start(&self) -> bool {
38        self.0 == START_NAME
39    }
40
41    /// Check if this is the END sentinel node.
42    pub fn is_end(&self) -> bool {
43        self.0 == END_NAME
44    }
45
46    /// Check if this is a special sentinel node (START or END).
47    pub fn is_sentinel(&self) -> bool {
48        self.is_start() || self.is_end()
49    }
50}
51
52impl fmt::Display for NodeId {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        write!(f, "{}", self.0)
55    }
56}
57
58impl From<&str> for NodeId {
59    fn from(s: &str) -> Self {
60        Self(s.to_string())
61    }
62}
63
64impl From<String> for NodeId {
65    fn from(s: String) -> Self {
66        Self(s)
67    }
68}
69
70impl From<&String> for NodeId {
71    fn from(s: &String) -> Self {
72        Self(s.clone())
73    }
74}
75
76impl AsRef<str> for NodeId {
77    fn as_ref(&self) -> &str {
78        &self.0
79    }
80}
81
82impl PartialEq<str> for NodeId {
83    fn eq(&self, other: &str) -> bool {
84        self.0 == other
85    }
86}
87
88impl PartialEq<&str> for NodeId {
89    fn eq(&self, other: &&str) -> bool {
90        self.0 == *other
91    }
92}
93
94impl PartialEq<String> for NodeId {
95    fn eq(&self, other: &String) -> bool {
96        &self.0 == other
97    }
98}
99
100// ============================================================================
101// Sentinel Nodes
102// ============================================================================
103
104/// Internal name for the START sentinel node.
105const START_NAME: &str = "__start__";
106
107/// Internal name for the END sentinel node.
108const END_NAME: &str = "__end__";
109
110/// The START sentinel node.
111///
112/// This represents the entry point of the graph. Use `start()` when adding
113/// edges from the beginning of the graph to the first node(s).
114///
115/// # Examples
116///
117/// ```
118/// use regula_core::{start, NodeId};
119///
120/// // Add an edge from START to the first node
121/// let s = start();
122///
123/// assert!(s.is_start());
124/// assert!(s.is_sentinel());
125/// ```
126pub static START: NodeId = NodeId(String::new()); // Placeholder, replaced at runtime
127
128/// The END sentinel node.
129///
130/// This represents the termination point of the graph. Routes that return
131/// `end()` will cause the graph to complete execution.
132///
133/// # Examples
134///
135/// ```
136/// use regula_core::{end, NodeId};
137///
138/// // Route to END when done
139/// let e = end();
140///
141/// assert!(e.is_end());
142/// assert!(e.is_sentinel());
143/// ```
144pub static END: NodeId = NodeId(String::new()); // Placeholder, replaced at runtime
145
146// We can't use const with String, so we use lazy initialization pattern
147// The actual START and END are created via functions
148
149/// Get the START node ID.
150///
151/// This is the entry point of every graph.
152#[inline]
153pub fn start() -> NodeId {
154    NodeId::new(START_NAME)
155}
156
157/// Get the END node ID.
158///
159/// This represents graph termination.
160#[inline]
161pub fn end() -> NodeId {
162    NodeId::new(END_NAME)
163}
164
165// Re-export as constants using lazy_static pattern would be ideal,
166// but for simplicity we provide the functions above.
167// Users can also use the string literals directly: "START", "END"
168
169/// Constant for START node name - use in pattern matching.
170pub const START_NODE: &str = START_NAME;
171
172/// Constant for END node name - use in pattern matching.
173pub const END_NODE: &str = END_NAME;
174
175#[cfg(test)]
176mod tests {
177    use super::*;
178
179    #[test]
180    fn test_node_id_creation() {
181        let id = NodeId::new("agent");
182        assert_eq!(id.as_str(), "agent");
183    }
184
185    #[test]
186    fn test_node_id_from_str() {
187        let id: NodeId = "tools".into();
188        assert_eq!(id.as_str(), "tools");
189    }
190
191    #[test]
192    fn test_node_id_equality() {
193        let id1 = NodeId::new("agent");
194        let id2: NodeId = "agent".into();
195        assert_eq!(id1, id2);
196        assert_eq!(id1, "agent");
197    }
198
199    #[test]
200    fn test_sentinel_nodes() {
201        let start = start();
202        let end = end();
203
204        assert!(start.is_start());
205        assert!(!start.is_end());
206        assert!(start.is_sentinel());
207
208        assert!(end.is_end());
209        assert!(!end.is_start());
210        assert!(end.is_sentinel());
211    }
212
213    #[test]
214    fn test_regular_node_not_sentinel() {
215        let id = NodeId::new("agent");
216        assert!(!id.is_sentinel());
217        assert!(!id.is_start());
218        assert!(!id.is_end());
219    }
220
221    #[test]
222    fn test_node_id_display() {
223        let id = NodeId::new("my_agent");
224        assert_eq!(format!("{}", id), "my_agent");
225    }
226
227    #[test]
228    fn test_node_id_serialization() {
229        let id = NodeId::new("agent");
230        let json = serde_json::to_string(&id).unwrap();
231        assert_eq!(json, "\"agent\"");
232
233        let parsed: NodeId = serde_json::from_str(&json).unwrap();
234        assert_eq!(parsed, id);
235    }
236}