mirror of
https://github.com/uuid-rs/uuid.git
synced 2025-10-02 15:24:57 +00:00
add a get_node_id method for v1 and v6 UUIDs
This commit is contained in:
parent
0f2aaaeab9
commit
17d592afb7
2
src/external/serde_support.rs
vendored
2
src/external/serde_support.rs
vendored
@ -184,7 +184,7 @@ pub mod compact {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use serde_derive::*;
|
use serde_derive::*;
|
||||||
use serde_test::{self, Configure};
|
use serde_test::Configure;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_compact() {
|
fn test_serialize_compact() {
|
||||||
|
2
src/external/slog_support.rs
vendored
2
src/external/slog_support.rs
vendored
@ -26,7 +26,7 @@ impl slog::Value for Uuid {
|
|||||||
mod tests {
|
mod tests {
|
||||||
use crate::tests::new;
|
use crate::tests::new;
|
||||||
|
|
||||||
use slog::{self, crit, Drain};
|
use slog::{crit, Drain};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_slog_kv() {
|
fn test_slog_kv() {
|
||||||
|
71
src/lib.rs
71
src/lib.rs
@ -877,24 +877,14 @@ impl Uuid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// If the UUID is the correct version (v1, v6, or v7) this will return
|
/// If the UUID is the correct version (v1, v6, or v7) this will return
|
||||||
/// the timestamp and counter portion parsed from a V1 UUID.
|
/// the timestamp in a version-agnostic [`Timestamp`]. For other versions
|
||||||
///
|
/// this will return `None`.
|
||||||
/// Returns `None` if the supplied UUID is not V1.
|
|
||||||
///
|
|
||||||
/// The V1 timestamp format defined in RFC4122 specifies a 60-bit
|
|
||||||
/// integer representing the number of 100-nanosecond intervals
|
|
||||||
/// since 00:00:00.00, 15 Oct 1582.
|
|
||||||
///
|
|
||||||
/// [`Timestamp`] offers several options for converting the raw RFC4122
|
|
||||||
/// value into more commonly-used formats, such as a unix timestamp.
|
|
||||||
///
|
///
|
||||||
/// # Roundtripping
|
/// # Roundtripping
|
||||||
///
|
///
|
||||||
/// This method is unlikely to roundtrip a timestamp in a UUID due to the way
|
/// This method is unlikely to roundtrip a timestamp in a UUID due to the way
|
||||||
/// UUIDs encode timestamps. The timestamp returned from this method will be truncated to
|
/// UUIDs encode timestamps. The timestamp returned from this method will be truncated to
|
||||||
/// 100ns precision for version 1 and 6 UUIDs, and to millisecond precision for version 7 UUIDs.
|
/// 100ns precision for version 1 and 6 UUIDs, and to millisecond precision for version 7 UUIDs.
|
||||||
///
|
|
||||||
/// [`Timestamp`]: v1/struct.Timestamp.html
|
|
||||||
pub const fn get_timestamp(&self) -> Option<Timestamp> {
|
pub const fn get_timestamp(&self) -> Option<Timestamp> {
|
||||||
match self.get_version() {
|
match self.get_version() {
|
||||||
Some(Version::Mac) => {
|
Some(Version::Mac) => {
|
||||||
@ -923,6 +913,26 @@ impl Uuid {
|
|||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If the UUID is the correct version (v1, or v6) this will return the
|
||||||
|
/// node value as a 6-byte array. For other versions this will return `None`.
|
||||||
|
pub const fn get_node_id(&self) -> Option<[u8; 6]> {
|
||||||
|
match self.get_version() {
|
||||||
|
Some(Version::Mac) | Some(Version::SortMac) => {
|
||||||
|
let mut node_id = [0; 6];
|
||||||
|
|
||||||
|
node_id[0] = self.0[10];
|
||||||
|
node_id[1] = self.0[11];
|
||||||
|
node_id[2] = self.0[12];
|
||||||
|
node_id[3] = self.0[13];
|
||||||
|
node_id[4] = self.0[14];
|
||||||
|
node_id[5] = self.0[15];
|
||||||
|
|
||||||
|
Some(node_id)
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Uuid {
|
impl Default for Uuid {
|
||||||
@ -1251,6 +1261,43 @@ mod tests {
|
|||||||
assert_eq!(uuid.get_version_num(), 3);
|
assert_eq!(uuid.get_version_num(), 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg_attr(
|
||||||
|
all(
|
||||||
|
target_arch = "wasm32",
|
||||||
|
target_vendor = "unknown",
|
||||||
|
target_os = "unknown"
|
||||||
|
),
|
||||||
|
wasm_bindgen_test
|
||||||
|
)]
|
||||||
|
fn test_get_timestamp_unsupported_version() {
|
||||||
|
let uuid = new();
|
||||||
|
|
||||||
|
assert_ne!(Version::Mac, uuid.get_version().unwrap());
|
||||||
|
assert_ne!(Version::SortMac, uuid.get_version().unwrap());
|
||||||
|
assert_ne!(Version::SortRand, uuid.get_version().unwrap());
|
||||||
|
|
||||||
|
assert!(uuid.get_timestamp().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg_attr(
|
||||||
|
all(
|
||||||
|
target_arch = "wasm32",
|
||||||
|
target_vendor = "unknown",
|
||||||
|
target_os = "unknown"
|
||||||
|
),
|
||||||
|
wasm_bindgen_test
|
||||||
|
)]
|
||||||
|
fn test_get_node_id_unsupported_version() {
|
||||||
|
let uuid = new();
|
||||||
|
|
||||||
|
assert_ne!(Version::Mac, uuid.get_version().unwrap());
|
||||||
|
assert_ne!(Version::SortMac, uuid.get_version().unwrap());
|
||||||
|
|
||||||
|
assert!(uuid.get_node_id().is_none());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
all(
|
all(
|
||||||
|
@ -151,6 +151,7 @@ const fn try_parse(input: &[u8]) -> Result<[u8; 16], InvalidUuid> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) const fn parse_braced(input: &[u8]) -> Result<[u8; 16], InvalidUuid> {
|
pub(crate) const fn parse_braced(input: &[u8]) -> Result<[u8; 16], InvalidUuid> {
|
||||||
if let (38, [b'{', s @ .., b'}']) = (input.len(), input) {
|
if let (38, [b'{', s @ .., b'}']) = (input.len(), input) {
|
||||||
parse_hyphenated(s)
|
parse_hyphenated(s)
|
||||||
@ -160,6 +161,7 @@ pub(crate) const fn parse_braced(input: &[u8]) -> Result<[u8; 16], InvalidUuid>
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub(crate) const fn parse_urn(input: &[u8]) -> Result<[u8; 16], InvalidUuid> {
|
pub(crate) const fn parse_urn(input: &[u8]) -> Result<[u8; 16], InvalidUuid> {
|
||||||
if let (45, [b'u', b'r', b'n', b':', b'u', b'u', b'i', b'd', b':', s @ ..]) =
|
if let (45, [b'u', b'r', b'n', b':', b'u', b'u', b'i', b'd', b':', s @ ..]) =
|
||||||
(input.len(), input)
|
(input.len(), input)
|
||||||
|
@ -135,6 +135,8 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(ts.0 - 0x01B2_1DD2_1381_4000, 14_968_545_358_129_460);
|
assert_eq!(ts.0 - 0x01B2_1DD2_1381_4000, 14_968_545_358_129_460);
|
||||||
|
|
||||||
|
assert_eq!(Some(node), uuid.get_node_id(),);
|
||||||
|
|
||||||
// Ensure parsing the same UUID produces the same timestamp
|
// Ensure parsing the same UUID produces the same timestamp
|
||||||
let parsed = Uuid::parse_str("20616934-4ba2-11e7-8000-010203040506").unwrap();
|
let parsed = Uuid::parse_str("20616934-4ba2-11e7-8000-010203040506").unwrap();
|
||||||
|
|
||||||
@ -142,6 +144,8 @@ mod tests {
|
|||||||
uuid.get_timestamp().unwrap(),
|
uuid.get_timestamp().unwrap(),
|
||||||
parsed.get_timestamp().unwrap()
|
parsed.get_timestamp().unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert_eq!(uuid.get_node_id().unwrap(), parsed.get_node_id().unwrap(),);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -137,6 +137,8 @@ mod tests {
|
|||||||
|
|
||||||
assert_eq!(ts.0 - 0x01B2_1DD2_1381_4000, 14_968_545_358_129_460);
|
assert_eq!(ts.0 - 0x01B2_1DD2_1381_4000, 14_968_545_358_129_460);
|
||||||
|
|
||||||
|
assert_eq!(Some(node), uuid.get_node_id(),);
|
||||||
|
|
||||||
// Ensure parsing the same UUID produces the same timestamp
|
// Ensure parsing the same UUID produces the same timestamp
|
||||||
let parsed = Uuid::parse_str("1e74ba22-0616-6934-8000-010203040506").unwrap();
|
let parsed = Uuid::parse_str("1e74ba22-0616-6934-8000-010203040506").unwrap();
|
||||||
|
|
||||||
@ -144,6 +146,8 @@ mod tests {
|
|||||||
uuid.get_timestamp().unwrap(),
|
uuid.get_timestamp().unwrap(),
|
||||||
parsed.get_timestamp().unwrap()
|
parsed.get_timestamp().unwrap()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
assert_eq!(uuid.get_node_id().unwrap(), parsed.get_node_id().unwrap(),);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
use uuid::{uuid, Uuid};
|
use uuid::{uuid, Uuid};
|
||||||
|
|
||||||
fn Ok() {}
|
fn Ok() {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user