use uuid::Uuid;
#[inline]
pub fn to_hex(u: Uuid) -> String {
u.simple().to_string()
}
#[inline]
pub fn to_hex_opt(u: Option<Uuid>) -> Option<String> {
u.map(to_hex)
}
#[inline]
pub fn from_hex(s: &str) -> Result<Uuid, uuid::Error> {
Uuid::parse_str(s)
}
#[inline]
pub fn from_hex_opt(s: Option<&str>) -> Result<Option<Uuid>, uuid::Error> {
match s {
Some(s) => from_hex(s).map(Some),
None => Ok(None),
}
}
#[cfg(test)]
#[allow(
clippy::unwrap_used,
clippy::expect_used,
reason = "test code — panics are acceptable failures"
)]
mod tests {
use super::*;
#[test]
fn round_trip() {
let u = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
let hex = to_hex(u);
assert_eq!(hex, "550e8400e29b41d4a716446655440000");
assert_eq!(hex.len(), 32);
assert_eq!(from_hex(&hex).unwrap(), u);
}
#[test]
fn from_hex_accepts_hyphenated() {
let u = from_hex("550e8400-e29b-41d4-a716-446655440000").unwrap();
assert_eq!(to_hex(u), "550e8400e29b41d4a716446655440000");
}
#[test]
fn option_round_trip() {
let u = Uuid::new_v4();
assert_eq!(
from_hex_opt(to_hex_opt(Some(u)).as_deref()).unwrap(),
Some(u)
);
assert_eq!(from_hex_opt(to_hex_opt(None).as_deref()).unwrap(), None);
}
}