Skip to content

Commit

Permalink
add uuid conversion to rust
Browse files Browse the repository at this point in the history
  • Loading branch information
carderne committed Aug 20, 2024
1 parent 9eba613 commit 29c6fc0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions upid_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2021"

[dependencies]
rand = { version = "0.8" }
uuid = { version = "1", optional = true }

[lib]
name = "upid"
Expand Down
2 changes: 2 additions & 0 deletions upid_rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
// https://github.com/dylanhart/ulid-rs

mod b32;
#[cfg(feature = "uuid")]
mod uuid;

pub use crate::b32::{DecodeError, ENCODE};

Expand Down
30 changes: 30 additions & 0 deletions upid_rs/src/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! Convert between Upid and Uuid.

use crate::Upid;
use uuid::Uuid;

impl From<Uuid> for Upid {
fn from(uuid: Uuid) -> Self {
Upid(uuid.as_u128())
}
}

impl From<Upid> for Uuid {
fn from(upid: Upid) -> Self {
Uuid::from_u128(upid.0)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn uuid_cycle() {
let want = Upid::new("user");
let uuid: Uuid = want.into();
let got: Upid = uuid.into();

assert_eq!(got, want)
}
}

0 comments on commit 29c6fc0

Please sign in to comment.