| 1 |
// Licensed under the Apache License, Version 2.0 |
| 2 |
// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 3 |
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 4 |
// All files in the project carrying such notice may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
|
| 7 |
//! Windows GUID/CLSID/IID string and binary serialization |
| 8 |
//! |
| 9 |
//! [`Guid`](struct.Guid.html) transparently wraps |
| 10 |
//! [`GUID`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa373931(v=vs.85).aspx). |
| 11 |
//! |
| 12 |
//! Implements `Display` and `FromStr` string conversion, also `Hash` and `Eq`. |
| 13 |
//! |
| 14 |
//! Curly braces (`{}`) are optional for `FromStr`. |
| 15 |
//! |
| 16 |
//! # serde # |
| 17 |
//! |
| 18 |
//! Use the `guid_serde` feature to derive `Serialize` and `Deserialize`, you can then |
| 19 |
//! derive them for structs containing `GUID` like so: |
| 20 |
//! |
| 21 |
//! ``` |
| 22 |
//! # fn main() {} |
| 23 |
//! # |
| 24 |
//! # #[cfg(feature = "guid_serde")] |
| 25 |
//! # extern crate serde_derive; |
| 26 |
//! # extern crate winapi; |
| 27 |
//! # |
| 28 |
//! # #[cfg(feature = "guid_serde")] |
| 29 |
//! # mod test { |
| 30 |
//! use guid_win::GUIDSerde; |
| 31 |
//! use serde_derive::{Deserialize, Serialize}; |
| 32 |
//! use winapi::shared::guiddef::GUID; |
| 33 |
//! |
| 34 |
//! #[derive(Serialize, Deserialize)] |
| 35 |
//! struct SerdeTest { |
| 36 |
//! #[serde(with = "GUIDSerde")] |
| 37 |
//! guid: GUID, |
| 38 |
//! } |
| 39 |
//! # } |
| 40 |
//! ``` |
| 41 |
|
| 42 |
extern crate comedy; |
| 43 |
#[cfg(feature = "guid_serde")] |
| 44 |
extern crate serde; |
| 45 |
#[cfg(feature = "guid_serde")] |
| 46 |
extern crate serde_derive; |
| 47 |
extern crate winapi; |
| 48 |
|
| 49 |
use std::ffi::{OsStr, OsString}; |
| 50 |
use std::fmt::{Debug, Display, Error, Formatter, Result}; |
| 51 |
use std::hash::{Hash, Hasher}; |
| 52 |
use std::mem; |
| 53 |
use std::os::windows::ffi::{OsStrExt, OsStringExt}; |
| 54 |
use std::result; |
| 55 |
use std::str::FromStr; |
| 56 |
|
| 57 |
use comedy::check_succeeded; |
| 58 |
|
| 59 |
use winapi::ctypes; |
| 60 |
use winapi::shared::guiddef::GUID; |
| 61 |
use winapi::um::combaseapi::{CLSIDFromString, StringFromGUID2}; |
| 62 |
|
| 63 |
#[cfg(feature = "guid_serde")] |
| 64 |
use serde_derive::{Deserialize, Serialize}; |
| 65 |
|
| 66 |
const GUID_STRING_CHARACTERS: usize = 38; |
| 67 |
|
| 68 |
#[cfg(feature = "guid_serde")] |
| 69 |
#[allow(non_snake_case)] |
| 70 |
#[derive(Serialize, Deserialize)] |
| 71 |
#[serde(remote = "GUID")] |
| 72 |
pub struct GUIDSerde { |
| 73 |
Data1: ctypes::c_ulong, |
| 74 |
Data2: ctypes::c_ushort, |
| 75 |
Data3: ctypes::c_ushort, |
| 76 |
Data4: [ctypes::c_uchar; 8], |
| 77 |
} |
| 78 |
|
| 79 |
/// Wraps `GUID` |
| 80 |
#[derive(Clone)] |
| 81 |
#[cfg_attr(feature = "guid_serde", derive(Serialize, Deserialize))] |
| 82 |
#[repr(transparent)] |
| 83 |
pub struct Guid(#[cfg_attr(feature = "guid_serde", serde(with = "GUIDSerde"))] pub GUID); |
| 84 |
|
| 85 |
impl PartialEq for Guid { |
| 86 |
fn eq(&self, other: &Guid) -> bool { |
| 87 |
self.0.Data1 == other.0.Data1 |
| 88 |
&& self.0.Data2 == other.0.Data2 |
| 89 |
&& self.0.Data3 == other.0.Data3 |
| 90 |
&& self.0.Data4 == other.0.Data4 |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
impl Eq for Guid {} |
| 95 |
|
| 96 |
impl Hash for Guid { |
| 97 |
fn hash<H: Hasher>(&self, state: &mut H) { |
| 98 |
self.0.Data1.hash(state); |
| 99 |
self.0.Data2.hash(state); |
| 100 |
self.0.Data3.hash(state); |
| 101 |
self.0.Data4.hash(state); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
impl Debug for Guid { |
| 106 |
fn fmt(&self, f: &mut Formatter) -> Result { |
| 107 |
write!(f, "{:?}", unsafe { |
| 108 |
&mem::transmute::<Guid, [u8; mem::size_of::<Guid>()]>(self.clone()) |
| 109 |
}) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/// Output a string via `StringFromGUID2()` |
| 114 |
impl Display for Guid { |
| 115 |
fn fmt(&self, f: &mut Formatter) -> Result { |
| 116 |
let mut s: [u16; GUID_STRING_CHARACTERS + 1] = unsafe { mem::uninitialized() }; |
| 117 |
|
| 118 |
let len = unsafe { |
| 119 |
StringFromGUID2( |
| 120 |
&(*self).0 as *const _ as *mut _, |
| 121 |
s.as_mut_ptr(), |
| 122 |
s.len() as ctypes::c_int, |
| 123 |
) |
| 124 |
}; |
| 125 |
|
| 126 |
if len <= 0 { |
| 127 |
return Err(Error); |
| 128 |
} |
| 129 |
// len is number of characters, including the null terminator |
| 130 |
|
| 131 |
let s = &s[..len as usize - 1]; |
| 132 |
// TODO: no reason to expect this to fail, maybe just unwrap() |
| 133 |
if let Ok(s) = OsString::from_wide(&s).into_string() { |
| 134 |
f.write_str(&s) |
| 135 |
} else { |
| 136 |
Err(Error) |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/// Read from a string via `CLSIDFromString()` |
| 142 |
/// |
| 143 |
/// Braces (`{}`) are added if missing. |
| 144 |
impl FromStr for Guid { |
| 145 |
type Err = comedy::error::HResult; |
| 146 |
|
| 147 |
fn from_str(s: &str) -> result::Result<Self, Self::Err> { |
| 148 |
let mut guid = unsafe { mem::uninitialized() }; |
| 149 |
|
| 150 |
let braced; |
| 151 |
let s = if s.starts_with('{') { |
| 152 |
s |
| 153 |
} else { |
| 154 |
braced = format!("{{{}}}", s); |
| 155 |
braced.as_str() |
| 156 |
}; |
| 157 |
let s: Vec<_> = OsStr::new(s).encode_wide().chain(Some(0)).collect(); |
| 158 |
|
| 159 |
unsafe { check_succeeded!(CLSIDFromString(s.as_ptr(), &mut guid)) }?; |
| 160 |
|
| 161 |
Ok(Guid(guid)) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
#[cfg(test)] |
| 166 |
mod test { |
| 167 |
use super::Guid; |
| 168 |
use std::str::FromStr; |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn without_braces() { |
| 172 |
let uuid = "F1BD1079-9F01-4BDC-8036-F09B70095066"; |
| 173 |
let guid = Guid::from_str(uuid).unwrap(); |
| 174 |
assert_eq!(format!("{}", guid), format!("{{{}}}", uuid)); |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn with_braces() { |
| 179 |
let uuid = "{F1BD1079-9F01-4BDC-8036-F09B70095066}"; |
| 180 |
let guid = Guid::from_str(uuid).unwrap(); |
| 181 |
assert_eq!(format!("{}", guid), uuid); |
| 182 |
} |
| 183 |
|
| 184 |
#[test] |
| 185 |
fn format_error() { |
| 186 |
let uuid = "foo"; |
| 187 |
Guid::from_str(uuid).unwrap_err(); |
| 188 |
} |
| 189 |
} |