summavy/core/
segment_id.rs1use std::cmp::{Ord, Ordering};
2use std::error::Error;
3use std::fmt;
4use std::str::FromStr;
5#[cfg(test)]
6use std::sync::atomic;
7
8#[cfg(test)]
9use once_cell::sync::Lazy;
10use serde::{Deserialize, Serialize};
11use uuid::Uuid;
12
13#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
22pub struct SegmentId(Uuid);
23
24#[cfg(test)]
25static AUTO_INC_COUNTER: Lazy<atomic::AtomicUsize> = Lazy::new(atomic::AtomicUsize::default);
26
27#[cfg(test)]
28const ZERO_ARRAY: [u8; 8] = [0u8; 8];
29
30#[cfg(test)]
36fn create_uuid() -> Uuid {
37 let new_auto_inc_id = (*AUTO_INC_COUNTER).fetch_add(1, atomic::Ordering::SeqCst);
38 Uuid::from_fields(new_auto_inc_id as u32, 0, 0, &ZERO_ARRAY)
39}
40
41#[cfg(not(test))]
42fn create_uuid() -> Uuid {
43 Uuid::new_v4()
44}
45
46impl SegmentId {
47 #[doc(hidden)]
48 pub fn generate_random() -> SegmentId {
49 SegmentId(create_uuid())
50 }
51
52 pub fn short_uuid_string(&self) -> String {
60 self.0.as_simple().to_string()[..8].to_string()
61 }
62
63 pub fn uuid_string(&self) -> String {
68 self.0.as_simple().to_string()
69 }
70
71 pub fn from_uuid_string(uuid_string: &str) -> Result<SegmentId, SegmentIdParseError> {
75 FromStr::from_str(uuid_string)
76 }
77}
78
79pub struct SegmentIdParseError(uuid::Error);
81
82impl Error for SegmentIdParseError {}
83
84impl fmt::Debug for SegmentIdParseError {
85 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86 self.0.fmt(f)
87 }
88}
89
90impl fmt::Display for SegmentIdParseError {
91 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
92 self.0.fmt(f)
93 }
94}
95
96impl FromStr for SegmentId {
97 type Err = SegmentIdParseError;
98
99 fn from_str(uuid_string: &str) -> Result<Self, SegmentIdParseError> {
100 let uuid = Uuid::parse_str(uuid_string).map_err(SegmentIdParseError)?;
101 Ok(SegmentId(uuid))
102 }
103}
104
105impl fmt::Debug for SegmentId {
106 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
107 write!(f, "Seg({:?})", self.short_uuid_string())
108 }
109}
110
111impl fmt::Display for SegmentId {
112 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113 write!(f, "Seg({:?})", self.short_uuid_string())
114 }
115}
116
117impl PartialOrd for SegmentId {
118 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
119 Some(self.cmp(other))
120 }
121}
122
123impl Ord for SegmentId {
124 fn cmp(&self, other: &Self) -> Ordering {
125 self.0.as_bytes().cmp(other.0.as_bytes())
126 }
127}
128
129#[cfg(test)]
130mod tests {
131 use super::SegmentId;
132
133 #[test]
134 fn test_to_uuid_string() {
135 let full_uuid = "a5c4dfcbdfe645089129e308e26d5523";
136 let segment_id = SegmentId::from_uuid_string(full_uuid).unwrap();
137 assert_eq!(segment_id.uuid_string(), full_uuid);
138 assert_eq!(segment_id.short_uuid_string(), "a5c4dfcb");
139 assert!(SegmentId::from_uuid_string("a5c4dfcbdfe645089129e308e26d5523b").is_err());
141 }
142}