From 66621bb8c71f38b06db240df55ddfddda7050326 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 24 Jun 2025 20:35:58 -0700 Subject: [PATCH] add tryfrom impls from uuid --- Cargo.toml | 2 +- src/uuid.rs | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 33cefe6..ec99a1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ uuid = { version = "1.17", default-features = false, optional = true } [dev-dependencies] divan = "0.1" -uuid = { version = "1", default-features = false, features = ["v7"] } +uuid = { version = "1", default-features = false, features = ["v7", "v4"] } julid-rs = { path = ".", features = ["uuid"] } [[bench]] diff --git a/src/uuid.rs b/src/uuid.rs index 78f396d..99ac09d 100644 --- a/src/uuid.rs +++ b/src/uuid.rs @@ -44,7 +44,21 @@ impl Julid { } } -#[derive(Debug)] +impl From for Uuid { + fn from(value: Julid) -> Self { + value.as_uuid() + } +} + +impl TryFrom for Julid { + type Error = UuidError; + + fn try_from(value: Uuid) -> Result { + Julid::from_uuid(value) + } +} + +#[derive(Debug, Clone, Copy, PartialEq)] pub enum UuidError { UnsupportedVersion(usize), UnsupportedVariant(uuid::Variant), @@ -64,7 +78,7 @@ impl fmt::Display for UuidError { #[cfg(test)] mod test { - use crate::Julid; + use crate::{uuid::UuidError, Julid}; #[test] fn into_uuid() { @@ -91,4 +105,11 @@ mod test { assert_eq!(ju1, ju2); assert_eq!(u1, u2); } + + #[test] + fn cant_even_from_uuid() { + let u = uuid::Uuid::new_v4(); + let jr: Result = u.try_into(); + assert_eq!(jr, Err(UuidError::UnsupportedVersion(4))); + } }