add tryfrom impls from uuid
This commit is contained in:
parent
dd6b28a039
commit
66621bb8c7
2 changed files with 24 additions and 3 deletions
|
@ -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]]
|
||||
|
|
25
src/uuid.rs
25
src/uuid.rs
|
@ -44,7 +44,21 @@ impl Julid {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
impl From<Julid> for Uuid {
|
||||
fn from(value: Julid) -> Self {
|
||||
value.as_uuid()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Uuid> for Julid {
|
||||
type Error = UuidError;
|
||||
|
||||
fn try_from(value: Uuid) -> Result<Self, Self::Error> {
|
||||
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<Julid, UuidError> = u.try_into();
|
||||
assert_eq!(jr, Err(UuidError::UnsupportedVersion(4)));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue