use crate::{Variant, UUID};
impl UUID {
#[must_use]
pub fn from_parts_dcom(
time_low: u32,
time_mid: u16,
time_hi_and_version: u16,
clock_seq: u16,
node: [u8; 6],
) -> Self {
let mut uuid = Self::nil();
uuid.bytes[0..4].copy_from_slice(&time_low.to_le_bytes());
uuid.bytes[4..6].copy_from_slice(&time_mid.to_le_bytes());
uuid.bytes[6..8].copy_from_slice(&time_hi_and_version.to_le_bytes());
uuid.bytes[8..10].copy_from_slice(&clock_seq.to_be_bytes());
uuid.bytes[10..16].copy_from_slice(&node);
uuid.with_variant(Variant::DCOM)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_dcom_from_parts_uuid() {
let uuid = UUID::from_parts_dcom(
0x1234_5678,
0x9ABC,
0xDEF0,
0x1234,
[0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF],
);
let expected_bytes = [
0x78, 0x56, 0x34, 0x12, 0xBC, 0x9A, 0xF0, 0xDE, 0xD2, 0x34, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, ];
assert_eq!(uuid.bytes, expected_bytes);
}
#[test]
fn test_dcom_variant() {
let uuid = UUID::from_parts_dcom(
0, 0, 0, 0, [0; 6], );
let variant_bits = (uuid.bytes[8] & 0xE0) >> 5;
assert_eq!(variant_bits, 0x06); }
#[test]
fn test_nil_uuid() {
let nil_uuid = UUID::nil();
assert_eq!(nil_uuid.bytes, [0; 16]);
}
#[test]
#[allow(clippy::unwrap_used)]
fn test_dcom_uuid_fields_preservation() {
let time_low = 0x1234_5678;
let time_mid = 0x9ABC;
let time_hi_and_version = 0xDEF0;
let clock_seq = 0x1234;
let node = [0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
let uuid = UUID::from_parts_dcom(time_low, time_mid, time_hi_and_version, clock_seq, node);
assert_eq!(
u32::from_le_bytes(uuid.bytes[0..4].try_into().unwrap()),
time_low
);
assert_eq!(
u16::from_le_bytes(uuid.bytes[4..6].try_into().unwrap()),
time_mid
);
assert_eq!(
u16::from_le_bytes(uuid.bytes[6..8].try_into().unwrap()),
time_hi_and_version
);
assert_eq!(uuid.bytes[10..16], node);
}
#[test]
fn test_dcom_uuid_variant_preservation() {
let uuid = UUID::from_parts_dcom(
0, 0, 0, 0xFFFF, [0; 6],
);
let variant_bits = (uuid.bytes[8] & 0xE0) >> 5;
assert_eq!(variant_bits, 0x06); }
#[test]
fn test_dcom_uuid_zero_values() {
let uuid = UUID::from_parts_dcom(0, 0, 0, 0, [0; 6]);
let mut expected = [0; 16];
expected[8] = 0xC0;
assert_eq!(uuid.bytes, expected);
}
#[test]
fn test_dcom_uuid_max_values() {
let uuid = UUID::from_parts_dcom(u32::MAX, u16::MAX, u16::MAX, u16::MAX, [0xFF; 6]);
let expected = [
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xDF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, ];
assert_eq!(uuid.bytes, expected);
}
#[test]
fn test_new_dcom_from_parts_valid_input() {
let time_low = 0x1234_5678;
let time_mid = 0xABCD;
let time_hi_and_version = 0x1FFF; let clock_seq = 0xEFFF;
let node = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB];
let uuid = UUID::from_parts_dcom(time_low, time_mid, time_hi_and_version, clock_seq, node);
let expected_bytes = [
0x78, 0x56, 0x34, 0x12, 0xCD, 0xAB, 0xFF, 0x1F, 0xCF, 0xFF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, ];
assert_eq!(uuid.bytes, expected_bytes);
}
#[test]
fn test_new_dcom_from_parts_zero_input() {
let time_low = 0;
let time_mid = 0;
let time_hi_and_version = 0;
let clock_seq = 0;
let node = [0; 6];
let uuid = UUID::from_parts_dcom(time_low, time_mid, time_hi_and_version, clock_seq, node);
assert_eq!(uuid, UUID::nil().with_variant(Variant::DCOM));
}
#[test]
fn test_new_dcom_from_parts_max_input() {
let time_low = u32::MAX;
let time_mid = u16::MAX;
let time_hi_and_version = u16::MAX;
let clock_seq = u16::MAX;
let node = [0xFF; 6];
let uuid = UUID::from_parts_dcom(time_low, time_mid, time_hi_and_version, clock_seq, node);
assert_eq!(uuid, UUID::max().with_variant(Variant::DCOM));
}
#[test]
fn test_new_dcom_from_parts_endianness() {
let time_low = 0x1122_3344;
let time_mid = 0x5566;
let time_hi_and_version = 0x7788;
let clock_seq = 0x99AA;
let node = [0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00];
let uuid = UUID::from_parts_dcom(time_low, time_mid, time_hi_and_version, clock_seq, node);
assert_eq!(uuid.bytes[0..4], [0x44, 0x33, 0x22, 0x11]); assert_eq!(uuid.bytes[4..6], [0x66, 0x55]); assert_eq!(uuid.bytes[6..8], [0x88, 0x77]); assert_eq!(uuid.bytes[8..10], [0xD9, 0xAA]); assert_eq!(uuid.bytes[10..16], [0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00]); }
}