1#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2pub struct StorageId(String);
3
4impl From<uuid::Uuid> for StorageId {
5    fn from(uuid: uuid::Uuid) -> Self {
6        StorageId(uuid.to_string())
7    }
8}
9
10impl From<String> for StorageId {
11    fn from(s: String) -> Self {
12        StorageId(s)
13    }
14}
15
16impl From<&str> for StorageId {
17    fn from(s: &str) -> Self {
18        StorageId(s.to_string())
19    }
20}
21
22impl std::fmt::Display for StorageId {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        write!(f, "{}", self.0)
25    }
26}
27
28impl StorageId {
29    pub fn new<R: rand::Rng>(rng: &mut R) -> Self {
30        let bytes = rng.random();
31        let uuid = uuid::Builder::from_random_bytes(bytes).into_uuid();
32        StorageId(uuid.to_string())
33    }
34
35    pub(crate) fn as_str(&self) -> &str {
36        &self.0
37    }
38}
39
40#[derive(Debug, Clone, PartialEq, Eq)]
41pub enum StorageIdError {
42    InvalidFormat,
43}
44
45impl std::fmt::Display for StorageIdError {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        match self {
48            StorageIdError::InvalidFormat => write!(f, "Invalid storage ID format"),
49        }
50    }
51}
52
53impl std::error::Error for StorageIdError {}