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