1use std::collections::BTreeMap;
2
3#[derive(Debug, Clone)]
6pub enum Type {
7 Bool,
8 U8,
9 U16,
10 U32,
11 U64,
12 I8,
13 I16,
14 I32,
15 I64,
16 F32,
17 F64,
18 Enumeration {
19 ty: Box<Type>,
20 valid_values: BTreeMap<i128, String>,
22 },
23 Structure {
24 members: Vec<StructureMember>,
25 size: usize,
26 },
27 Pointer(Box<Type>),
28 Array {
29 ty: Box<Type>,
30 lengths: Vec<u64>,
31 },
32}
33
34#[derive(Debug, Clone)]
35pub struct StructureMember {
36 pub offset: u64,
37 pub name: String,
38 pub ty: Type,
39}
40
41impl Type {
42 pub fn size(&self) -> usize {
43 match self {
44 Type::Bool => 1,
45 Type::U8 => 1,
46 Type::U16 => 2,
47 Type::U32 => 4,
48 Type::U64 => 8,
49 Type::I8 => 1,
50 Type::I16 => 2,
51 Type::I32 => 4,
52 Type::I64 => 8,
53 Type::F32 => 4,
54 Type::F64 => 8,
55 Type::Enumeration { ty, .. } => ty.size(),
56 Type::Structure { size, .. } => *size,
57 Type::Pointer(ty) => ty.size(),
58 Type::Array { ty, lengths } => {
59 if lengths.is_empty() {
60 return 0;
61 }
62
63 ty.size() * (lengths.iter().product::<u64>() as usize)
64 }
65 }
66 }
67}