use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ComError {
Hresult {
code: i32,
context: &'static str,
dispid: i32,
},
UnexpectedType {
expected: &'static str,
actual: &'static str,
},
}
impl ComError {
#[must_use]
pub const fn member(code: i32, context: &'static str, dispid: i32) -> Self {
Self::Hresult {
code,
context,
dispid,
}
}
#[must_use]
pub const fn hresult(code: i32, context: &'static str) -> Self {
Self::Hresult {
code,
context,
dispid: 0,
}
}
}
impl fmt::Display for ComError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Hresult {
code,
context,
dispid,
} => {
if *dispid == 0 {
write!(f, "COM call {context} failed with HRESULT {code:#010x}")
} else {
write!(
f,
"COM call {context} on DISPID {dispid:#x} failed with HRESULT {code:#010x}"
)
}
}
Self::UnexpectedType { expected, actual } => {
write!(f, "expected VARIANT of type {expected}, got {actual}")
}
}
}
}
impl std::error::Error for ComError {}