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
use std::fmt::Debug;

/// This is wrapper for handling HRESULT values.
///
/// Value is printed in hexadecimal format for convinience, this is usually the
/// format it's given in MSDN. Similarily this can be pattern matched using
/// hexadecimal format: HRESULT(0x800706BA)
#[derive(PartialEq, PartialOrd, Clone, Copy)]
pub struct HRESULT(pub u32);

impl HRESULT {
    /// Is any failure?
    #[inline]
    pub fn failed(&self) -> bool {
        (self.0 as i32) < 0
    }

    /// Indicates not a failure
    #[inline]
    pub fn ok() -> HRESULT {
        HRESULT(0)
    }

    /// Create value
    #[inline]
    pub fn from_i32(v: i32) -> HRESULT {
        HRESULT(v as u32)
    }
}

impl Default for HRESULT {
    fn default() -> Self {
        HRESULT(0)
    }
}

impl Debug for HRESULT {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "HRESULT(0x{:X})", self.0)
    }
}

impl From<i32> for HRESULT {
    fn from(item: i32) -> Self {
        HRESULT::from_i32(item)
    }
}

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

    #[test]
    fn test_ok() {
        assert_eq!(HRESULT::ok().failed(), false);
    }

    #[test]
    fn test_failure() {
        assert_eq!(HRESULT(0x800706BA).failed(), true);
    }
}