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
#![allow(non_camel_case_types)]
use std::error::Error;
use std::fmt;
use std::os::raw::{c_int, c_uchar, c_uint, c_ulonglong, c_ushort, c_void};
pub type HRESULT = c_int;
pub type UINT8 = c_uchar;
pub type UINT16 = c_ushort;
pub type UINT32 = c_uint;
pub type UINT64 = c_ulonglong;
pub type VOID = c_void;
pub type BOOL = c_int;
pub type DWORD = c_uint;
pub type SIZE_T = c_ulonglong;
pub const FALSE: i32 = 0;
pub const TRUE: i32 = 1;
pub const S_OK: HRESULT = 0;
pub const E_FAIL: HRESULT = -2147467259;
pub const ERROR_HV_NOT_PRESENT: HRESULT = -1070264320;
pub const E_INVALIDARG: HRESULT = -2147024809;
#[derive(Debug)]
pub struct WHPError {
result: HRESULT,
}
impl WHPError {
pub fn new(result: HRESULT) -> WHPError {
WHPError { result: result }
}
pub fn result(&self) -> HRESULT {
self.result
}
}
impl Error for WHPError {
fn description(&self) -> &str {
"WHP error"
}
}
impl fmt::Display for WHPError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "WHP error code: 0x{:X}", self.result)
}
}
pub fn check_result(res: HRESULT) -> Result<(), WHPError> {
match res {
S_OK => Ok(()),
_ => Err(WHPError::new(res)),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_check_result_ok() {
check_result(S_OK).unwrap();
}
#[test]
#[should_panic]
fn test_check_result_fail() {
check_result(E_INVALIDARG).unwrap();
}
}