Skip to content

Commit

Permalink
impl compare for FabricErrorCode (#110)
Browse files Browse the repository at this point in the history
FabricErrorCode needs PartialEq impl for comparison.
  • Loading branch information
youyuanwu authored Dec 12, 2024
1 parent 5781c7d commit 8892f31
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crates/libs/core/src/error/errorcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const E_NOT_FOUND: FABRIC_ERROR_CODE =
macro_rules! define_fabric_error_code{
($( $code1:ident ),* ,($lit:literal), $( $code:ident ),*) =>{
#[allow(non_camel_case_types)]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
#[repr(i32)]
pub enum FabricErrorCode {
// Define windows error codes for SF
Expand Down
9 changes: 9 additions & 0 deletions crates/libs/core/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ impl FabricError {
pub fn new(code: HRESULT) -> Self {
Self(code)
}

/// Convert to fabric error code if possible.
pub fn try_as_fabric_error_code(&self) -> Result<FabricErrorCode, &str> {
FabricErrorCode::try_from(FABRIC_ERROR_CODE(self.0 .0))
}
}

impl From<HRESULT> for FabricError {
Expand Down Expand Up @@ -104,6 +109,10 @@ mod test {
);
let e = crate::Error::from(fe.clone());
assert_eq!(e.code(), fe.into());
let ec = FabricError::from(e)
.try_as_fabric_error_code()
.expect("unknown code");
assert_eq!(ec, FabricErrorCode::FABRIC_E_CODE_PACKAGE_NOT_FOUND);
}

#[test]
Expand Down

0 comments on commit 8892f31

Please sign in to comment.