From 9c2cf66bba78b2b4265546b3a1c04f06644175c3 Mon Sep 17 00:00:00 2001 From: vados Date: Tue, 31 Oct 2023 02:08:29 +0900 Subject: [PATCH] feat(deps): remove vendored uuidv6 dep Now that the PR to rust-uuidv6 (https://github.com/jedisct1/rust-uuidv6/pull/1), it looks like we can remove the vendored code that added support for outputting bytes. This commit removes the vendored code and uses a published version of uuidv6. Signed-off-by: vados --- Cargo.lock | 10 +++ Cargo.toml | 1 + src/cuid.rs | 1 - src/cuid2.rs | 3 +- src/nanoid.rs | 2 +- src/uuid_v6.rs | 8 +-- src/uuid_v6/vendor.rs | 147 ------------------------------------------ 7 files changed, 15 insertions(+), 157 deletions(-) delete mode 100644 src/uuid_v6/vendor.rs diff --git a/Cargo.lock b/Cargo.lock index c4eee1b..b1ed83e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1329,6 +1329,7 @@ dependencies = [ "timeflake-rs", "ulid", "uuid7", + "uuidv6", "xid", ] @@ -2574,6 +2575,15 @@ dependencies = [ "rand_chacha", ] +[[package]] +name = "uuidv6" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e3f2328d4a62d25ebf0795824bb91b00bf7c1c668c368b02aa8768e48d30a8" +dependencies = [ + "getrandom", +] + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 4852499..23574fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ sonyflake = "0.2.0" timeflake-rs = "0.3.0" ulid = "1.1.0" uuid7 = "0.7.2" +uuidv6 = "0.1.2" xid = "1.0.3" miette = { version = "5.10.0", features = ["fancy"] } diff --git a/src/cuid.rs b/src/cuid.rs index b378140..f0a4285 100644 --- a/src/cuid.rs +++ b/src/cuid.rs @@ -1,7 +1,6 @@ use std::io; use chrono::{Datelike, NaiveDateTime, Timelike}; -use cuid; use pgrx::*; use crate::common::OrPgxError; diff --git a/src/cuid2.rs b/src/cuid2.rs index d9b61a1..f6a0736 100644 --- a/src/cuid2.rs +++ b/src/cuid2.rs @@ -1,4 +1,3 @@ -use cuid2; use pgrx::*; /// Generate a random cuid2 UUID @@ -28,4 +27,4 @@ mod tests { let generated = crate::cuid2::idkit_cuid2_generate(); assert_eq!(generated.len(), 24); } -} \ No newline at end of file +} diff --git a/src/nanoid.rs b/src/nanoid.rs index 007fc9b..790c7ab 100644 --- a/src/nanoid.rs +++ b/src/nanoid.rs @@ -1,5 +1,5 @@ -use pgrx::*; use nanoid::nanoid; +use pgrx::*; /// Generate a nanoid #[pg_extern] diff --git a/src/uuid_v6.rs b/src/uuid_v6.rs index cbca92d..c228d3b 100644 --- a/src/uuid_v6.rs +++ b/src/uuid_v6.rs @@ -1,7 +1,5 @@ use pgrx::*; - -mod vendor; -use vendor::{Node, UUIDv6}; +use uuidv6::{self, Node, RawUUIDv6, UUIDv6}; /// Generate a UUID v6 #[pg_extern] @@ -21,9 +19,7 @@ fn idkit_uuidv6_generate_text() -> String { fn idkit_uuidv6_generate_uuid() -> pgrx::Uuid { let node = Node::new(); - // This depends on PR to rust-uuidv6 - // see: https://github.com/jedisct1/rust-uuidv6/pull/1 - pgrx::Uuid::from_slice(&UUIDv6::new(&node).create_bytes()) + pgrx::Uuid::from_slice(&RawUUIDv6::new(&node).create()) .unwrap_or_else(|e| error!("{}", format!("failed to generate/parse uuidv6: {}", e))) } diff --git a/src/uuid_v6/vendor.rs b/src/uuid_v6/vendor.rs deleted file mode 100644 index 2194803..0000000 --- a/src/uuid_v6/vendor.rs +++ /dev/null @@ -1,147 +0,0 @@ -/// Note: this file was pulle from https://github.com/jedisct1/rust-uuidv6 @ v0.1.1 -/// With modifications made locally. -/// -/// PR: https://github.com/jedisct1/rust-uuidv6/pull/1 - -use std::time::{SystemTime, UNIX_EPOCH}; - -fn hex_format(out: &mut [u8], bin: &[u8]) { - const HEX_CHARS: &[u8; 16] = b"0123456789abcdef"; - let mut j = 0; - for b in bin { - out[j] = HEX_CHARS[(b >> 4) as usize]; - out[j + 1] = HEX_CHARS[(b & 0x0f) as usize]; - j += 2; - } -} - -/// A 6 bytes spatially unique identifier. -#[derive(Default, Debug, Copy, Clone, Hash, Eq, PartialEq, PartialOrd, Ord)] -pub struct Node { - node_id: [u8; 6], -} - -impl Node { - /// Create a random node identifier - pub fn new() -> Self { - let mut node_id = [0u8; 6]; - getrandom::getrandom(&mut node_id).unwrap(); - Node { node_id } - } - - /// Create a node identifier from a byte array - #[allow(dead_code)] - pub fn from_bytes(bytes: &[u8; 6]) -> Self { - Node { node_id: *bytes } - } - - /// Create a UUIDv6 base object - #[allow(dead_code)] - pub fn uuidv6(&self) -> UUIDv6 { - UUIDv6::new(self) - } -} - -#[derive(Default, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] -pub struct UUIDv6 { - ts: u64, - counter: u16, - initial_counter: u16, - node: Node, -} - -impl UUIDv6 { - /// Create a new UUIDv6 base object - pub fn new(node: &Node) -> UUIDv6 { - let ts = ((SystemTime::now() - .duration_since(UNIX_EPOCH) - .expect("Time went backwards") - .as_nanos() - .checked_add(1221929280000000) - .expect("Time is completely off")) - / 100) as u64; - let mut x = [0u8; 2]; - getrandom::getrandom(&mut x).unwrap(); - let initial_counter = u16::from_be_bytes(x); - UUIDv6 { - ts, - counter: initial_counter, - initial_counter, - node: *node, - } - } - - /// Return the next bytes UUIDv6 as bytes - pub fn create_bytes(&mut self) -> [u8; 16] { - let mut buf = [0u8; 16]; - let ts = self.ts; - buf[0..8].copy_from_slice(&(ts << 4).to_be_bytes()); - let x = (0x06u16 << 12) | (((buf[6] as u16) << 8 | buf[7] as u16) >> 4); - buf[6..8].copy_from_slice(&x.to_be_bytes()); - - buf[8..10].copy_from_slice(&self.counter.to_be_bytes()); - self.counter = self.counter.wrapping_add(1); - if self.counter == self.initial_counter { - *self = Self::new(&self.node); - }; - - buf[10..].copy_from_slice(&self.node.node_id); - - return buf; - } - - /// Return the next UUIDv6 string - pub fn create(&mut self) -> String { - let buf = self.create_bytes(); - let mut out = [0u8; 4 + 32]; - out[8] = b'-'; - out[13] = b'-'; - out[18] = b'-'; - out[23] = b'-'; - - hex_format(&mut out[0..], &buf[0..4]); - hex_format(&mut out[9..], &buf[4..6]); - hex_format(&mut out[14..], &buf[6..8]); - hex_format(&mut out[19..], &buf[8..10]); - hex_format(&mut out[24..], &buf[10..]); - - String::from_utf8_lossy(&out).into_owned() - } - -} - -#[derive(Default, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] -pub struct UUIDv6Iterator { - uuid: UUIDv6, -} - -impl Iterator for UUIDv6Iterator { - type Item = String; - - fn next(&mut self) -> Option { - Some(self.uuid.create()) - } -} - -impl IntoIterator for UUIDv6 { - type IntoIter = UUIDv6Iterator; - type Item = String; - - fn into_iter(self) -> Self::IntoIter { - UUIDv6Iterator { uuid: self } - } -} - -#[test] -fn test() { - let node = Node::new(); - - let mut st = node.uuidv6().into_iter(); - - let uid_1 = st.next(); - let uid_2 = st.next(); - let uid_3 = st.next(); - debug_assert_ne!(uid_1, uid_2); - debug_assert_ne!(uid_2, uid_3); - debug_assert_ne!(uid_3, uid_1); -}