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