Skip to main content

tss_esapi/
error.rs

1// Copyright 2020 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3mod return_code;
4mod wrapper;
5
6pub use return_code::{
7    ArgumentNumber, BaseReturnCode, EsapiReturnCode, FapiReturnCode, MuapiReturnCode, ReturnCode,
8    SapiReturnCode, TctiReturnCode, TpmFormatOneResponseCode, TpmFormatZeroErrorResponseCode,
9    TpmFormatZeroResponseCode, TpmFormatZeroWarningResponseCode, TpmResponseCode,
10};
11pub use wrapper::WrapperErrorKind;
12
13pub type Result<T> = std::result::Result<T, Error>;
14
15/// Main error type used by the crate to return issues with a method call. The value can either be
16/// a TSS-generated response code or a wrapper error - marking an issue caught within the wrapping
17/// layer.
18#[derive(Debug, Copy, Clone, PartialEq, Eq)]
19pub enum Error {
20    WrapperError(WrapperErrorKind),
21    TssError(ReturnCode),
22}
23
24impl Error {
25    /// Creates a wrapper error.
26    pub(crate) const fn local_error(kind: WrapperErrorKind) -> Self {
27        Error::WrapperError(kind)
28    }
29
30    /// Creates a TSS error.
31    pub(crate) const fn tss_error(return_code: ReturnCode) -> Self {
32        Error::TssError(return_code)
33    }
34}
35
36impl std::fmt::Display for Error {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        match self {
39            Error::WrapperError(e) => e.fmt(f),
40            Error::TssError(e) => e.fmt(f),
41        }
42    }
43}
44
45impl std::error::Error for Error {
46    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
47        match self {
48            Error::WrapperError(wrapper_error) => Some(wrapper_error),
49            Error::TssError(response_code) => Some(response_code),
50        }
51    }
52}