talk/
types.rs

1//! Common type definitions used throughout the Talk library
2//!
3//! This module provides newtype wrappers around UUID for type-safe identifiers.
4
5use serde::{Deserialize, Serialize};
6use std::fmt;
7use uuid::Uuid;
8
9/// Unique identifier for an Agent
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub struct AgentId(Uuid);
12
13impl AgentId {
14    /// Create a new random AgentId
15    pub fn new() -> Self {
16        Self(Uuid::new_v4())
17    }
18
19    /// Get the underlying UUID
20    pub fn as_uuid(&self) -> &Uuid {
21        &self.0
22    }
23}
24
25impl Default for AgentId {
26    fn default() -> Self {
27        Self::new()
28    }
29}
30
31impl fmt::Display for AgentId {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        write!(f, "{}", self.0)
34    }
35}
36
37impl From<Uuid> for AgentId {
38    fn from(uuid: Uuid) -> Self {
39        Self(uuid)
40    }
41}
42
43/// Unique identifier for a Guideline
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
45pub struct GuidelineId(Uuid);
46
47impl GuidelineId {
48    /// Create a new random GuidelineId
49    pub fn new() -> Self {
50        Self(Uuid::new_v4())
51    }
52
53    /// Get the underlying UUID
54    pub fn as_uuid(&self) -> &Uuid {
55        &self.0
56    }
57}
58
59impl Default for GuidelineId {
60    fn default() -> Self {
61        Self::new()
62    }
63}
64
65impl fmt::Display for GuidelineId {
66    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67        write!(f, "{}", self.0)
68    }
69}
70
71impl From<Uuid> for GuidelineId {
72    fn from(uuid: Uuid) -> Self {
73        Self(uuid)
74    }
75}
76
77/// Unique identifier for a Tool
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
79pub struct ToolId(Uuid);
80
81impl ToolId {
82    /// Create a new random ToolId
83    pub fn new() -> Self {
84        Self(Uuid::new_v4())
85    }
86
87    /// Get the underlying UUID
88    pub fn as_uuid(&self) -> &Uuid {
89        &self.0
90    }
91}
92
93impl Default for ToolId {
94    fn default() -> Self {
95        Self::new()
96    }
97}
98
99impl fmt::Display for ToolId {
100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101        write!(f, "{}", self.0)
102    }
103}
104
105impl From<Uuid> for ToolId {
106    fn from(uuid: Uuid) -> Self {
107        Self(uuid)
108    }
109}
110
111/// Unique identifier for a Journey
112#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
113pub struct JourneyId(Uuid);
114
115impl JourneyId {
116    /// Create a new random JourneyId
117    pub fn new() -> Self {
118        Self(Uuid::new_v4())
119    }
120
121    /// Get the underlying UUID
122    pub fn as_uuid(&self) -> &Uuid {
123        &self.0
124    }
125}
126
127impl Default for JourneyId {
128    fn default() -> Self {
129        Self::new()
130    }
131}
132
133impl fmt::Display for JourneyId {
134    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135        write!(f, "{}", self.0)
136    }
137}
138
139impl From<Uuid> for JourneyId {
140    fn from(uuid: Uuid) -> Self {
141        Self(uuid)
142    }
143}
144
145/// Unique identifier for a Journey Step
146#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
147pub struct StepId(Uuid);
148
149impl StepId {
150    /// Create a new random StepId
151    pub fn new() -> Self {
152        Self(Uuid::new_v4())
153    }
154
155    /// Get the underlying UUID
156    pub fn as_uuid(&self) -> &Uuid {
157        &self.0
158    }
159}
160
161impl Default for StepId {
162    fn default() -> Self {
163        Self::new()
164    }
165}
166
167impl fmt::Display for StepId {
168    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169        write!(f, "{}", self.0)
170    }
171}
172
173impl From<Uuid> for StepId {
174    fn from(uuid: Uuid) -> Self {
175        Self(uuid)
176    }
177}
178
179/// Unique identifier for a Session
180#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
181pub struct SessionId(Uuid);
182
183impl SessionId {
184    /// Create a new random SessionId
185    pub fn new() -> Self {
186        Self(Uuid::new_v4())
187    }
188
189    /// Get the underlying UUID
190    pub fn as_uuid(&self) -> &Uuid {
191        &self.0
192    }
193}
194
195impl Default for SessionId {
196    fn default() -> Self {
197        Self::new()
198    }
199}
200
201impl fmt::Display for SessionId {
202    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
203        write!(f, "{}", self.0)
204    }
205}
206
207impl From<Uuid> for SessionId {
208    fn from(uuid: Uuid) -> Self {
209        Self(uuid)
210    }
211}
212
213/// Unique identifier for a Message
214#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
215pub struct MessageId(Uuid);
216
217impl MessageId {
218    /// Create a new random MessageId
219    pub fn new() -> Self {
220        Self(Uuid::new_v4())
221    }
222
223    /// Get the underlying UUID
224    pub fn as_uuid(&self) -> &Uuid {
225        &self.0
226    }
227}
228
229impl Default for MessageId {
230    fn default() -> Self {
231        Self::new()
232    }
233}
234
235impl fmt::Display for MessageId {
236    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
237        write!(f, "{}", self.0)
238    }
239}
240
241impl From<Uuid> for MessageId {
242    fn from(uuid: Uuid) -> Self {
243        Self(uuid)
244    }
245}
246
247#[cfg(test)]
248mod tests {
249    use super::*;
250
251    #[test]
252    fn test_agent_id_creation() {
253        let id1 = AgentId::new();
254        let id2 = AgentId::new();
255        assert_ne!(id1, id2, "AgentIds should be unique");
256    }
257
258    #[test]
259    fn test_agent_id_display() {
260        let id = AgentId::new();
261        let display_str = format!("{}", id);
262        assert!(
263            !display_str.is_empty(),
264            "Display string should not be empty"
265        );
266    }
267
268    #[test]
269    fn test_agent_id_serialization() {
270        let id = AgentId::new();
271        let json = serde_json::to_string(&id).unwrap();
272        let deserialized: AgentId = serde_json::from_str(&json).unwrap();
273        assert_eq!(
274            id, deserialized,
275            "AgentId should serialize and deserialize correctly"
276        );
277    }
278
279    #[test]
280    fn test_guideline_id_creation() {
281        let id1 = GuidelineId::new();
282        let id2 = GuidelineId::new();
283        assert_ne!(id1, id2, "GuidelineIds should be unique");
284    }
285
286    #[test]
287    fn test_tool_id_creation() {
288        let id1 = ToolId::new();
289        let id2 = ToolId::new();
290        assert_ne!(id1, id2, "ToolIds should be unique");
291    }
292
293    #[test]
294    fn test_journey_id_creation() {
295        let id1 = JourneyId::new();
296        let id2 = JourneyId::new();
297        assert_ne!(id1, id2, "JourneyIds should be unique");
298    }
299
300    #[test]
301    fn test_step_id_creation() {
302        let id1 = StepId::new();
303        let id2 = StepId::new();
304        assert_ne!(id1, id2, "StepIds should be unique");
305    }
306
307    #[test]
308    fn test_session_id_creation() {
309        let id1 = SessionId::new();
310        let id2 = SessionId::new();
311        assert_ne!(id1, id2, "SessionIds should be unique");
312    }
313
314    #[test]
315    fn test_message_id_creation() {
316        let id1 = MessageId::new();
317        let id2 = MessageId::new();
318        assert_ne!(id1, id2, "MessageIds should be unique");
319    }
320
321    #[test]
322    fn test_all_ids_from_uuid() {
323        let uuid = Uuid::new_v4();
324
325        let agent_id = AgentId::from(uuid);
326        assert_eq!(agent_id.as_uuid(), &uuid);
327
328        let guideline_id = GuidelineId::from(uuid);
329        assert_eq!(guideline_id.as_uuid(), &uuid);
330
331        let tool_id = ToolId::from(uuid);
332        assert_eq!(tool_id.as_uuid(), &uuid);
333
334        let journey_id = JourneyId::from(uuid);
335        assert_eq!(journey_id.as_uuid(), &uuid);
336
337        let step_id = StepId::from(uuid);
338        assert_eq!(step_id.as_uuid(), &uuid);
339
340        let session_id = SessionId::from(uuid);
341        assert_eq!(session_id.as_uuid(), &uuid);
342
343        let message_id = MessageId::from(uuid);
344        assert_eq!(message_id.as_uuid(), &uuid);
345    }
346}