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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//! # upid
//!
//! `upid` is the Rust implementation UPID, an alternative to UUID and ULID
//! that includes a useful prefix.
//!
//! The code below is derived from the following:
//! https://github.com/dylanhart/ulid-rs

mod b32;

pub use crate::b32::DecodeError;

use std::fmt;
use std::str::FromStr;
use std::time::{Duration, SystemTime};

use rand::Rng;

const VERSION: &str = "a";

fn now() -> std::time::SystemTime {
    std::time::SystemTime::now()
}

#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Hash, Clone, Copy)]
pub struct Upid(pub u128);

impl Upid {
    /// Creates a new Upid with the provided prefix and current time (UTC)
    ///
    /// The prefix should only contain lower-case latin alphabet characters.
    /// # Example
    /// ```rust
    /// use upid::Upid;
    ///
    /// let my_upid = Upid::new("user");
    /// ```
    pub fn new(prefix: &str) -> Upid {
        Upid::from_prefix(prefix)
    }

    /// Creates a Upid with the provided prefix and current time (UTC)
    ///
    /// The prefix should only contain lower-case latin alphabet characters.
    /// # Example
    /// ```rust
    /// use upid::Upid;
    ///
    /// let my_upid = Upid::from_prefix("user");
    /// ```
    pub fn from_prefix(prefix: &str) -> Upid {
        Upid::from_prefix_and_datetime(prefix, now())
    }

    /// Creates a new Upid with the given prefix and datetime
    ///
    /// The prefix should only contain lower-case latin alphabet characters.
    ///
    /// This will take the maximum of the `[SystemTime]` argument and `[SystemTime::UNIX_EPOCH]`
    /// as earlier times are not valid for a Upid timestamp
    ///
    /// # Example
    /// ```rust
    /// use std::time::{SystemTime, Duration};
    /// use upid::Upid;
    ///
    /// let upid = Upid::from_prefix_and_datetime("user", SystemTime::now());
    /// ```
    pub fn from_prefix_and_datetime(prefix: &str, datetime: SystemTime) -> Upid {
        let milliseconds = datetime
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap_or(Duration::ZERO)
            .as_millis();
        Upid::from_prefix_and_milliseconds(prefix, milliseconds)
    }

    /// Creates a new Upid with the given prefix and timestamp in millisecons
    ///
    /// The prefix should only contain lower-case latin alphabet characters.
    ///
    /// # Example
    /// ```rust
    /// use upid::Upid;
    ///
    /// let ms: u128 = 1720568902000;
    /// let upid = Upid::from_prefix_and_milliseconds("user", ms);
    /// ```
    pub fn from_prefix_and_milliseconds(prefix: &str, milliseconds: u128) -> Upid {
        // cut off the 8 lsb drops precision to 256 ms
        // future version could play with this differently
        // eg drop 4 bits on each side
        let time_bits = milliseconds >> 8;

        // get 64 bits of randomness on lsb side of a u128
        let mut source = rand::thread_rng();
        let random = source.gen::<u64>() as u128;

        // pad with 'z' if shorter than 4, cut to 4 if longer
        let prefix = format!("{:z<4}", prefix);
        let prefix: String = prefix.chars().take(4).collect();
        let prefix = format!("{}{}", prefix, VERSION);

        // decode_prefix Errors if the last character is past 'j' in the b32 alphabet
        // and we control that with the VERSION variable
        // If the prefix has characters from outside the alphabet, they will be wrapped into 'z's
        // And we have ensured above that it is exactly 5 characters long
        let p = b32::decode_prefix(prefix.as_bytes())
            .expect("decode_prefix failed with version character overflow");

        let res = (time_bits << 88)
            | (random << 24)
            | ((p[0] as u128) << 16)
            | ((p[1] as u128) << 8)
            | p[2] as u128;

        Upid(res)
    }

    /// Gets the datetime of when this Upid was created accurate to around 300ms
    ///
    /// # Example
    /// ```rust
    /// use std::time::{SystemTime, Duration};
    /// use upid::Upid;
    ///
    /// let dt = SystemTime::now();
    /// let upid = Upid::from_prefix_and_datetime("user", dt);
    ///
    /// assert!(dt + Duration::from_millis(300) >= upid.datetime());
    /// ```
    pub fn datetime(&self) -> SystemTime {
        let stamp = self.milliseconds();
        SystemTime::UNIX_EPOCH + Duration::from_millis(stamp)
    }

    /// Creates a Upid from a Base32 encoded string
    ///
    /// # Example
    /// ```rust
    /// use upid::Upid;
    ///
    /// let text = "user_aaccvpp5guht4dts56je5a";
    /// let result = Upid::from_string(text);
    ///
    /// assert_eq!(&result.unwrap().to_string(), text);
    /// ```
    pub fn from_string(encoded: &str) -> Result<Upid, DecodeError> {
        match b32::decode(encoded) {
            Ok(int_val) => Ok(Upid(int_val)),
            Err(err) => Err(err),
        }
    }

    /// Gets the prefix of this upid
    ///
    /// # Example
    /// ```rust
    /// use upid::Upid;
    ///
    /// let prefix = "user";
    /// let upid = Upid::from_prefix(prefix);
    ///
    /// assert_eq!(upid.prefix(), prefix);
    /// ```
    pub fn prefix(&self) -> String {
        let bytes: [u8; 16] = self.0.to_be_bytes();
        let (prefix, _) = b32::encode_prefix(&bytes[b32::END_RANDO_BIN..]);
        prefix
    }

    /// Gets the timestamp section of this upid
    ///
    /// # Example
    /// ```rust
    /// use upid::Upid;
    ///
    /// let ms: u128 = 1720568902000;
    /// let upid = Upid::from_prefix_and_milliseconds("user", ms);
    ///
    /// assert!(ms - u128::from(upid.milliseconds()) < 256);
    /// ```
    pub const fn milliseconds(&self) -> u64 {
        ((self.0 >> 88) << 8) as u64
    }

    /// Creates a Base32 encoded string that represents this Upid
    ///
    /// # Example
    /// ```rust
    /// use upid::Upid;
    ///
    /// let text = "user_aaccvpp5guht4dts56je5a";
    /// let upid = Upid::from_string(text).unwrap();
    ///
    /// assert_eq!(&upid.to_string(), text);
    /// ```
    #[allow(clippy::inherent_to_string_shadow_display)] // Significantly faster than Display::to_string
    pub fn to_string(&self) -> String {
        b32::encode(self.0)
    }

    /// Creates a Upid using the provided bytes array.
    ///
    /// # Example
    /// ```rust
    /// use upid::Upid;
    /// let bytes = [0xFF; 16];
    ///
    /// let upid = Upid::from_bytes(bytes);
    /// ```
    pub const fn from_bytes(bytes: [u8; 16]) -> Upid {
        Self(u128::from_be_bytes(bytes))
    }

    /// Returns the bytes of the Upid in big-endian order.
    ///
    /// # Example
    /// ```rust
    /// use upid::Upid;
    ///
    /// let text = "user_aaccvpp5guht4dts56je5a";
    /// let upid = Upid::from_string(text).unwrap();
    /// ```
    pub const fn to_bytes(&self) -> [u8; 16] {
        self.0.to_be_bytes()
    }
}

impl Default for Upid {
    fn default() -> Self {
        Upid::new("")
    }
}

impl From<Upid> for String {
    fn from(upid: Upid) -> String {
        upid.to_string()
    }
}

impl From<u128> for Upid {
    fn from(value: u128) -> Upid {
        Upid(value)
    }
}

impl From<Upid> for u128 {
    fn from(upid: Upid) -> u128 {
        upid.0
    }
}

impl FromStr for Upid {
    type Err = DecodeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Upid::from_string(s)
    }
}

impl fmt::Display for Upid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        write!(f, "{}", self.to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const EPS: u128 = 256;

    #[test]
    fn can_into_thing() {
        let want = Upid::from_str("user_aaccvpp5guht4dts56je5a").unwrap();
        let s: String = want.into();
        let u: u128 = want.into();

        assert_eq!(Upid::from_str(&s).unwrap(), want);
        assert_eq!(Upid::from(u), want);
    }

    #[test]
    fn can_display_things() {
        println!("{}", DecodeError::InvalidLength);
        println!("{}", DecodeError::InvalidChar);
    }

    #[test]
    fn test_dynamic() {
        let upid = Upid::new("user");
        let encoded = upid.to_string();
        let upid2 = Upid::from_string(&encoded).expect("failed to deserialize");
        assert_eq!(upid, upid2);
    }

    #[test]
    fn test_order() {
        let dt = SystemTime::now();
        let upid1 = Upid::from_prefix_and_datetime("user", dt);
        let upid2 = Upid::from_prefix_and_datetime("user", dt + Duration::from_millis(300));
        assert!(upid1 < upid2);
    }

    #[test]
    fn test_timestamp() {
        let dt = SystemTime::now();
        let want = dt
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap()
            .as_millis();
        let upid = Upid::from_prefix_and_milliseconds("user", want);
        let got = u128::from(upid.milliseconds());

        assert!(want - got < EPS);
    }

    #[test]
    fn test_datetime() {
        let dt = SystemTime::now();
        let upid = Upid::from_prefix_and_datetime("user", dt);

        assert!(upid.datetime() <= dt);
        assert!(upid.datetime() + Duration::from_millis(300) >= dt);
    }

    #[test]
    fn test_invalid_prefix() {
        // Invalid characters just become 'zzzz'
        let want = "zzzz";

        // even if too long
        let got = Upid::from_prefix("[0#/]]1,").prefix();
        assert_eq!(got, want);

        // or too short
        let got = Upid::from_prefix("[0").prefix();
        assert_eq!(got, want);
    }
}