use uuid::Uuid;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RequestId(pub Uuid);
impl RequestId {
pub fn new() -> Self {
Self(Uuid::now_v7())
}
pub fn to_string_hex(&self) -> String {
self.0.simple().to_string()
}
}
impl Default for RequestId {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Display for RequestId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_string_hex())
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct RequestIdLayer;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn requests_are_unique() {
let a = RequestId::new();
let b = RequestId::new();
assert_ne!(a, b);
}
#[test]
fn to_string_hex_is_simple_form() {
let r = RequestId::new();
let s = r.to_string_hex();
assert_eq!(s.len(), 32);
assert!(s
.chars()
.all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase()));
}
}