pjson_rs_domain/value_objects/
stream_id.rs

1//! Stream ID Value Object
2//!
3//! Pure domain object for stream identification.
4//! Serialization is handled in the application layer via DTOs.
5
6use std::fmt;
7use uuid::Uuid;
8
9/// Unique identifier for streams within a session
10///
11/// This is a pure domain object with no serialization concerns.
12/// For serialization, use `StreamIdDto` from the application layer.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
14pub struct StreamId(Uuid);
15
16impl StreamId {
17    /// Create new random stream ID
18    pub fn new() -> Self {
19        Self(Uuid::new_v4())
20    }
21
22    /// Create stream ID from UUID
23    pub fn from_uuid(uuid: Uuid) -> Self {
24        Self(uuid)
25    }
26
27    /// Create stream ID from string
28    pub fn from_string(s: &str) -> Result<Self, uuid::Error> {
29        Uuid::parse_str(s).map(Self)
30    }
31
32    /// Get underlying UUID
33    pub fn as_uuid(&self) -> Uuid {
34        self.0
35    }
36
37    /// Get string representation
38    pub fn as_str(&self) -> String {
39        self.0.to_string()
40    }
41}
42
43impl Default for StreamId {
44    fn default() -> Self {
45        Self::new()
46    }
47}
48
49impl fmt::Display for StreamId {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        write!(f, "{}", self.0)
52    }
53}
54
55impl From<Uuid> for StreamId {
56    fn from(uuid: Uuid) -> Self {
57        Self(uuid)
58    }
59}
60
61impl From<StreamId> for Uuid {
62    fn from(id: StreamId) -> Self {
63        id.0
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_stream_id_creation() {
73        let id1 = StreamId::new();
74        let id2 = StreamId::new();
75
76        assert_ne!(id1, id2);
77    }
78
79    #[test]
80    fn test_stream_id_from_string() {
81        let uuid_str = "550e8400-e29b-41d4-a716-446655440000";
82        let id = StreamId::from_string(uuid_str).unwrap();
83        assert_eq!(id.as_str(), uuid_str);
84    }
85}