Debian Codegraph @

.cargo/vendor/onenote_parser/src/shared/guid.rs

raw
1 use crate::errors::Result;
2 use crate::Reader;
3 use std::fmt;
4 use uuid::Uuid;
5
6 /// A global UUID.
7 ///
8 /// Microsoft is using weird mixed endianness in their GUIDs which we have to consider when
9 /// parsing a GUID from a stream of bytes.
10 ///
11 /// See also [\[1\]] and [\[2\]].
12 ///
13 /// [\[1\]]: https://stackoverflow.com/questions/10190817/guid-byte-order-in-net
14 /// [\[2\]]: https://docs.microsoft.com/en-us/dotnet/api/system.guid.tobytearray?view=net-5.0#remarks
15 #[derive(Clone, Copy, PartialEq, Hash, Eq)]
16 pub struct Guid(pub Uuid);
17
18 impl Guid {
19 pub(crate) fn from_str(value: &str) -> Result<Guid> {
20 Uuid::parse_str(value).map(Guid).map_err(|e| e.into())
21 }
22
23 pub(crate) fn parse(reader: Reader) -> Result<Guid> {
24 // Read as little endian
25 let v = reader.get_u128()?;
26
27 let guid = Guid(Uuid::from_bytes([
28 // Big Endian
29 (v >> 24) as u8,
30 (v >> 16) as u8,
31 (v >> 8) as u8,
32 v as u8,
33 // Big Endian
34 (v >> 40) as u8,
35 (v >> 32) as u8,
36 // Big Endian
37 (v >> 56) as u8,
38 (v >> 48) as u8,
39 // Little Endian
40 (v >> 64) as u8,
41 (v >> 72) as u8,
42 (v >> 80) as u8,
43 (v >> 88) as u8,
44 (v >> 96) as u8,
45 (v >> 104) as u8,
46 (v >> 112) as u8,
47 (v >> 120) as u8,
48 ]));
49
50 Ok(guid)
51 }
52
53 pub(crate) fn nil() -> Guid {
54 Guid(Uuid::nil())
55 }
56
57 pub(crate) fn is_nil(&self) -> bool {
58 self.0.is_nil()
59 }
60 }
61
62 impl fmt::Display for Guid {
63 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64 write!(f, "{{{:X}}}", self.0)
65 }
66 }
67
68 impl fmt::Debug for Guid {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 write!(f, "Guid {}", self)
71 }
72 }