1use crate::constants::response_code::Tss2ResponseCode;
4use crate::tss2_esys::TSS2_RC;
5pub type Result<T> = std::result::Result<T, Error>;
9
10#[derive(Copy, Clone, PartialEq, Eq, Debug)]
11pub enum Error {
12 WrapperError(WrapperErrorKind),
13 Tss2Error(Tss2ResponseCode),
14}
15
16impl Error {
17 pub(crate) fn from_tss_rc(rc: TSS2_RC) -> Self {
18 Error::Tss2Error(Tss2ResponseCode::from_tss_rc(rc))
19 }
20
21 pub(crate) fn local_error(kind: WrapperErrorKind) -> Self {
22 Error::WrapperError(kind)
23 }
24
25 pub fn is_success(self) -> bool {
27 if let Error::Tss2Error(tss2_rc) = self {
28 tss2_rc.is_success()
29 } else {
30 false
31 }
32 }
33}
34
35impl std::fmt::Display for Error {
36 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 match self {
38 Error::WrapperError(e) => e.fmt(f),
39 Error::Tss2Error(e) => e.fmt(f),
40 }
41 }
42}
43
44impl std::error::Error for Error {
45 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46 match self {
47 Error::WrapperError(wrapper_error) => Some(wrapper_error),
48 Error::Tss2Error(response_code) => Some(response_code),
49 }
50 }
51}
52
53#[derive(Copy, Clone, PartialEq, Eq, Debug)]
55pub enum WrapperErrorKind {
56 WrongParamSize,
59 ParamsMissing,
61 InconsistentParams,
63 UnsupportedParam,
65 InvalidParam,
67 WrongValueFromTpm,
69 MissingAuthSession,
72 InvalidHandleState,
75 InternalError,
77}
78
79impl std::fmt::Display for WrapperErrorKind {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 match self {
82 WrapperErrorKind::WrongParamSize => {
83 write!(f, "parameter provided is of the wrong size")
84 }
85 WrapperErrorKind::ParamsMissing => {
86 write!(f, "some of the required parameters were not provided")
87 }
88 WrapperErrorKind::InconsistentParams => write!(
89 f,
90 "the provided parameters have inconsistent values or variants"
91 ),
92 WrapperErrorKind::UnsupportedParam => write!(
93 f,
94 "the provided parameter is not yet supported by the library"
95 ),
96 WrapperErrorKind::InvalidParam => {
97 write!(f, "the provided parameter is invalid for that type.")
98 }
99 WrapperErrorKind::WrongValueFromTpm => write!(f, "the TPM returned an invalid value."),
100 WrapperErrorKind::MissingAuthSession => write!(f, "Missing authorization session"),
101 WrapperErrorKind::InvalidHandleState => write!(f, "Invalid handle state"),
102 WrapperErrorKind::InternalError => {
103 write!(f, "an unexpected error occurred within the crate")
104 }
105 }
106 }
107}
108
109impl std::error::Error for WrapperErrorKind {}