1use std::sync::Arc;
4
5use serde::{Deserialize, Serialize};
6use uuid::Uuid;
7
8#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
10#[serde(transparent)]
11pub struct StoreId(
12 pub Uuid,
14);
15
16impl std::fmt::Display for StoreId {
17 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18 self.0.fmt(f)
19 }
20}
21
22#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
24#[serde(transparent)]
25pub struct CircleId(
26 pub Uuid,
28);
29
30impl std::fmt::Display for CircleId {
31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 self.0.fmt(f)
33 }
34}
35
36#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
38#[serde(transparent)]
39pub struct KeyId(
40 pub Uuid,
42);
43
44impl std::fmt::Display for KeyId {
45 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46 self.0.fmt(f)
47 }
48}
49
50#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
56#[serde(transparent)]
57pub struct MemberId(
58 pub Uuid,
60);
61
62impl std::fmt::Display for MemberId {
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 self.0.fmt(f)
65 }
66}
67
68impl std::str::FromStr for MemberId {
69 type Err = InvalidMemberId;
70 fn from_str(value: &str) -> Result<Self, Self::Err> {
73 canonical_uuid(value).map(Self).map_err(|_not_canonical| InvalidMemberId)
74 }
75}
76
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
79pub struct InvalidMemberId;
80
81impl std::fmt::Display for InvalidMemberId {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 f.write_str("not a canonical member id")
84 }
85}
86
87impl std::error::Error for InvalidMemberId {}
88
89pub fn canonical_uuid(text: &str) -> Result<Uuid, NotCanonicalUuid> {
92 match Uuid::try_parse(text) {
93 Ok(id) if id.hyphenated().to_string() == text => Ok(id),
94 _ => Err(NotCanonicalUuid),
95 }
96}
97
98#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
100#[error("not a canonical lowercase hyphenated UUID")]
101pub struct NotCanonicalUuid;
102
103#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
105#[serde(transparent)]
106pub struct InviteId(
107 pub Uuid,
109);
110
111impl std::fmt::Display for InviteId {
112 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 self.0.fmt(f)
114 }
115}
116
117#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
122#[serde(transparent)]
123pub struct FileId(
124 pub Uuid,
126);
127
128impl std::fmt::Display for FileId {
129 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
130 self.0.fmt(f)
131 }
132}
133
134#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
138#[serde(transparent)]
139pub struct ObjectId(
140 pub Uuid,
142);
143
144impl std::fmt::Display for ObjectId {
145 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
146 self.0.fmt(f)
147 }
148}
149
150#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
152#[serde(transparent)]
153pub struct DeviceId(
154 pub u64,
156);
157
158pub trait IdSource: Send + Sync {
160 fn new_id(&self) -> Uuid;
162
163 fn new_device_id(&self) -> DeviceId {
170 let (high, low) = self.new_id().as_u64_pair();
171 DeviceId((low & 0x3fff_ffff_ffff_ffff) | ((high & 3) << 62))
172 }
173}
174
175pub type IdSourceRef = Arc<dyn IdSource>;
177
178pub struct UuidIds;
180
181impl IdSource for UuidIds {
182 fn new_id(&self) -> Uuid {
183 Uuid::new_v4()
184 }
185}
186
187#[cfg(feature = "test-utils")]
192pub struct SequentialIds(std::sync::atomic::AtomicU64);
193
194#[cfg(feature = "test-utils")]
195impl SequentialIds {
196 pub fn new() -> Self {
198 Self(std::sync::atomic::AtomicU64::new(1))
199 }
200}
201
202#[cfg(feature = "test-utils")]
203impl IdSource for SequentialIds {
204 fn new_id(&self) -> Uuid {
205 use std::sync::atomic::Ordering;
206 let n = self
207 .0
208 .try_update(Ordering::Relaxed, Ordering::Relaxed, |n| n.checked_add(1))
209 .expect("sequential id source exhausted");
210 Uuid::from_u64_pair(0x7000 | (n >> 62), 0x8000_0000_0000_0000 | (n & 0x3fff_ffff_ffff_ffff))
212 }
213}
214
215#[cfg(test)]
216#[path = "id_source_tests.rs"]
217mod tests;