pub struct HRESULT(i32);
impl std::fmt::Debug for HRESULT {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
f.debug_struct("HRESULT")
.field("s", &self.s()) .field("r", &self.r()) .field("c", &self.c()) .field("n", &self.n()) .field("x", &self.x()) .field("facility", &self.facility()) .field("code", &self.code()) .finish()
}
}
impl std::fmt::Display for HRESULT {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "{:#08X}", self.0)
}
}
impl From<i32> for HRESULT {
fn from(val: i32) -> Self {
HRESULT(val)
}
}
impl From<u32> for HRESULT {
fn from(val: u32) -> Self {
HRESULT(val as i32)
}
}
impl HRESULT {
pub fn is_success(&self) -> bool {
self.s()
}
pub fn is_customer(&self) -> bool {
self.c()
}
pub fn s(&self) -> bool {
self.0 >= 0
}
pub fn r(&self) -> bool {
self.0 & 0b0100_0000_0000_0000_0000_0000_0000_0000 != 0
}
pub fn c(&self) -> bool {
self.0 & 0b0010_0000_0000_0000_0000_0000_0000_0000 != 0
}
pub fn n(&self) -> bool {
self.0 & 0b0001_0000_0000_0000_0000_0000_0000_0000 != 0
}
pub fn x(&self) -> bool {
self.0 & 0b0000_1000_0000_0000_0000_0000_0000_0000 != 0
}
pub fn facility(&self) -> u16 {
(self.0 >> 16) as u16 & 0b0000_0111_1111_1111
}
pub fn code(&self) -> u16 {
self.0 as u16
}
}
#[allow(clippy::unreadable_literal)]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simple() {
let hr: HRESULT = 0x80070005_u32.into();
assert!(!hr.s());
assert!(!hr.r());
assert!(!hr.c());
assert!(!hr.n());
assert!(!hr.x());
assert_eq!(hr.facility(), 0x7);
assert_eq!(hr.code(), 0x5);
let hr: HRESULT = 0x80004005_u32.into();
assert!(!hr.s());
assert!(!hr.r());
assert!(!hr.c());
assert!(!hr.n());
assert!(!hr.x());
assert_eq!(hr.facility(), 0x0);
assert_eq!(hr.code(), 0x4005);
}
}