Skip to main content

vibe_ticket/core/
id.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3use uuid::Uuid;
4
5/// Macro to generate ID types backed by UUID
6macro_rules! define_id_type {
7    (
8        $(#[$meta:meta])*
9        $name:ident
10    ) => {
11        $(#[$meta])*
12        #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
13        #[serde(transparent)]
14        pub struct $name(Uuid);
15
16        impl $name {
17            /// Creates a new random ID
18            #[must_use] pub fn new() -> Self {
19                Self(Uuid::new_v4())
20            }
21
22            /// Creates an ID from a UUID
23            #[must_use] pub const fn from_uuid(uuid: Uuid) -> Self {
24                Self(uuid)
25            }
26
27            /// Returns the inner UUID
28            #[must_use] pub const fn as_uuid(&self) -> &Uuid {
29                &self.0
30            }
31
32            /// Parses an ID from a string
33            pub fn parse_str(input: &str) -> Result<Self, uuid::Error> {
34                Ok(Self(Uuid::parse_str(input)?))
35            }
36
37            /// Returns a shortened version of the ID for display
38            #[must_use] pub fn short(&self) -> String {
39                self.0.to_string()[..8].to_string()
40            }
41        }
42
43        impl Default for $name {
44            fn default() -> Self {
45                Self::new()
46            }
47        }
48
49        impl fmt::Display for $name {
50            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51                write!(f, "{}", self.0)
52            }
53        }
54
55        impl From<Uuid> for $name {
56            fn from(uuid: Uuid) -> Self {
57                Self(uuid)
58            }
59        }
60
61        impl AsRef<Uuid> for $name {
62            fn as_ref(&self) -> &Uuid {
63                &self.0
64            }
65        }
66    };
67}
68
69// Define the ID types
70define_id_type! {
71    /// Unique identifier for a ticket
72    ///
73    /// Uses UUID v4 internally for globally unique identification
74    TicketId
75}
76
77define_id_type! {
78    /// Unique identifier for a task
79    ///
80    /// Uses UUID v4 internally for globally unique identification
81    TaskId
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    mod ticket_id_tests {
89        use super::*;
90
91        #[test]
92        fn test_creation() {
93            let id1 = TicketId::new();
94            let id2 = TicketId::new();
95            assert_ne!(id1, id2);
96        }
97
98        #[test]
99        fn test_parsing() {
100            let id = TicketId::new();
101            let id_str = id.to_string();
102            let parsed = TicketId::parse_str(&id_str).unwrap();
103            assert_eq!(id, parsed);
104        }
105
106        #[test]
107        fn test_short() {
108            let id = TicketId::new();
109            let short = id.short();
110            assert_eq!(short.len(), 8);
111            assert!(id.to_string().starts_with(&short));
112        }
113
114        #[test]
115        fn test_from_uuid() {
116            let uuid = Uuid::new_v4();
117            let id = TicketId::from_uuid(uuid);
118            assert_eq!(id.as_uuid(), &uuid);
119        }
120
121        #[test]
122        fn test_default() {
123            let id1 = TicketId::default();
124            let id2 = TicketId::default();
125            assert_ne!(id1, id2);
126        }
127    }
128
129    mod task_id_tests {
130        use super::*;
131
132        #[test]
133        fn test_creation() {
134            let id1 = TaskId::new();
135            let id2 = TaskId::new();
136            assert_ne!(id1, id2);
137        }
138
139        #[test]
140        fn test_parsing() {
141            let id = TaskId::new();
142            let id_str = id.to_string();
143            let parsed = TaskId::parse_str(&id_str).unwrap();
144            assert_eq!(id, parsed);
145        }
146
147        #[test]
148        fn test_short() {
149            let id = TaskId::new();
150            let short = id.short();
151            assert_eq!(short.len(), 8);
152            assert!(id.to_string().starts_with(&short));
153        }
154
155        #[test]
156        fn test_from_uuid() {
157            let uuid = Uuid::new_v4();
158            let id = TaskId::from_uuid(uuid);
159            assert_eq!(id.as_uuid(), &uuid);
160        }
161
162        #[test]
163        fn test_default() {
164            let id1 = TaskId::default();
165            let id2 = TaskId::default();
166            assert_ne!(id1, id2);
167        }
168    }
169}