1use std::mem;
2pub use windows_core::GUID;
34/// Extensions for [`GUID`].
5pub trait GuidExt {
6/// Formats the GUID as a simple hex string.
7fn format_simple(&self) -> String;
89/// Formats the GUID as a hyphenated hex string.
10fn format_hyphenated(&self) -> String;
1112/// Returns the contained GUID **misinterpreted** as raw bytes.
13 ///
14 /// Some GW2 community projects misinterpret the memory layout of the GUID as bytes rather than a Windows [`GUID`].
15 /// This is helpful when comparing or interfacing with such projects.
16 ///
17 /// # Safety
18 /// The returned bytes represent the memory of the underlying Windows [`GUID`] struct.
19 /// They do not represent the actual GUID.
20 /// Constructing a GUID with them will result in a different GUID than the original.
21 ///
22 /// To get the correct bytes you can convert the GUID to a [`u128`] and then to bytes.
23unsafe fn misinterpret(&self) -> [u8; 16];
24}
2526impl GuidExt for GUID {
27#[inline]
28fn format_simple(&self) -> String {
29format!("{:0>32X}", self.to_u128())
30 }
3132#[inline]
33fn format_hyphenated(&self) -> String {
34format!("{self:?}")
35 }
3637#[inline]
38unsafe fn misinterpret(&self) -> [u8; 16] {
39 mem::transmute::<GUID, [u8; 16]>(*self)
40 }
41}
4243#[cfg(test)]
44mod tests {
45use super::*;
46use crate::{content::ContentInfo, Event, StateChange, TryExtract};
47use std::mem;
4849#[test]
50fn extract() {
51let event = Event {
52 is_statechange: StateChange::IdToGUID.into(),
53 src_agent: 4820869827943421467,
54 dst_agent: 11091919494850445953,
55 skill_id: 446,
56 overstack_value: 0,
57 ..unsafe { mem::zeroed() }
58 };
59assert_eq!(event.src_agent, 0x42E72B9102F7561B);
60assert_eq!(event.dst_agent, 0x99EE6A0357CA8281);
6162let info = ContentInfo::try_extract(&event).expect("failed to extract");
63assert_eq!(
64 info.guid,
65 GUID::from_u128(0x02F7561B_2B91_42E7_8182_CA57036AEE99)
66 );
67 }
6869#[test]
70fn guid_format() {
71let guid = GUID::from_u128(0x02F7561B_2B91_42E7_8182_CA57036AEE99);
7273assert_eq!(guid.format_simple(), "02F7561B2B9142E78182CA57036AEE99");
74assert_eq!(
75 guid.format_hyphenated(),
76"02F7561B-2B91-42E7-8182-CA57036AEE99"
77);
78 }
79}