irox_tools/util/
identifier.rs

1// SPDX-License-Identifier: MIT
2// Copyright ${YEAR} IROX Contributors
3//
4
5//!
6//! An Identifier represents a way to uniquely identify an item, whether as a [`String`], [`u64`],
7//! or [`UUID`].
8//!
9
10extern crate alloc;
11use crate::format;
12use crate::hash::murmur3_128;
13use crate::uuid::UUID;
14use alloc::string::{String, ToString};
15use core::fmt::{Display, Formatter};
16
17///
18/// Represents a way to uniquely identify an item.
19#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
20pub enum Identifier {
21    Integer(u64),
22    String(String),
23    UUID(UUID),
24}
25
26impl Identifier {
27    /// Represents this identifier as an [`u64`] type
28    #[must_use]
29    pub fn as_integer(&self) -> Identifier {
30        match self {
31            Identifier::Integer(i) => Identifier::Integer(*i),
32            Identifier::String(s) => {
33                let hash = murmur3_128(s.as_bytes());
34                Identifier::Integer(hash as u64)
35            }
36            Identifier::UUID(u) => {
37                let inner: u128 = u.into();
38                Identifier::Integer(inner as u64)
39            }
40        }
41    }
42
43    /// Represents this identifier as a [`String`] type
44    #[must_use]
45    pub fn as_string(&self) -> Identifier {
46        match self {
47            Identifier::Integer(i) => Identifier::String(format!("{i}")),
48            Identifier::String(s) => Identifier::String(s.to_string()),
49            Identifier::UUID(u) => Identifier::String(format!("{u}")),
50        }
51    }
52
53    /// Represents this identifier as a [`UUID`] type
54    #[must_use]
55    pub fn as_uuid(&self) -> Identifier {
56        match self {
57            Identifier::Integer(i) => {
58                let inner: u128 = *i as u128;
59                Identifier::UUID(inner.into())
60            }
61            Identifier::String(s) => {
62                let inner: u128 = murmur3_128(s);
63                Identifier::UUID(inner.into())
64            }
65            Identifier::UUID(u) => Identifier::UUID(*u),
66        }
67    }
68}
69
70impl Display for Identifier {
71    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
72        match self {
73            Identifier::Integer(i) => f.write_fmt(format_args!("{i}")),
74            Identifier::String(s) => f.write_fmt(format_args!("{s}")),
75            Identifier::UUID(u) => f.write_fmt(format_args!("{u}")),
76        }
77    }
78}
79
80impl From<u64> for Identifier {
81    fn from(value: u64) -> Self {
82        Identifier::Integer(value)
83    }
84}
85
86impl From<&u64> for Identifier {
87    fn from(value: &u64) -> Self {
88        Identifier::Integer(*value)
89    }
90}
91
92impl From<String> for Identifier {
93    fn from(value: String) -> Self {
94        Identifier::String(value)
95    }
96}
97
98impl From<&str> for Identifier {
99    fn from(value: &str) -> Self {
100        Identifier::String(value.to_string())
101    }
102}
103
104impl From<UUID> for Identifier {
105    fn from(value: UUID) -> Self {
106        Identifier::UUID(value)
107    }
108}
109
110impl From<&UUID> for Identifier {
111    fn from(value: &UUID) -> Self {
112        Identifier::UUID(*value)
113    }
114}