| 1 | use std::{ops::Range, str::FromStr}; |
| 2 | |
| 3 | #[derive (Clone, Debug, Default, Eq, PartialEq)] |
| 4 | pub enum Type { |
| 5 | BitField(Range<u8>), |
| 6 | Bool, |
| 7 | /// A single Unicode character |
| 8 | Char, |
| 9 | |
| 10 | Debug, |
| 11 | Display, |
| 12 | FormatSequence, |
| 13 | |
| 14 | F32, |
| 15 | F64, |
| 16 | |
| 17 | /// `{=?}` OR `{}` |
| 18 | #[default] // when not specified in the format string, this type is assumed |
| 19 | Format, |
| 20 | FormatArray(usize), // FIXME: This `usize` is not the target's `usize`; use `u64` instead? |
| 21 | /// `{=[?]}` |
| 22 | FormatSlice, |
| 23 | |
| 24 | I8, |
| 25 | I16, |
| 26 | I32, |
| 27 | I64, |
| 28 | I128, |
| 29 | Isize, |
| 30 | |
| 31 | /// Interned string index. |
| 32 | IStr, |
| 33 | /// String slice (i.e. passed directly; not as interned string indices). |
| 34 | Str, |
| 35 | |
| 36 | U8, |
| 37 | U16, |
| 38 | U32, |
| 39 | U64, |
| 40 | U128, |
| 41 | Usize, |
| 42 | |
| 43 | /// Byte slice `{=[u8]}`. |
| 44 | U8Slice, |
| 45 | U8Array(usize), // FIXME: This `usize` is not the target's `usize`; use `u64` instead? |
| 46 | } |
| 47 | |
| 48 | // FIXME: either all or none of the type parsing should be done in here |
| 49 | impl FromStr for Type { |
| 50 | type Err = (); |
| 51 | |
| 52 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 53 | Ok(match s { |
| 54 | "u8" => Type::U8, |
| 55 | "u16" => Type::U16, |
| 56 | "u32" => Type::U32, |
| 57 | "u64" => Type::U64, |
| 58 | "u128" => Type::U128, |
| 59 | "usize" => Type::Usize, |
| 60 | "i8" => Type::I8, |
| 61 | "i16" => Type::I16, |
| 62 | "i32" => Type::I32, |
| 63 | "i64" => Type::I64, |
| 64 | "i128" => Type::I128, |
| 65 | "isize" => Type::Isize, |
| 66 | "f32" => Type::F32, |
| 67 | "f64" => Type::F64, |
| 68 | "bool" => Type::Bool, |
| 69 | "str" => Type::Str, |
| 70 | "istr" => Type::IStr, |
| 71 | "__internal_Debug" => Type::Debug, |
| 72 | "__internal_Display" => Type::Display, |
| 73 | "__internal_FormatSequence" => Type::FormatSequence, |
| 74 | "[u8]" => Type::U8Slice, |
| 75 | "?" => Type::Format, |
| 76 | "[?]" => Type::FormatSlice, |
| 77 | "char" => Type::Char, |
| 78 | _ => return Err(()), |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |