tpm2_protocol/macro/
enum.rs1#[macro_export]
10macro_rules! tpm_enum {
11 (@impl $(#[$enum_meta:meta])* $vis:vis enum $name:ident($wrapper:ty, $repr:ty) {
12 $(
13 $(#[$variant_meta:meta])*
14 ($variant:ident, $value:expr, $display:literal)
15 ),* $(,)?
16 }) => {
17 $(#[$enum_meta])*
18 #[repr($repr)]
19 $vis enum $name {
20 $(
21 $(#[$variant_meta])*
22 $variant = $value
23 ),*
24 }
25
26 impl TryFrom<$repr> for $name {
27 type Error = $crate::TpmError;
28
29 #[allow(clippy::cast_lossless, clippy::cast_sign_loss)]
30 fn try_from(value: $repr) -> Result<Self, $crate::TpmError> {
31 match value {
32 $(
33 _ if value == $value => Ok(Self::$variant),
34 )*
35 _ => Err($crate::TpmError::VariantNotAvailable { offset: 0, value: value as u64 }),
36 }
37 }
38 }
39
40 impl $name {
41 #[must_use]
42 pub const fn value(self) -> $repr {
43 self as $repr
44 }
45 }
46
47 impl core::fmt::Display for $name {
48 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49 let s = match self {
50 $(Self::$variant => $display),*
51 };
52 write!(f, "{}", s)
53 }
54 }
55
56 impl $crate::TpmSized for $name {
57 const SIZE: usize = core::mem::size_of::<$repr>();
58
59 fn len(&self) -> usize {
60 Self::SIZE
61 }
62 }
63
64 impl $crate::TpmMarshal for $name {
65 fn marshal(&self, writer: &mut $crate::TpmWriter) -> $crate::TpmResult<()> {
66 let value = <$wrapper>::from(*self as $repr);
67 $crate::TpmMarshal::marshal(&value, writer)
68 }
69 }
70
71 impl<'a> $crate::TpmField<'a> for $name {
72 type View = Self;
73
74 fn cast_prefix_field(buf: &'a [u8]) -> $crate::TpmResult<(Self::View, &'a [u8])> {
75 let (value, buf) = <$wrapper as $crate::TpmCast>::cast_prefix(buf)?;
76 let raw: $repr = value.value();
77 let enum_val = Self::try_from(raw)?;
78
79 Ok((enum_val, buf))
80 }
81 }
82 };
83
84 ($(#[$meta:meta])* $vis:vis enum $name:ident(TpmUint8) { $($rest:tt)* }) => {
85 tpm_enum!(@impl $(#[$meta])* $vis enum $name($crate::basic::TpmUint8, u8) { $($rest)* });
86 };
87 ($(#[$meta:meta])* $vis:vis enum $name:ident(TpmInt8) { $($rest:tt)* }) => {
88 tpm_enum!(@impl $(#[$meta])* $vis enum $name($crate::basic::TpmInt8, i8) { $($rest)* });
89 };
90 ($(#[$meta:meta])* $vis:vis enum $name:ident(TpmUint16) { $($rest:tt)* }) => {
91 tpm_enum!(@impl $(#[$meta])* $vis enum $name($crate::basic::TpmUint16, u16) { $($rest)* });
92 };
93 ($(#[$meta:meta])* $vis:vis enum $name:ident(TpmUint32) { $($rest:tt)* }) => {
94 tpm_enum!(@impl $(#[$meta])* $vis enum $name($crate::basic::TpmUint32, u32) { $($rest)* });
95 };
96 ($(#[$meta:meta])* $vis:vis enum $name:ident(TpmInt32) { $($rest:tt)* }) => {
97 tpm_enum!(@impl $(#[$meta])* $vis enum $name($crate::basic::TpmInt32, i32) { $($rest)* });
98 };
99 ($(#[$meta:meta])* $vis:vis enum $name:ident(TpmUint64) { $($rest:tt)* }) => {
100 tpm_enum!(@impl $(#[$meta])* $vis enum $name($crate::basic::TpmUint64, u64) { $($rest)* });
101 };
102}