use std::borrow::Cow;
use std::fmt;
use std::str::FromStr;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use uuid::Uuid;
use crate::value::{Field, FieldError, FieldKind, Kind, Value};
const ALPHABET: &[u8; 32] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ";
const LEN: usize = 26;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Ulid(u128);
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("`{0}` is not a ULID (26 characters of Crockford base32)")]
pub struct UlidError(String);
impl Ulid {
pub fn new() -> Self {
let now = Utc::now().timestamp_millis();
let millis = u64::try_from(now).unwrap_or(0);
Self::from_parts(millis, random_80())
}
pub const fn nil() -> Self {
Ulid(0)
}
pub const fn from_parts(millis: u64, random: u128) -> Self {
let millis = (millis as u128) & ((1 << 48) - 1);
let random = random & ((1 << 80) - 1);
Ulid((millis << 80) | random)
}
pub const fn is_nil(&self) -> bool {
self.0 == 0
}
pub const fn timestamp_ms(&self) -> u64 {
(self.0 >> 80) as u64
}
pub fn datetime(&self) -> DateTime<Utc> {
let millis = i64::try_from(self.timestamp_ms()).unwrap_or(i64::MAX);
DateTime::from_timestamp_millis(millis).unwrap_or_default()
}
pub const fn to_u128(&self) -> u128 {
self.0
}
pub const fn from_u128(bits: u128) -> Self {
Ulid(bits)
}
pub fn to_uuid(&self) -> Uuid {
Uuid::from_u128(self.0)
}
}
fn random_80() -> u128 {
Uuid::new_v4().as_u128() & ((1 << 80) - 1)
}
impl fmt::Display for Ulid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut text = [0u8; LEN];
let mut bits = self.0;
for slot in text.iter_mut().rev() {
*slot = ALPHABET[(bits & 0x1f) as usize];
bits >>= 5;
}
f.write_str(std::str::from_utf8(&text).unwrap_or_default())
}
}
impl FromStr for Ulid {
type Err = UlidError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let invalid = || UlidError(s.to_owned());
if s.len() != LEN {
return Err(invalid());
}
let mut bits: u128 = 0;
for (i, c) in s.bytes().enumerate() {
let digit = decode_char(c).ok_or_else(invalid)?;
if i == 0 && digit > 7 {
return Err(invalid());
}
bits = (bits << 5) | u128::from(digit);
}
Ok(Ulid(bits))
}
}
fn decode_char(c: u8) -> Option<u8> {
let c = c.to_ascii_uppercase();
let c = match c {
b'I' | b'L' => b'1',
b'O' => b'0',
other => other,
};
ALPHABET
.iter()
.position(|&a| a == c)
.and_then(|i| u8::try_from(i).ok())
}
impl Serialize for Ulid {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(self)
}
}
impl<'de> Deserialize<'de> for Ulid {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let text = Cow::<str>::deserialize(deserializer)?;
text.parse().map_err(serde::de::Error::custom)
}
}
impl From<Ulid> for Value {
fn from(ulid: Ulid) -> Self {
Value::String(ulid.to_string())
}
}
impl Field for Ulid {
const KIND: FieldKind = FieldKind::Ulid;
fn to_value(&self) -> Value {
Value::from(*self)
}
fn null() -> Value {
Value::Null(Kind::String)
}
fn from_value(value: Value) -> Result<Self, FieldError> {
match value {
Value::Uuid(uuid) => Ok(Ulid(uuid.as_u128())),
other => String::from_value(other)?
.parse()
.map_err(|error: UlidError| FieldError::Invalid(error.to_string())),
}
}
fn type_name() -> Cow<'static, str> {
Cow::Borrowed("Ulid")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn known_vectors() {
assert_eq!(Ulid::nil().to_string(), "00000000000000000000000000");
assert_eq!(
Ulid::from_u128(u128::MAX).to_string(),
"7ZZZZZZZZZZZZZZZZZZZZZZZZZ"
);
let id = Ulid::from_parts(1_469_918_176_385, 0);
assert_eq!(&id.to_string()[..10], "01ARYZ6S41");
assert_eq!(id.timestamp_ms(), 1_469_918_176_385);
}
#[test]
fn parsing_is_case_insensitive_and_strict_about_length_and_range() {
let id = Ulid::new();
let text = id.to_string();
assert_eq!(text.to_lowercase().parse::<Ulid>().unwrap(), id);
assert!(
"8ZZZZZZZZZZZZZZZZZZZZZZZZZ".parse::<Ulid>().is_err(),
"overflows 128 bits"
);
assert!("0123".parse::<Ulid>().is_err());
assert!("0000000000000000000000000U".parse::<Ulid>().is_err());
assert_eq!(
"0000000000000000000000000L".parse::<Ulid>().unwrap(),
Ulid::from_u128(1)
);
}
#[test]
fn new_ulids_carry_the_time_and_differ() {
let before = Utc::now().timestamp_millis();
let a = Ulid::new();
let b = Ulid::new();
assert_ne!(a, b);
let ms = i64::try_from(a.timestamp_ms()).unwrap();
assert!(ms >= before && ms <= Utc::now().timestamp_millis());
assert!(!a.is_nil());
}
#[test]
fn serde_and_values() {
let id = Ulid::new();
let json = serde_json::to_string(&id).unwrap();
assert_eq!(json, format!("\"{id}\""));
assert_eq!(serde_json::from_str::<Ulid>(&json).unwrap(), id);
assert_eq!(id.to_value(), Value::String(id.to_string()));
assert_eq!(Ulid::from_value(id.to_value()).unwrap(), id);
assert_eq!(Ulid::from_value(Value::Uuid(id.to_uuid())).unwrap(), id);
}
}