pelite\util/
guid.rs

1// Provide implementations for image GUID here
2// FIXME! Should I keep this GUID implementation or defer to another GUID library?
3
4use std::fmt;
5use crate::image::GUID;
6
7#[inline(always)]
8fn group(guid: &GUID) -> (u32, u16, u16, u16, u64) {
9	let g1 = guid.Data1;
10	let g2 = guid.Data2;
11	let g3 = guid.Data3;
12	// Mind the (little-) endianness
13	let g4 = (guid.Data4[0] as u16) << 8 | guid.Data4[1] as u16;
14	let g5 =
15		(guid.Data4[2] as u64) << 8*5 | (guid.Data4[3] as u64) << 8*4 |
16		(guid.Data4[4] as u64) << 8*3 | (guid.Data4[5] as u64) << 8*2 |
17		(guid.Data4[6] as u64) << 8*1 | (guid.Data4[7] as u64) << 8*0;
18	(g1, g2, g3, g4, g5)
19}
20
21fn lower_dashed(guid: &GUID, f: &mut fmt::Formatter) -> fmt::Result {
22	let (g1, g2, g3, g4, g5) = group(guid);
23	write!(f, "{{{:08x}-{:04x}-{:04x}-{:04x}-{:012x}}}", g1, g2, g3, g4, g5)
24}
25// fn upper_dashed(guid: &GUID, f: &mut fmt::Formatter) -> fmt::Result {
26// 	let (g1, g2, g3, g4, g5) = group(guid);
27// 	write!(f, "{{{:08X}-{:04X}-{:04X}-{:04X}-{:012X}}}", g1, g2, g3, g4, g5)
28// }
29fn lower_hex(guid: &GUID, f: &mut fmt::Formatter) -> fmt::Result {
30	let (g1, g2, g3, g4, g5) = group(guid);
31	write!(f, "{:08x}{:04x}{:04x}{:04x}{:012x}", g1, g2, g3, g4, g5)
32}
33fn upper_hex(guid: &GUID, f: &mut fmt::Formatter) -> fmt::Result {
34	let (g1, g2, g3, g4, g5) = group(guid);
35	write!(f, "{:08X}{:04X}{:04X}{:04X}{:012X}", g1, g2, g3, g4, g5)
36}
37
38/// example: `{00000000-0000-0000-c000-000000000046}`
39impl fmt::Display for GUID {
40	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41		lower_dashed(self, f)
42	}
43}
44/// example: `{00000000-0000-0000-c000-000000000046}`
45impl fmt::Debug for GUID {
46	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47		lower_dashed(self, f)
48	}
49}
50/// example: `0000000000000000c000000000000046`
51impl fmt::LowerHex for GUID {
52	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53		lower_hex(self, f)
54	}
55}
56/// example: `0000000000000000C000000000000046`
57impl fmt::UpperHex for GUID {
58	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59		upper_hex(self, f)
60	}
61}
62
63#[cfg(feature = "serde")]
64impl serde::Serialize for GUID {
65	fn serialize<S: serde::Serializer>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> {
66		serializer.collect_str(self)
67	}
68}