payload_offloading_for_aws/offload/
id_provider.rs1use std::sync::Mutex;
2
3use uuid::Uuid;
4
5pub trait IdProvider {
6 fn generate(&self) -> Result<String, String>;
7}
8
9#[derive(Debug)]
10pub struct FixedIdsProvider {
11 pub ids: Mutex<Vec<String>>,
12}
13
14impl FixedIdsProvider {
15 pub fn new(ids: Vec<&str>) -> Self {
16 Self {
17 ids: Mutex::new(ids.into_iter().map(|i| i.to_owned()).collect()),
18 }
19 }
20}
21
22impl IdProvider for FixedIdsProvider {
23 fn generate(&self) -> Result<String, String> {
24 let mut locked = self
25 .ids
26 .lock()
27 .map_err(|e| format!("lock ids failed: {e}"))?;
28
29 if !locked.is_empty() {
30 let taken = locked.remove(0);
32 locked.push(taken.clone());
33 Ok(taken)
34 } else {
35 Err("FixedIdsProvider was exhausted".to_owned())
36 }
37 }
38}
39
40#[derive(Debug, Default)]
41pub struct RandomUuidProvider {}
42
43impl IdProvider for RandomUuidProvider {
44 fn generate(&self) -> Result<String, String> {
45 Ok(Uuid::new_v4().to_string())
46 }
47}