bssl_macros/lib.rs
1// Copyright 2026 The BoringSSL Authors
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// https://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
16#![no_std]
17
18#[doc(hidden)]
19#[macro_export]
20macro_rules! bssl_enum {
21 (
22 $(#[$attr:meta])*
23 $vis:vis enum $name:ident : $repr:ty {
24 $(
25 $(#[$vattr:meta])*
26 $item:ident = $e:expr
27 ),* $(,)?
28 }
29 ) => {
30 $(#[$attr])*
31 #[repr($repr)]
32 $vis enum $name {
33 $(
34 $(#[$vattr])*
35 $item = $e,
36 )*
37 }
38
39 impl ::core::convert::TryFrom<$repr> for $name {
40 type Error = $repr;
41
42 fn try_from(value: $repr) -> Result<Self, Self::Error> {
43 $(
44 #[allow(non_upper_case_globals)]
45 const $item: $repr = $e;
46 )*
47
48 #[allow(non_upper_case_globals)]
49 match value {
50 $(
51 $item => ::core::result::Result::Ok(Self::$item),
52 )*
53 _ => ::core::result::Result::Err(value),
54 }
55 }
56 }
57 };
58}