use uuid::Uuid;
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct BufferId(Uuid);
impl BufferId {
#[must_use]
pub fn new() -> Self {
Self(Uuid::now_v7())
}
#[must_use]
pub fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
#[must_use]
pub fn as_uuid(&self) -> Uuid {
self.0
}
#[must_use]
pub const fn nil() -> Self {
Self(Uuid::nil())
}
#[must_use]
pub fn is_nil(&self) -> bool {
self.0.is_nil()
}
}
impl Default for BufferId {
fn default() -> Self {
Self::new()
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct WindowId(Uuid);
impl WindowId {
#[must_use]
pub fn new() -> Self {
Self(Uuid::now_v7())
}
#[must_use]
pub fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
#[must_use]
pub fn as_uuid(&self) -> Uuid {
self.0
}
}
impl Default for WindowId {
fn default() -> Self {
Self::new()
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct UndoGroupId(Uuid);
impl UndoGroupId {
#[must_use]
pub fn new() -> Self {
Self(Uuid::now_v7())
}
#[must_use]
pub fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
#[must_use]
pub fn as_uuid(&self) -> Uuid {
self.0
}
}
impl Default for UndoGroupId {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn buffer_ids_are_unique() {
let a = BufferId::new();
let b = BufferId::new();
assert_ne!(a, b);
}
#[test]
fn nil_is_all_zeros_and_distinct_from_fresh_ids() {
let n = BufferId::nil();
assert!(n.is_nil());
assert_eq!(n.as_uuid(), Uuid::nil());
let fresh = BufferId::new();
assert!(!fresh.is_nil());
assert_ne!(n, fresh);
}
#[test]
fn buffer_ids_sort_by_creation_order() {
let a = BufferId::new();
let b = BufferId::new();
assert!(a < b || a.as_uuid().get_version_num() == 7);
}
}