• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 /// Create a struct holding a `bool`.
16 macro_rules! boolean_element_struct {
17     ($type_name:ident) => {
18         #[derive(Debug)]
19         #[allow(missing_docs)]
20         pub struct $type_name {
21             enabled: bool,
22         }
23     };
24 }
25 
26 /// Create a `From<bool>` impl for a struct made with [boolean_element_struct].
27 macro_rules! boolean_element_struct_from_bool {
28     ($type_name:ident) => {
29         impl From<bool> for $type_name {
30             fn from(value: bool) -> Self {
31                 $type_name { enabled: value }
32             }
33         }
34     };
35 }
36 
37 /// The guts of an ActionElement impl
38 macro_rules! boolean_element_action_element_impl_shared {
39     ($type_name:ident, $index:expr) => {
40         const HIGH_BIT_INDEX: u32 = $index;
41         const ACTION_TYPE: $crate::legacy::data_elements::actions::ActionType =
42             $crate::legacy::data_elements::actions::ActionType::$type_name;
43     };
44 }
45 
46 /// Create a struct w/ `From<bool>` and [`ActionElement`](super::ActionElement) impls.
47 /// Use `plaintext_only`, `ciphertext_only`, or `plaintext_and_ciphertext` to create appropriate
48 /// impls.
49 macro_rules! boolean_element {
50     ($type_name:ident, $index:expr, ciphertext_only) => {
51         $crate::legacy::data_elements::actions::macros::boolean_element_struct!($type_name);
52         $crate::legacy::data_elements::actions::macros::boolean_element_struct_from_bool!($type_name);
53 
54         impl $crate::legacy::data_elements::actions::ActionElement for $type_name {
55             $crate::legacy::data_elements::actions::macros::boolean_element_action_element_impl_shared!(
56                 $type_name, $index
57             );
58 
59             fn supports_flavor(flavor: $crate::legacy::PacketFlavorEnum) -> bool {
60                 match flavor {
61                     $crate::legacy::PacketFlavorEnum::Plaintext => false,
62                     $crate::legacy::PacketFlavorEnum::Ciphertext => true,
63                 }
64             }
65 
66             fn bits(&self) -> u8 {
67                 self.enabled as u8
68             }
69         }
70 
71         $crate::legacy::data_elements::actions::macros::boolean_element_to_encrypted_element!($type_name);
72     };
73     ($type_name:ident, $index:expr, plaintext_and_ciphertext) => {
74         $crate::legacy::data_elements::actions::macros::boolean_element_struct!($type_name);
75         $crate::legacy::data_elements::actions::macros::boolean_element_struct_from_bool!($type_name);
76 
77         impl $crate::legacy::data_elements::actions::ActionElement for $type_name {
78             $crate::legacy::data_elements::actions::macros::boolean_element_action_element_impl_shared!(
79                 $type_name, $index
80             );
81 
82             fn supports_flavor(flavor: $crate::legacy::PacketFlavorEnum) -> bool {
83                 match flavor {
84                     $crate::legacy::PacketFlavorEnum::Plaintext => true,
85                     $crate::legacy::PacketFlavorEnum::Ciphertext => true,
86                 }
87             }
88 
89             fn bits(&self) -> u8 {
90                 self.enabled as u8
91             }
92         }
93 
94         $crate::legacy::data_elements::actions::macros::boolean_element_to_plaintext_element!($type_name);
95         $crate::legacy::data_elements::actions::macros::boolean_element_to_encrypted_element!($type_name);
96     };
97 }
98 
99 /// Create a [`ToActionElement<Encrypted>`](super::ActionElementFlavor) impl with the given index and length 1.
100 macro_rules! boolean_element_to_encrypted_element {
101     ( $type_name:ident) => {
102         impl $crate::legacy::data_elements::actions::ActionElementFlavor<$crate::legacy::Ciphertext>
103             for $type_name
104         {
105         }
106     };
107 }
108 
109 /// Create a [`ToActionElement<Plaintext>`](super::ActionElementFlavor) impl with the given index and length 1.
110 macro_rules! boolean_element_to_plaintext_element {
111     ( $type_name:ident) => {
112         impl $crate::legacy::data_elements::actions::ActionElementFlavor<$crate::legacy::Plaintext>
113             for $type_name
114         {
115         }
116     };
117 }
118 
119 // expose macros to the rest of the crate without macro_export's behavior of exporting at crate root
120 pub(crate) use {
121     boolean_element, boolean_element_action_element_impl_shared, boolean_element_struct,
122     boolean_element_struct_from_bool, boolean_element_to_encrypted_element,
123     boolean_element_to_plaintext_element,
124 };
125