Skip to main content

sim_lib_openai_server/
ids.rs

1use sim_kernel::{Error, Result};
2
3/// Generates sequential, prefixed gateway ids of the form `prefix_NNNNNN`.
4///
5/// Counting is deterministic: ids advance monotonically from a fixed start,
6/// which makes generated ids reproducible across runs.
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct GatewayIdGenerator {
9    prefix: String,
10    next: u64,
11}
12
13impl GatewayIdGenerator {
14    /// Builds a generator that emits ids with the given `prefix`, starting the
15    /// counter at `start`.
16    pub fn deterministic(prefix: impl Into<String>, start: u64) -> Self {
17        Self {
18            prefix: prefix.into(),
19            next: start,
20        }
21    }
22
23    /// Returns the next id and advances the counter, erroring if the counter
24    /// would overflow `u64`.
25    pub fn next_id(&mut self) -> Result<String> {
26        let value = self.next;
27        self.next = self
28            .next
29            .checked_add(1)
30            .ok_or_else(|| Error::Eval("gateway id counter overflow".to_owned()))?;
31        Ok(format!("{}_{value:06}", self.prefix))
32    }
33}