1 // Copyright 2023 The Android Open Source Project
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 use std::convert::TryFrom;
16 
17 use crate::packets::{hci, lmp};
18 
19 pub enum Either<L, R> {
20     Left(L),
21     Right(R),
22 }
23 
24 macro_rules! impl_try_from {
25     ($T: path) => {
26         impl<L, R> TryFrom<$T> for Either<L, R>
27         where
28             L: TryFrom<$T>,
29             R: TryFrom<$T>,
30         {
31             type Error = ();
32 
33             fn try_from(value: $T) -> Result<Self, Self::Error> {
34                 let left = L::try_from(value.clone());
35                 if let Ok(left) = left {
36                     return Ok(Either::Left(left));
37                 }
38                 let right = R::try_from(value);
39                 if let Ok(right) = right {
40                     return Ok(Either::Right(right));
41                 }
42                 Err(())
43             }
44         }
45     };
46 }
47 
48 impl_try_from!(lmp::LmpPacket);
49 impl_try_from!(hci::Command);
50