1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::sync::atomic::{AtomicU64, Ordering};
pub type AgentId = String;
pub type ThreadId = String;
pub type RunId = String;
pub type FileId = String;
#[derive(Debug)]
pub struct IdGen {
prefix: String,
counter: AtomicU64,
}
impl IdGen {
pub fn new(prefix: impl Into<String>) -> Self {
Self {
prefix: prefix.into(),
counter: AtomicU64::new(0),
}
}
pub fn next(&self, kind: &str) -> String {
let n = self.counter.fetch_add(1, Ordering::Relaxed);
format!("{}-{}-{}", self.prefix, kind, n)
}
pub fn next_agent(&self) -> AgentId {
self.next("agent")
}
pub fn next_thread(&self) -> ThreadId {
self.next("thread")
}
pub fn next_run(&self) -> RunId {
self.next("run")
}
pub fn next_file(&self) -> FileId {
self.next("file")
}
}