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