Skip to main content

continuity_buffer/
id.rs

1//! Identifier newtypes for buffers and undo groups.
2//!
3//! Both wrap UUIDv7 so that they sort by creation time naturally.
4
5use uuid::Uuid;
6
7/// A buffer's stable identifier.
8#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
9pub struct BufferId(Uuid);
10
11impl BufferId {
12    /// Allocate a new id.
13    #[must_use]
14    pub fn new() -> Self {
15        Self(Uuid::now_v7())
16    }
17
18    /// Wrap an existing UUID. Used by deserialization paths (persistence,
19    /// search index lookups) when the id was minted earlier.
20    #[must_use]
21    pub fn from_uuid(uuid: Uuid) -> Self {
22        Self(uuid)
23    }
24
25    /// Borrow the inner UUID for serialization.
26    #[must_use]
27    pub fn as_uuid(&self) -> Uuid {
28        self.0
29    }
30
31    /// All-zero sentinel value. Used by tab kinds that carry no
32    /// underlying buffer (e.g. the buffer-history visualization tab) —
33    /// the persist layer never assigns this id, so lookups against it
34    /// naturally return `None`.
35    #[must_use]
36    pub const fn nil() -> Self {
37        Self(Uuid::nil())
38    }
39
40    /// `true` when this id is the all-zero sentinel returned by
41    /// [`Self::nil`]. Useful for guards that need to short-circuit
42    /// before calling into the editor for a non-buffer surface.
43    #[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/// A window's stable identifier.
56///
57/// Wraps a UUIDv7 so cross-session restoration can match a persisted
58/// `windows` row to the spawned in-memory window.
59#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
60pub struct WindowId(Uuid);
61
62impl WindowId {
63    /// Allocate a new id.
64    #[must_use]
65    pub fn new() -> Self {
66        Self(Uuid::now_v7())
67    }
68
69    /// Wrap an existing UUID. Used by deserialization paths.
70    #[must_use]
71    pub fn from_uuid(uuid: Uuid) -> Self {
72        Self(uuid)
73    }
74
75    /// Borrow the inner UUID for serialization.
76    #[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/// An undo-group identifier.
89#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
90pub struct UndoGroupId(Uuid);
91
92impl UndoGroupId {
93    /// Allocate a new id.
94    #[must_use]
95    pub fn new() -> Self {
96        Self(Uuid::now_v7())
97    }
98
99    /// Wrap an existing UUID. Used by deserialization paths (persistence
100    /// recovery, edit-log round-trips).
101    #[must_use]
102    pub fn from_uuid(uuid: Uuid) -> Self {
103        Self(uuid)
104    }
105
106    /// Borrow the inner UUID for serialization.
107    #[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        // UUIDv7 is time-ordered; b was created later so a < b (almost always).
145        assert!(a < b || a.as_uuid().get_version_num() == 7);
146    }
147}