Skip to contentSkip to conversation

refactor!(TpmRc): remove unnecessary Result wrapper

PATCH In review Jarkko Sakkinen 1 participant 2 messages Aug 31, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 refactor!(TpmRc): remove unnecessary Result wrapper

Result wrapper in the return value of TpmRc::base() exists only to retain API backwards compatibility in 0.10.x branch. Remove the wrapper in the main branch.

---
 src/data/tpm_rc.rs | 20 +++++++-------------
 tests/runner.rs    |  2 +-
 2 files changed, 8 insertions(+), 14 deletions(-)
src/data/tpm_rc.rs +7 -13
index c7fa71a..fa8756f 100644
@@ -165,12 +165,9 @@ pub struct TpmRc {
 impl TpmRc {
     /// Returns the base error code, with handle, parameter, or session index
     /// stripped out.
-    ///
-    /// # Errors
-    ///
-    /// This method returns a `Result` in order to retain API compatibility.
-    pub fn base(self) -> Result<TpmRcBase, TpmErrorKind> {
-        Ok(self.base)
+    #[must_use]
+    pub fn base(self) -> TpmRcBase {
+        self.base
     }
 
     #[must_use]
@@ -250,14 +247,11 @@ impl From<TpmRcBase> for TpmRc {
 
 impl Display for TpmRc {
     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
-        if let Ok(base) = self.base() {
-            if let Some(index) = self.index() {
-                write!(f, "[{base}, {index}]")
-            } else {
-                write!(f, "{base}")
-            }
+        let base = self.base();
+        if let Some(index) = self.index() {
+            write!(f, "[{base}, {index}]")
         } else {
-            write!(f, "TPM_RC_UNKNOWN(0x{:08X})", self.value())
+            write!(f, "{base}")
         }
     }
 }
tests/runner.rs +1 -1
index 83fc209..218d813 100644
@@ -239,7 +239,7 @@ fn test_tpm_rc_base_from_raw() {
 
     for (description, raw_rc, expected_base) in cases {
         let rc = TpmRc::try_from(raw_rc).unwrap();
-        assert_eq!(rc.base(), Ok(expected_base), "{description}");
+        assert_eq!(rc.base(), expected_base, "{description}");
     }
 }
 
Signature · 1 line
2.39.5
#1 lore raw
Quoted 5 lines · Jarkko Sakkinen

Result wrapper in the return value of TpmRc::base() exists only to retain API backwards compatibility in 0.10.x branch. Remove the wrapper in the main branch.

This was easy test patch to check that everything is working. I'll apply it to the main branch without further ado.

Quoted 60 lines
---
 src/data/tpm_rc.rs | 20 +++++++-------------
 tests/runner.rs    |  2 +-
 2 files changed, 8 insertions(+), 14 deletions(-)
src/data/tpm_rc.rs +7 -13
index c7fa71a..fa8756f 100644
@@ -165,12 +165,9 @@ pub struct TpmRc {
 impl TpmRc {
     /// Returns the base error code, with handle, parameter, or session index
     /// stripped out.
-    ///
-    /// # Errors
-    ///
-    /// This method returns a `Result` in order to retain API compatibility.
-    pub fn base(self) -> Result<TpmRcBase, TpmErrorKind> {
-        Ok(self.base)
+    #[must_use]
+    pub fn base(self) -> TpmRcBase {
+        self.base
     }
 
     #[must_use]
@@ -250,14 +247,11 @@ impl From<TpmRcBase> for TpmRc {
 
 impl Display for TpmRc {
     fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
-        if let Ok(base) = self.base() {
-            if let Some(index) = self.index() {
-                write!(f, "[{base}, {index}]")
-            } else {
-                write!(f, "{base}")
-            }
+        let base = self.base();
+        if let Some(index) = self.index() {
+            write!(f, "[{base}, {index}]")
         } else {
-            write!(f, "TPM_RC_UNKNOWN(0x{:08X})", self.value())
+            write!(f, "{base}")
         }
     }
 }
tests/runner.rs +1 -1
index 83fc209..218d813 100644
@@ -239,7 +239,7 @@ fn test_tpm_rc_base_from_raw() {
 
     for (description, raw_rc, expected_base) in cases {
         let rc = TpmRc::try_from(raw_rc).unwrap();
-        assert_eq!(rc.base(), Ok(expected_base), "{description}");
+        assert_eq!(rc.base(), expected_base, "{description}");
     }
 }
 
Signature · 1 line
2.39.5

BR, Jarkko