Skip to main content

headless_lms_mcp/
ids.rs

1//! Server-side id assignment for new blocks and entities.
2
3#[cfg(test)]
4use std::sync::atomic::{AtomicU64, Ordering};
5
6use uuid::Uuid;
7
8/// Mints ids for everything an edit creates; agents never choose ids.
9pub trait IdGenerator: Send + Sync {
10    fn new_id(&self) -> Uuid;
11}
12
13/// Random v4 ids, for production.
14#[derive(Debug, Default, Clone, Copy)]
15pub struct RandomIds;
16
17impl IdGenerator for RandomIds {
18    fn new_id(&self) -> Uuid {
19        Uuid::new_v4()
20    }
21}
22
23/// Predictable ids (`00000000-0000-4000-8000-00000000000n`), for tests.
24#[cfg(test)]
25#[derive(Debug, Default)]
26pub struct SequentialIds {
27    issued: AtomicU64,
28}
29
30#[cfg(test)]
31impl SequentialIds {
32    /// The id the n:th call (1-based) to `new_id` returns.
33    pub fn nth(n: u64) -> Uuid {
34        Uuid::from_u64_pair(0x0000_0000_0000_4000, 0x8000_0000_0000_0000 | n)
35    }
36}
37
38#[cfg(test)]
39impl IdGenerator for SequentialIds {
40    fn new_id(&self) -> Uuid {
41        let n = self.issued.fetch_add(1, Ordering::Relaxed) + 1;
42        Self::nth(n)
43    }
44}