1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
extern crate chomp;
extern crate failure;
#[macro_use]
extern crate failure_derive;
extern crate guid_parser;
use chomp::prelude::*;
use guid_parser::Chunks;
use std::string::ToString;
#[derive(Fail, Debug)]
#[fail(display = "{}", msg)]
pub struct ParseGuidError {
msg: String
}
fn parse_chunks(src: &str) -> Result<Chunks, ParseGuidError> {
parse_only(guid_parser::chunks, src.as_bytes())
.map_err(|(_, e)| ParseGuidError {
msg: e.to_string()
})
}
pub fn parse_bytes(src: &str) -> Result<[u8; 16], ParseGuidError> {
parse_chunks(src).map(|chunks| chunks.to_bytes())
}
#[cfg(windows)]
pub fn parse(src: &str) -> Result<GUID, ParseGuidError> {
parse_chunks(src).map(|chunks| chunks.to_guid())
}
#[cfg(windows)]
#[macro_use]
extern crate proc_macro_hack;
#[cfg(windows)]
#[allow(unused_imports)]
#[macro_use]
extern crate guid_macro_impl;
#[cfg(windows)]
#[doc(hidden)]
pub use guid_macro_impl::*;
#[cfg(windows)]
proc_macro_expr_decl! {
#[doc(hidden)]
guid_parts! => guid_parts_impl
}
#[cfg(windows)]
extern crate winapi;
#[cfg(windows)]
pub use winapi::guiddef::GUID;
#[cfg(windows)]
#[macro_export]
macro_rules! guid {
{$literal:expr} => {
{
const PARTS: (u32, u16, u16, [u8; 8]) = guid_parts! {$literal};
$crate::GUID {
Data1: PARTS.0,
Data2: PARTS.1,
Data3: PARTS.2,
Data4: PARTS.3
}
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_parser() {
use parse_bytes;
let bytes = parse_bytes("6B29FC40-CA47-1067-B31D-00DD010662DA").unwrap();
assert_eq!(bytes, [ 0x6B, 0x29, 0xFC, 0x40, 0xCA, 0x47, 0x10, 0x67, 0xB3, 0x1D, 0x00, 0xDD, 0x01, 0x06, 0x62, 0xDA ]);
}
#[cfg(windows)]
#[test]
fn test_macro() {
assert_eq!(guid!{"6B29FC40-CA47-1067-B31D-00DD010662DA"}.Data1, 0x6B29FC40);
assert_eq!(guid!{"6B29FC40-CA47-1067-B31D-00DD010662DA"}.Data2, 0xCA47);
assert_eq!(guid!{"6B29FC40-CA47-1067-B31D-00DD010662DA"}.Data3, 0x1067);
assert_eq!(guid!{"6B29FC40-CA47-1067-B31D-00DD010662DA"}.Data4, [ 0xB3, 0x1D, 0x00, 0xDD, 0x01, 0x06, 0x62, 0xDA ]);
}
}