1use uuid::Uuid;
6
7#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
9pub struct BufferId(Uuid);
10
11impl BufferId {
12 #[must_use]
14 pub fn new() -> Self {
15 Self(Uuid::now_v7())
16 }
17
18 #[must_use]
21 pub fn from_uuid(uuid: Uuid) -> Self {
22 Self(uuid)
23 }
24
25 #[must_use]
27 pub fn as_uuid(&self) -> Uuid {
28 self.0
29 }
30
31 #[must_use]
36 pub const fn nil() -> Self {
37 Self(Uuid::nil())
38 }
39
40 #[must_use]
44 pub fn is_nil(&self) -> bool {
45 self.0.is_nil()
46 }
47}
48
49impl Default for BufferId {
50 fn default() -> Self {
51 Self::new()
52 }
53}
54
55#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
60pub struct WindowId(Uuid);
61
62impl WindowId {
63 #[must_use]
65 pub fn new() -> Self {
66 Self(Uuid::now_v7())
67 }
68
69 #[must_use]
71 pub fn from_uuid(uuid: Uuid) -> Self {
72 Self(uuid)
73 }
74
75 #[must_use]
77 pub fn as_uuid(&self) -> Uuid {
78 self.0
79 }
80}
81
82impl Default for WindowId {
83 fn default() -> Self {
84 Self::new()
85 }
86}
87
88#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
90pub struct UndoGroupId(Uuid);
91
92impl UndoGroupId {
93 #[must_use]
95 pub fn new() -> Self {
96 Self(Uuid::now_v7())
97 }
98
99 #[must_use]
102 pub fn from_uuid(uuid: Uuid) -> Self {
103 Self(uuid)
104 }
105
106 #[must_use]
108 pub fn as_uuid(&self) -> Uuid {
109 self.0
110 }
111}
112
113impl Default for UndoGroupId {
114 fn default() -> Self {
115 Self::new()
116 }
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 #[test]
124 fn buffer_ids_are_unique() {
125 let a = BufferId::new();
126 let b = BufferId::new();
127 assert_ne!(a, b);
128 }
129
130 #[test]
131 fn nil_is_all_zeros_and_distinct_from_fresh_ids() {
132 let n = BufferId::nil();
133 assert!(n.is_nil());
134 assert_eq!(n.as_uuid(), Uuid::nil());
135 let fresh = BufferId::new();
136 assert!(!fresh.is_nil());
137 assert_ne!(n, fresh);
138 }
139
140 #[test]
141 fn buffer_ids_sort_by_creation_order() {
142 let a = BufferId::new();
143 let b = BufferId::new();
144 assert!(a < b || a.as_uuid().get_version_num() == 7);
146 }
147}