enum_ordinalize_derive/
variant_type.rs

1use proc_macro2::{Ident, Span, TokenStream};
2use quote::{ToTokens, TokenStreamExt};
3
4#[derive(Debug)]
5pub(crate) enum VariantType {
6    ISize,
7    I8,
8    I16,
9    I32,
10    I64,
11    I128,
12    USize,
13    U8,
14    U16,
15    U32,
16    U64,
17    U128,
18    NonDetermined,
19}
20
21impl VariantType {
22    #[inline]
23    pub(crate) fn from_str<S: AsRef<str>>(s: S) -> VariantType {
24        let s = s.as_ref();
25
26        match s {
27            "i8" => VariantType::I8,
28            "i16" => VariantType::I16,
29            "i32" => VariantType::I32,
30            "i64" => VariantType::I64,
31            "i128" => VariantType::I128,
32            "isize" => VariantType::ISize,
33            "u8" => VariantType::U8,
34            "u16" => VariantType::U16,
35            "u32" => VariantType::U32,
36            "u64" => VariantType::U64,
37            "u128" => VariantType::U128,
38            "usize" => VariantType::USize,
39            _ => VariantType::NonDetermined,
40        }
41    }
42
43    #[inline]
44    pub(crate) fn as_str(&self) -> &'static str {
45        match self {
46            VariantType::ISize => "isize",
47            VariantType::I8 => "i8",
48            VariantType::I16 => "i16",
49            VariantType::I32 => "i32",
50            VariantType::I64 => "i64",
51            VariantType::I128 => "i128",
52            VariantType::USize => "usize",
53            VariantType::U8 => "u8",
54            VariantType::U16 => "u16",
55            VariantType::U32 => "u32",
56            VariantType::U64 => "u64",
57            VariantType::U128 => "u128",
58            _ => unreachable!(),
59        }
60    }
61}
62
63impl Default for VariantType {
64    #[inline]
65    fn default() -> Self {
66        VariantType::NonDetermined
67    }
68}
69
70impl ToTokens for VariantType {
71    #[inline]
72    fn to_tokens(&self, tokens: &mut TokenStream) {
73        tokens.append(Ident::new(self.as_str(), Span::call_site()));
74    }
75}