ps_uuid/methods/from_parts_ncs.rs
1use crate::UUID;
2
3impl UUID {
4 /// Constructs a new NCS UUID (Variant 0) from pre-computed timestamp bytes, address family, and address.
5 ///
6 /// # Parameters
7 /// - `timestamp`: 6-byte timestamp in 4-microsecond units since 1980-01-01 00:00 UTC (big-endian).
8 /// - `address_family`: Network address family (0-13).
9 /// - `address`: 7-byte node ID (e.g., extended MAC address or unique host identifier).
10 ///
11 /// # NCS UUID Structure
12 /// - Timestamp (48 bits): Raw timestamp bytes in 4-microsecond units since 1980-01-01 00:00 UTC.
13 /// - Reserved (16 bits): Set to 0.
14 /// - Address Family (8 bits): Network type identifier.
15 /// - Node ID (56 bits): Unique host identifier.
16 ///
17 /// # Returns
18 /// A new NCS UUID with the specified components.
19 ///
20 /// # Example
21 /// ```rust
22 /// let timestamp = [93, 16, 39, 53, 62, 58]; // Pre-computed timestamp bytes
23 /// let address = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
24 /// let uuid = ps_uuid::UUID::from_parts_ncs(×tamp, 2, &address);
25 /// ```
26 #[must_use]
27 pub fn from_parts_ncs(timestamp: &[u8; 6], address_family: u8, address: &[u8; 7]) -> Self {
28 let mut uuid = Self::nil();
29
30 uuid.bytes[0..6].copy_from_slice(timestamp);
31
32 // Set address family (byte 8)
33 uuid.bytes[8] = address_family;
34
35 // Set node ID (bytes 9–15)
36 uuid.bytes[9..16].copy_from_slice(address);
37
38 uuid.with_variant(crate::Variant::NCS)
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn test_valid_ncs_uuid() {
48 let timestamp = &[93, 16, 39, 53, 62, 58];
49 let address = [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
50 let uuid = UUID::from_parts_ncs(timestamp, 2, &address);
51 let bytes = uuid.as_bytes();
52
53 assert_eq!(&bytes[0..6], timestamp);
54 // Check reserved bits
55 assert_eq!(&bytes[6..8], &[0, 0]);
56 // Check address family and variant
57 assert_eq!(bytes[8], 2);
58 // Check node ID
59 assert_eq!(&bytes[9..16], &address);
60 }
61
62 #[test]
63 fn test_nil_uuid() {
64 let ncs_nil = UUID::from_parts_ncs(&[0, 0, 0, 0, 0, 0], 0, &[0, 0, 0, 0, 0, 0, 0]);
65 let nil = UUID::nil();
66
67 assert_eq!(ncs_nil, nil, "UUIDs should be equal.");
68 }
69}