1use crate::supporting_types::{AnonymousStructType, EnumType, NamedStructType, Signature};
7use crate::Type;
8use std::fmt::{Display, Formatter};
9use std::rc::Rc;
10
11pub type TypeRef = Rc<Type>;
12
13#[derive(Debug, Eq, PartialEq, Hash, Clone)]
14pub enum TypeKind {
15 Byte,
17 Int,
18 Codepoint,
19 Float,
20 String(Rc<Type>, Rc<Type>), StringStorage(Rc<Type>, Rc<Type>, usize), Bool,
23 Unit, Tuple(Vec<Rc<Type>>),
27 NamedStruct(NamedStructType),
28 AnonymousStruct(AnonymousStructType),
29 Range(TypeRef),
30
31 Enum(EnumType),
32
33 Function(Signature),
34
35 Optional(Rc<Type>),
36
37 FixedCapacityAndLengthArray(Rc<Type>, usize),
39
40 SliceView(Rc<Type>),
42
43 VecStorage(Rc<Type>, usize),
45 DynamicLengthVecView(Rc<Type>),
46
47 StackStorage(Rc<Type>, usize),
48 StackView(Rc<Type>),
49
50 QueueStorage(Rc<Type>, usize),
51 QueueView(Rc<Type>),
52
53 MapStorage(Rc<Type>, Rc<Type>, usize),
54 DynamicLengthMapView(Rc<Type>, Rc<Type>),
55
56 SparseView(Rc<Type>),
57 SparseStorage(Rc<Type>, usize),
58
59 GridStorage(Rc<Type>, usize, usize),
60 GridView(Rc<Type>),
61}
62
63impl Display for TypeKind {
64 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
65 match self {
66 Self::Byte => write!(f, "byte"),
67 Self::Codepoint => write!(f, "codepoint"),
68 Self::Int => write!(f, "int"),
69 Self::Float => write!(f, "float"),
70 Self::String(..) => write!(f, "string"),
71 Self::StringStorage(_, size, _) => write!(f, "string[{size}]"),
72 Self::Bool => write!(f, "bool"),
73 Self::Unit => write!(f, "()"),
74 Self::Tuple(types) => write!(f, "({})", seq_fmt::comma(types)),
75 Self::NamedStruct(named_struct) => write!(f, "{named_struct}", ),
76 Self::AnonymousStruct(anon_struct) => write!(f, "{anon_struct}"),
77 Self::Range(range) => {
78 if let Self::AnonymousStruct(anon_struct) = &*range.kind {
79 write!(f, "range{anon_struct}")
80 } else {
81 write!(f, "range<invalid>")
82 }
83 }
84 Self::Enum(enum_type) => write!(f, "{enum_type}"),
85 Self::Function(signature) => write!(f, "{signature}"),
86 Self::Optional(inner) => write!(f, "option<{inner}>"),
87 Self::FixedCapacityAndLengthArray(inner, size) => {
88 write!(f, "[{inner}; {size}]")
89 }
90 Self::SliceView(inner) => write!(f, "[{inner}]"),
91 Self::VecStorage(inner, capacity) => {
92 write!(f, "Vec<{inner}, {capacity}>")
93 }
94 Self::DynamicLengthVecView(inner) => write!(f, "Vec<{inner}>"),
95 Self::StackStorage(inner, capacity) => {
96 write!(f, "Stack<{inner}, {capacity}>")
97 }
98 Self::StackView(inner) => write!(f, "Stack<{inner}>"),
99 Self::QueueStorage(inner, capacity) => {
100 write!(f, "Queue<{inner}, {capacity}>")
101 }
102 Self::QueueView(inner) => write!(f, "Queue<{inner}>"),
103 Self::MapStorage(key, value, capacity) => {
104 write!(f, "Map<{key}, {value}, {capacity}>")
105 }
106 Self::DynamicLengthMapView(key, value) => {
107 write!(f, "Map<{key}, {value}>")
108 }
109 Self::SparseView(inner) => write!(f, "Sparse<{inner}>"),
110 Self::SparseStorage(inner, capacity) => {
111 write!(f, "Sparse<{inner}, {capacity}>")
112 }
113 Self::GridStorage(inner, width, height) => {
114 write!(f, "Grid<{inner}, {width}, {height}>")
115 }
116 Self::GridView(inner) => write!(f, "Grid<{inner}>"),
117 }
118 }
119}