use core::fmt;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
Transport {
code: i32,
},
ResponseBufferTooSmall,
MalformedResponse,
}
impl Error {
#[inline]
pub(crate) fn check(rc: i32) -> Result<(), Error> {
if rc == 0 {
Ok(())
} else {
Err(Error::Transport { code: rc })
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Transport { code } => {
write!(f, "wolfTPM transport error {code:#010x}")
}
Error::ResponseBufferTooSmall => {
write!(f, "response buffer too small for TPM response")
}
Error::MalformedResponse => {
write!(f, "TPM returned a malformed response header")
}
}
}
}
impl std::error::Error for Error {}
#[cfg(feature = "tss")]
impl From<tpm2_rs_base::errors::TssError> for Error {
fn from(e: tpm2_rs_base::errors::TssError) -> Self {
Error::Transport { code: e.get() as i32 }
}
}