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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! # guid-create
//!
//! Rust helper for randomly creating GUIDs.
//!
//! ``` rust
//! extern crate guid_create;
//! use guid_create::GUID;
//!
//! // Create GUIDs
//! let guid = GUID::rand();
//! let guid = GUID::parse("87935CDE-7094-4C2B-A0F4-DD7D512DD261").unwrap();
//! let guid = GUID::build_from_components(0x87935CDE, 0x7094, 0x4C2B, &[0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61], );
//! let guid = GUID::build_from_slice(&[ 0x87, 0x93, 0x5C, 0xDE, 0x70, 0x94, 0x4C, 0x2B, 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61,]);
//!
//! // View GUIDs
//! guid.to_string();  // 87935CDE-7094-4C2B-A0F4-DD7D512DD261
//!
//! // Check GUIDs
//! guid.data1();
//! guid.data2();
//! guid.data3();
//! guid.data4();
//! ```
#[cfg(test)]
extern crate quickcheck;
#[cfg(test)]
#[macro_use(quickcheck)]
extern crate quickcheck_macros;

use std::{convert::TryInto, fmt};

#[cfg(windows)]
use winapi::shared::guiddef::GUID as WinGuid;

/// Parsing error type.
#[derive(Debug)]
pub struct ParseError;

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Malformed GUID, expecting XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
        )
    }
}

/// A GUID backed by 16 byte array.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default, Hash)]
pub struct GUID {
    data: [u8; 16],
}

impl GUID {
    /// Construct a `GUID` from components.
    ///
    /// ``` rust
    /// let guid = guid_create::GUID::build_from_components(
    ///     0x87935CDE,
    ///     0x7094,
    ///     0x4C2B,
    ///     &[ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]
    /// );
    ///
    /// assert_eq!(guid.data1(), 0x87935CDE);
    /// assert_eq!(guid.data2(), 0x7094);
    /// assert_eq!(guid.data3(), 0x4C2B);
    /// assert_eq!(guid.data4(), [ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]);
    /// assert_eq!(guid.to_string(), "87935CDE-7094-4C2B-A0F4-DD7D512DD261");
    /// ```
    pub fn build_from_components(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Self {
        let mut guid = GUID::default();
        // first data
        guid.data[..4].copy_from_slice(&d1.to_be_bytes());
        // second data
        guid.data[4..6].copy_from_slice(&d2.to_be_bytes());
        // third data
        guid.data[6..8].copy_from_slice(&d3.to_be_bytes());
        // fourth data
        guid.data[8..16].copy_from_slice(&d4[..8]);

        guid
    }

    /// Construct a `GUID` from 16 bytes.
    ///
    /// ``` rust
    /// let guid = guid_create::GUID::build_from_slice(&[
    ///     0x87, 0x93, 0x5C, 0xDE, 0x70, 0x94, 0x4C, 0x2B, 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D,
    ///     0xD2, 0x61,
    /// ]);

    /// assert_eq!(guid.data1(), 0x87935CDE);
    /// assert_eq!(guid.data2(), 0x7094);
    /// assert_eq!(guid.data3(), 0x4C2B);
    /// assert_eq!(
    ///     guid.data4(),
    ///     [0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61]
    /// );
    /// assert_eq!(guid.to_string(), "87935CDE-7094-4C2B-A0F4-DD7D512DD261");
    /// ```
    pub fn build_from_slice(data: &[u8; 16]) -> Self {
        GUID { data: *data }
    }

    /// Construct a `GUID` from a string.
    ///
    /// ``` rust
    /// let guid = guid_create::GUID::parse("87935CDE-7094-4C2B-A0F4-DD7D512DD261").unwrap();
    ///
    /// assert_eq!(guid.data1(), 0x87935CDE);
    /// assert_eq!(guid.data2(), 0x7094);
    /// assert_eq!(guid.data3(), 0x4C2B);
    /// assert_eq!(guid.data4(), [ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]);
    /// assert_eq!(guid.to_string(), "87935CDE-7094-4C2B-A0F4-DD7D512DD261");
    /// ```
    pub fn parse(guid: &str) -> Result<Self, ParseError> {
        fn n(ch: u8) -> Result<u8, ParseError> {
            match ch {
                b'0'..=b'9' => Ok(ch - 48),
                b'A'..=b'F' => Ok(ch - 55),
                b'a'..=b'f' => Ok(ch - 87),
                _ => Err(ParseError),
            }
        }
        fn hexbyte(s: &[u8]) -> Result<(u8, &[u8]), ParseError> {
            match s {
                [a, b, tail @ ..] => n(*a)
                    .and_then(|a| n(*b).map(|b| a * 16 + b))
                    .map(|x| (x, tail)),
                _ => Err(ParseError),
            }
        }
        fn strip_dash(s: &[u8]) -> Result<&[u8], ParseError> {
            match s {
                [b'-', tail @ ..] => Ok(tail),
                _ => Err(ParseError),
            }
        }

        let mut data = [0u8; 16];

        let mut s = guid.as_bytes();

        fn fill<'a>(buf: &mut [u8], mut s: &'a [u8]) -> Result<&'a [u8], ParseError> {
            for l in buf {
                let (d, s_) = hexbyte(s)?;
                *l = d;
                s = s_;
            }
            Ok(s)
        }

        // first four bytes
        s = fill(&mut data[..4], s)?;
        s = strip_dash(s)?;

        // second two bytes
        s = fill(&mut data[4..6], s)?;
        s = strip_dash(s)?;

        // third two bytes
        s = fill(&mut data[6..8], s)?;
        s = strip_dash(s)?;

        // fourth two bytes
        s = fill(&mut data[8..10], s)?;
        s = strip_dash(s)?;

        // trailing bytes
        s = fill(&mut data[10..], s)?;

        // should be empty!
        if s.is_empty() {
            Ok(Self { data })
        } else {
            Err(ParseError)
        }
    }

    /// Generates a new GUID with 16 random bytes.
    pub fn rand() -> GUID {
        GUID {
            data: rand::random(),
        }
    }

    /// The first four bytes.
    ///
    /// ``` rust
    /// extern crate guid_create;
    /// let guid = guid_create::GUID::build_from_components(
    ///     500,
    ///     600,
    ///     700,
    ///     &[ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]
    /// );
    ///
    /// assert_eq!(guid.data1(), 500);
    /// ```
    pub fn data1(&self) -> u32 {
        u32::from_be_bytes(
            self.data[0..4]
                .try_into()
                .expect("slice with incorrect length"),
        )
    }

    /// Bytes 5 and 6.
    ///
    /// ``` rust
    /// extern crate guid_create;
    /// let guid = guid_create::GUID::build_from_components(
    ///     500,
    ///     600,
    ///     700,
    ///     &[ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]
    /// );
    ///
    /// assert_eq!(guid.data2(), 600);
    /// ```
    pub fn data2(&self) -> u16 {
        u16::from_be_bytes(
            self.data[4..6]
                .try_into()
                .expect("slice with incorrect length"),
        )
    }

    /// Bytes 7 and 8.
    ///
    /// ``` rust
    /// extern crate guid_create;
    /// let guid = guid_create::GUID::build_from_components(
    ///     500,
    ///     600,
    ///     700,
    ///     &[ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]
    /// );
    ///
    /// assert_eq!(guid.data3(), 700);
    /// ```
    pub fn data3(&self) -> u16 {
        u16::from_be_bytes(
            self.data[6..8]
                .try_into()
                .expect("slice with incorrect length"),
        )
    }

    /// The last eight bytes.
    ///
    /// ``` rust
    /// extern crate guid_create;
    /// let guid = guid_create::GUID::build_from_components(
    ///     500,
    ///     600,
    ///     700,
    ///     &[ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]
    /// );
    ///
    /// assert_eq!(
    ///     guid.data4(),
    ///     [0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61]
    /// );
    /// ```
    pub fn data4(&self) -> [u8; 8] {
        self.data[8..16]
            .try_into()
            .expect("slice with incorrect length")
    }

    /// Convert the `GUID` to a `winapi` [GUID](https://docs.rs/winapi/0.3.4/x86_64-pc-windows-msvc/winapi/shared/guiddef/struct.GUID.html)
    /// > Only present on windows targets
    ///
    /// ``` rust
    /// extern crate guid_create;
    /// let guid = guid_create::GUID::build_from_components(
    /// 	0x87935CDE,
    /// 	0x7094,
    /// 	0x4C2B,
    /// 	&[ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]
    /// 	);
    ///
    /// let win = guid.as_winapi_guid();
    /// assert_eq!(win.Data1, 0x87935CDE);
    /// assert_eq!(win.Data2, 0x7094);
    /// assert_eq!(win.Data3, 0x4C2B);
    /// assert_eq!(win.Data4, [ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]);
    /// ```
    #[cfg(windows)]
    pub fn as_winapi_guid(&self) -> WinGuid {
        WinGuid {
            Data1: self.data1(),
            Data2: self.data2(),
            Data3: self.data3(),
            Data4: self.data4(),
        }
    }

    /// Convert a `winapi` [GUID](https://docs.rs/winapi/0.3.4/x86_64-pc-windows-msvc/winapi/shared/guiddef/struct.GUID.html) to a `GUID`
    /// > Only present on windows targets
    ///
    /// ``` rust
    /// extern crate guid_create;
    /// extern crate winapi;
    /// let win = winapi::shared::guiddef::GUID {
    /// 	Data1: 0x87935CDE,
    /// 	Data2: 0x7094,
    /// 	Data3: 0x4C2B,
    /// 	Data4: [ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]
    /// 	};
    ///
    /// let guid = guid_create::GUID::from_winapi_guid(win);
    /// assert_eq!(guid.data1(), 0x87935CDE);
    /// assert_eq!(guid.data2(), 0x7094);
    /// assert_eq!(guid.data3(), 0x4C2B);
    /// assert_eq!(guid.data4(), [ 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61 ]);
    /// ```
    #[cfg(windows)]
    pub fn from_winapi_guid(guid: WinGuid) -> Self {
        GUID::build_from_components(guid.Data1, guid.Data2, guid.Data3, &guid.Data4)
    }
}

#[cfg(feature = "serde")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for GUID {
    fn deserialize<D>(deserializer: D) -> Result<GUID, D::Error>
    where
        D: Deserializer<'de>,
    {
        let string_guid = String::deserialize(deserializer)?;
        let guid = GUID::parse(&string_guid)
            .map_err(|_| de::Error::custom(format!("cannot convert {string_guid} to guid")))?;
        Ok(guid)
    }
}

#[cfg(feature = "serde")]
impl Serialize for GUID {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(&*self.to_string())
    }
}

impl fmt::Display for GUID {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{:08X}-{:04X}-{:04X}-{:04X}-{:08X}{:04X}",
            self.data1(),
            self.data2(),
            self.data3(),
            u16::from_be_bytes(self.data[8..10].try_into().unwrap()),
            u32::from_be_bytes(self.data[10..14].try_into().unwrap()),
            u16::from_be_bytes(self.data[14..16].try_into().unwrap()),
        )
    }
}

#[cfg(test)]
impl quickcheck::Arbitrary for GUID {
    fn arbitrary(g: &mut quickcheck::Gen) -> Self {
        let mut data = [0u8; 16];
        data.fill_with(|| u8::arbitrary(g));
        Self { data }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn travis_test() {}

    #[test]
    fn string_lengths() {
        for _ in 0..10000 {
            let guid = GUID::rand();
            let s = guid.to_string();
            println!("{}", s);
            assert_eq!(s.len(), 36);
        }
    }

    #[cfg(windows)]
    #[test]
    fn win_guid() {
        for _ in 0..10000 {
            let guid = GUID::rand();
            let win = guid.as_winapi_guid();
            assert_eq!(guid.data1(), win.Data1);
            assert_eq!(guid.data2(), win.Data2);
            assert_eq!(guid.data3(), win.Data3);
            assert_eq!(guid.data4(), win.Data4);
            let convert_back = GUID::from_winapi_guid(win);
            assert_eq!(guid, convert_back);
        }
    }

    #[test]
    fn create_from_components() {
        let guid = GUID::build_from_components(
            0x87935CDE,
            0x7094,
            0x4C2B,
            &[0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61],
        );

        assert_eq!(guid.data1(), 0x87935CDE);
        assert_eq!(guid.data2(), 0x7094);
        assert_eq!(guid.data3(), 0x4C2B);
        assert_eq!(
            guid.data4(),
            [0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61]
        );
        assert_eq!(guid.to_string(), "87935CDE-7094-4C2B-A0F4-DD7D512DD261");
    }

    #[test]
    fn create_from_array() {
        let guid = GUID::build_from_slice(&[
            0x87, 0x93, 0x5C, 0xDE, 0x70, 0x94, 0x4C, 0x2B, 0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D,
            0xD2, 0x61,
        ]);

        println!("{}", guid);

        assert_eq!(guid.data1(), 0x87935CDE);
        assert_eq!(guid.data2(), 0x7094);
        assert_eq!(guid.data3(), 0x4C2B);
        assert_eq!(
            guid.data4(),
            [0xA0, 0xF4, 0xDD, 0x7D, 0x51, 0x2D, 0xD2, 0x61]
        );
        assert_eq!(guid.to_string(), "87935CDE-7094-4C2B-A0F4-DD7D512DD261");
    }

    #[test]
    fn parse_strings() {
        for _ in 0..10000 {
            let guid = GUID::rand();
            let s = guid.to_string();
            let guid2 = GUID::parse(&s).unwrap();
            assert_eq!(guid, guid2);
        }
    }

    #[quickcheck]
    fn no_panic_parse(s: String) {
        GUID::parse(&s).ok();
    }

    #[quickcheck]
    fn parse_success(guid: GUID) -> bool {
        let s = guid.to_string();
        let g2 = GUID::parse(&s).unwrap();
        g2 == guid
    }
}