| 1 | use quote::ToTokens; |
| 2 | use syn::{Ident, Index}; |
| 3 | |
| 4 | pub(crate) enum IdentOrIndex { |
| 5 | Ident(Ident), |
| 6 | Index(Index), |
| 7 | } |
| 8 | |
| 9 | impl From<Ident> for IdentOrIndex { |
| 10 | #[inline] |
| 11 | fn from(value: Ident) -> Self { |
| 12 | Self::Ident(value) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | impl From<Index> for IdentOrIndex { |
| 17 | #[inline] |
| 18 | fn from(value: Index) -> Self { |
| 19 | Self::Index(value) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | impl From<&Ident> for IdentOrIndex { |
| 24 | #[inline] |
| 25 | fn from(value: &Ident) -> Self { |
| 26 | Self::Ident(value.clone()) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl From<usize> for IdentOrIndex { |
| 31 | #[inline] |
| 32 | fn from(value: usize) -> Self { |
| 33 | Self::Index(Index::from(value)) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl ToTokens for IdentOrIndex { |
| 38 | #[inline] |
| 39 | fn to_tokens(&self, token_stream: &mut proc_macro2::TokenStream) { |
| 40 | match self { |
| 41 | Self::Ident(ident) => ToTokens::to_tokens(ident, token_stream), |
| 42 | Self::Index(index) => ToTokens::to_tokens(index, token_stream), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl IdentOrIndex { |
| 48 | #[inline] |
| 49 | pub(crate) fn from_ident_with_index(ident: Option<&Ident>, index: usize) -> IdentOrIndex { |
| 50 | if let Some(ident) = ident { |
| 51 | Self::from(ident) |
| 52 | } else { |
| 53 | Self::from(index) |
| 54 | } |
| 55 | } |
| 56 | } |