1use serde::{Deserialize, Serialize};
7use std::fmt;
8
9#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
23pub struct NodeId(String);
24
25impl NodeId {
26 pub fn new(name: impl Into<String>) -> Self {
28 Self(name.into())
29 }
30
31 pub fn as_str(&self) -> &str {
33 &self.0
34 }
35
36 pub fn is_start(&self) -> bool {
38 self.0 == START_NAME
39 }
40
41 pub fn is_end(&self) -> bool {
43 self.0 == END_NAME
44 }
45
46 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
100const START_NAME: &str = "__start__";
106
107const END_NAME: &str = "__end__";
109
110pub static START: NodeId = NodeId(String::new()); pub static END: NodeId = NodeId(String::new()); #[inline]
153pub fn start() -> NodeId {
154 NodeId::new(START_NAME)
155}
156
157#[inline]
161pub fn end() -> NodeId {
162 NodeId::new(END_NAME)
163}
164
165pub const START_NODE: &str = START_NAME;
171
172pub 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}