1use std::fmt::Write;
9
10use crate::bytes::{le_u16, le_u32};
11
12#[must_use]
14pub fn format_guid(raw: &[u8; 16]) -> String {
15 let d1 = le_u32(raw, 0);
16 let d2 = le_u16(raw, 4);
17 let d3 = le_u16(raw, 6);
18 let mut tail = String::with_capacity(20);
19 for (i, b) in raw[8..16].iter().enumerate() {
20 if i == 2 {
21 tail.push('-');
22 }
23 let _ = write!(tail, "{b:02x}");
25 }
26 format!("{d1:08x}-{d2:04x}-{d3:04x}-{tail}")
27}
28
29#[cfg(test)]
30mod tests {
31 use super::*;
32
33 #[test]
34 fn formats_mixed_endian_guid() {
35 let raw = [
38 0x3b, 0xd6, 0x67, 0x49, 0x29, 0x2e, 0xd8, 0x4a, 0x83, 0x99, 0xf6, 0xa3, 0x39, 0xe3,
39 0xd0, 0x01,
40 ];
41 assert_eq!(format_guid(&raw), "4967d63b-2e29-4ad8-8399-f6a339e3d001");
42 }
43
44 #[test]
45 fn formats_zero_guid() {
46 assert_eq!(
47 format_guid(&[0u8; 16]),
48 "00000000-0000-0000-0000-000000000000"
49 );
50 }
51}