use std::collections::HashMap;
#[derive(Debug, thiserror::Error)]
pub enum UuidPersistenceError {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(String),
}
impl From<serde_json::Error> for UuidPersistenceError {
fn from(e: serde_json::Error) -> Self {
UuidPersistenceError::Serialization(e.to_string())
}
}
pub trait UuidPersistence: Send + Sync {
fn load(&self) -> Option<HashMap<String, String>>;
fn save(&self, mappings: &HashMap<String, String>) -> Result<(), UuidPersistenceError>;
}
#[derive(Debug, Clone, Default)]
pub struct NoOpUuidPersistence;
impl UuidPersistence for NoOpUuidPersistence {
fn load(&self) -> Option<HashMap<String, String>> {
None
}
fn save(&self, _mappings: &HashMap<String, String>) -> Result<(), UuidPersistenceError> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_noop_persistence() {
let storage = NoOpUuidPersistence;
assert!(storage.load().is_none());
let mappings = HashMap::new();
assert!(storage.save(&mappings).is_ok());
}
}