Skip to main content

evault_core/traits/
id_gen.rs

1//! [`IdGenerator`] — abstraction over UUID/identifier creation.
2//!
3//! Like [`Clock`](super::Clock), this exists so tests can swap in a
4//! deterministic generator and inspect generated ids without consulting
5//! globals.
6
7use uuid::Uuid;
8
9/// Source of fresh UUIDs.
10pub trait IdGenerator: Send + Sync {
11    /// Produce a fresh identifier.
12    fn next(&self) -> Uuid;
13}
14
15/// Production [`IdGenerator`] that returns version-4 UUIDs.
16#[derive(Debug, Default, Clone, Copy)]
17pub struct UuidV4IdGenerator;
18
19impl IdGenerator for UuidV4IdGenerator {
20    fn next(&self) -> Uuid {
21        Uuid::new_v4()
22    }
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28
29    #[test]
30    fn uuid_v4_generator_returns_unique_ids() {
31        let g = UuidV4IdGenerator;
32        let a = g.next();
33        let b = g.next();
34        assert_ne!(a, b);
35    }
36}