| 1 | use proc_macro2::Span; |
| 2 | |
| 3 | use crate::create_syn_error; |
| 4 | use crate::generation::common::PANIC_ERROR_MESSAGE; |
| 5 | use crate::parsing::types::IntegerType::{ |
| 6 | Bool, I8, I16, I32, I64, I128, Isize, U8, U16, U32, U64, U128, UnknownInteger, Usize, |
| 7 | }; |
| 8 | |
| 9 | #[derive(PartialEq, Debug)] |
| 10 | pub(crate) enum IntegerType { |
| 11 | U8, |
| 12 | U16, |
| 13 | U32, |
| 14 | U64, |
| 15 | U128, |
| 16 | Usize, |
| 17 | Isize, |
| 18 | I8, |
| 19 | I16, |
| 20 | I32, |
| 21 | I64, |
| 22 | I128, |
| 23 | Bool, |
| 24 | UnknownInteger, |
| 25 | } |
| 26 | |
| 27 | const UNSIGNED_INTEGER_TYPES: [IntegerType; 6] = [U8, U16, U32, U64, U128, Usize]; |
| 28 | const SIGNED_INTEGER_TYPES: [IntegerType; 6] = [I8, I16, I32, I64, I128, Isize]; |
| 29 | const SUPPORTED_BITFIELD_TYPES: [IntegerType; 5] = [U8, U16, U32, U64, U128]; |
| 30 | const SUPPORTED_BITFIELD_FIELD_TYPES: [IntegerType; 13] = |
| 31 | [U8, U16, U32, U64, U128, Usize, I8, I16, I32, I64, I128, Isize, Bool]; |
| 32 | const SIZE_TYPES: [IntegerType; 2] = [Usize, Isize]; |
| 33 | |
| 34 | /// Returns the integer type from the type. |
| 35 | pub(crate) fn get_integer_type_from_type(ty: &syn::Type) -> IntegerType { |
| 36 | match &*get_type_ident(ty).unwrap() { |
| 37 | "u8" => U8, |
| 38 | "u16" => U16, |
| 39 | "u32" => U32, |
| 40 | "u64" => U64, |
| 41 | "u128" => U128, |
| 42 | "usize" => Usize, |
| 43 | "i8" => I8, |
| 44 | "i16" => I16, |
| 45 | "i32" => I32, |
| 46 | "i64" => I64, |
| 47 | "i128" => I128, |
| 48 | "isize" => Isize, |
| 49 | "bool" => Bool, |
| 50 | _ => UnknownInteger, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// Returns the integer suffix from the integer type. |
| 55 | pub(crate) fn get_integer_suffix_from_integer_type( |
| 56 | integer_type: IntegerType, |
| 57 | ) -> syn::Result<String> { |
| 58 | match integer_type { |
| 59 | U8 => Ok("u8".to_string()), |
| 60 | U16 => Ok("u16".to_string()), |
| 61 | U32 => Ok("u32".to_string()), |
| 62 | U64 => Ok("u64".to_string()), |
| 63 | U128 => Ok("u128".to_string()), |
| 64 | Usize => Ok("usize".to_string()), |
| 65 | I8 => Ok("i8".to_string()), |
| 66 | I16 => Ok("i16".to_string()), |
| 67 | I32 => Ok("i32".to_string()), |
| 68 | I64 => Ok("i64".to_string()), |
| 69 | I128 => Ok("i128".to_string()), |
| 70 | Isize => Ok("isize".to_string()), |
| 71 | Bool => Ok("bool".to_string()), |
| 72 | _ => Err(create_syn_error(Span::call_site(), PANIC_ERROR_MESSAGE))?, |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /// Returns if the type is a supported bitfield type. |
| 77 | pub(crate) fn is_supported_bitfield_type(ty: &syn::Type) -> bool { |
| 78 | SUPPORTED_BITFIELD_TYPES.contains(&get_integer_type_from_type(ty)) |
| 79 | } |
| 80 | |
| 81 | /// Returns if the type is a supported bitfield field type. |
| 82 | pub(crate) fn is_supported_field_type(ty: &syn::Type) -> bool { |
| 83 | is_custom_field_type(ty) |
| 84 | || SUPPORTED_BITFIELD_FIELD_TYPES.contains(&get_integer_type_from_type(ty)) |
| 85 | } |
| 86 | |
| 87 | /// Returns if the type is an unsigned integer type. |
| 88 | pub(crate) fn is_unsigned_integer_type(ty: &syn::Type) -> bool { |
| 89 | UNSIGNED_INTEGER_TYPES.contains(&get_integer_type_from_type(ty)) || is_bool_type(ty) |
| 90 | } |
| 91 | |
| 92 | /// Returns if the type is an unsigned integer type. |
| 93 | pub(crate) fn is_signed_integer_type(ty: &syn::Type) -> bool { |
| 94 | SIGNED_INTEGER_TYPES.contains(&get_integer_type_from_type(ty)) |
| 95 | } |
| 96 | |
| 97 | /// Returns if the type is an unsigned integer type. |
| 98 | pub(crate) fn is_bool_type(ty: &syn::Type) -> bool { |
| 99 | get_integer_type_from_type(ty) == Bool |
| 100 | } |
| 101 | |
| 102 | /// Returns the number of bits of the integer type. |
| 103 | pub(crate) fn get_bits_from_type(ty: &syn::Type) -> syn::Result<u8> { |
| 104 | match get_type_ident(ty).unwrap().as_str() { |
| 105 | "bool" => Ok(1), |
| 106 | "u8" | "i8" => Ok(8), |
| 107 | "u16" | "i16" => Ok(16), |
| 108 | "u32" | "i32" => Ok(32), |
| 109 | "u64" | "i64" => Ok(64), |
| 110 | "u128" | "i128" => Ok(128), |
| 111 | _ => Err(create_syn_error(Span::call_site(), PANIC_ERROR_MESSAGE))?, |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /// Returns if the type is a size type. |
| 116 | pub(crate) fn is_size_type(ty: &syn::Type) -> bool { |
| 117 | SIZE_TYPES.contains(&get_integer_type_from_type(ty)) |
| 118 | } |
| 119 | |
| 120 | /// Returns if the type is a custom field type. |
| 121 | pub(crate) fn is_custom_field_type(ty: &syn::Type) -> bool { |
| 122 | !is_unsigned_integer_type(ty) && !is_signed_integer_type(ty) && !is_bool_type(ty) |
| 123 | } |
| 124 | |
| 125 | /// Returns the identifier of the type. |
| 126 | pub(crate) fn get_type_ident(ty: &syn::Type) -> Option<String> { |
| 127 | if let syn::Type::Path(ty) = ty { |
| 128 | if let Some(segment) = ty.path.segments.first() { |
| 129 | return Some(segment.ident.to_string()); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | None |
| 134 | } |