swamp_types/
type_kind.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/swamp
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5
6use crate::Type;
7use crate::supporting_types::{AnonymousStructType, EnumType, NamedStructType, Signature};
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    // Primitives
16    Byte,
17    Int,
18    Float,
19    String,
20    StringStorage(Rc<Type>, usize),
21    Bool,
22    Unit, // Empty or nothing
23
24    // Aggregate type, Containers
25    Tuple(Vec<Rc<Type>>),
26    NamedStruct(NamedStructType),
27    AnonymousStruct(AnonymousStructType),
28    Range(TypeRef),
29
30    Enum(EnumType),
31
32    Function(Signature),
33
34    Optional(Rc<Type>),
35
36    // Fixed capacity `[T; N]
37    FixedCapacityAndLengthArray(Rc<Type>, usize),
38
39    // View `[T]`
40    SliceView(Rc<Type>),
41
42    // Collections
43    VecStorage(Rc<Type>, usize),
44    DynamicLengthVecView(Rc<Type>),
45
46    StackStorage(Rc<Type>, usize),
47    StackView(Rc<Type>),
48
49    QueueStorage(Rc<Type>, usize),
50    QueueView(Rc<Type>),
51
52    MapStorage(Rc<Type>, Rc<Type>, usize),
53    DynamicLengthMapView(Rc<Type>, Rc<Type>),
54
55    SparseView(Rc<Type>),
56    SparseStorage(Rc<Type>, usize),
57
58    GridStorage(Rc<Type>, usize, usize),
59    GridView(Rc<Type>),
60}
61
62impl Display for TypeKind {
63    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
64        match self {
65            Self::Byte => write!(f, "byte"),
66            Self::Int => write!(f, "int"),
67            Self::Float => write!(f, "float"),
68            Self::String => write!(f, "string"),
69            Self::StringStorage(_typeref, capacity) => write!(f, "String<{capacity}>"),
70            Self::Bool => write!(f, "bool"),
71            Self::Unit => write!(f, "()"),
72            Self::Tuple(types) => write!(f, "({})", seq_fmt::comma(types)),
73            Self::NamedStruct(named_struct) => write!(f, "{named_struct}",),
74            Self::AnonymousStruct(anon_struct) => write!(f, "{anon_struct}"),
75            Self::Range(range) => {
76                if let Self::NamedStruct(named_struct) = &*range.kind {
77                    if let Self::AnonymousStruct(anon_struct) = &*named_struct.anon_struct_type.kind
78                    {
79                        write!(f, "range{anon_struct}")
80                    } else {
81                        write!(f, "range<invalid>")
82                    }
83                } else {
84                    write!(f, "range<invalid>")
85                }
86            }
87            Self::Enum(enum_type) => write!(f, "{enum_type}"),
88            Self::Function(signature) => write!(f, "{signature}"),
89            Self::Optional(inner) => write!(f, "option<{inner}>"),
90            Self::FixedCapacityAndLengthArray(inner, size) => {
91                write!(f, "[{inner}; {size}]")
92            }
93            Self::SliceView(inner) => write!(f, "[{inner}]"),
94            Self::VecStorage(inner, capacity) => {
95                write!(f, "Vec<{inner}, {capacity}>")
96            }
97            Self::DynamicLengthVecView(inner) => write!(f, "Vec<{inner}>"),
98            Self::StackStorage(inner, capacity) => {
99                write!(f, "Stack<{inner}, {capacity}>")
100            }
101            Self::StackView(inner) => write!(f, "Stack<{inner}>"),
102            Self::QueueStorage(inner, capacity) => {
103                write!(f, "Queue<{inner}, {capacity}>")
104            }
105            Self::QueueView(inner) => write!(f, "Queue<{inner}>"),
106            Self::MapStorage(key, value, capacity) => {
107                write!(f, "Map<{key}, {value}, {capacity}>")
108            }
109            Self::DynamicLengthMapView(key, value) => {
110                write!(f, "Map<{key}, {value}>")
111            }
112            Self::SparseView(inner) => write!(f, "Sparse<{inner}>"),
113            Self::SparseStorage(inner, capacity) => {
114                write!(f, "Sparse<{inner}, {capacity}>")
115            }
116            Self::GridStorage(inner, width, height) => {
117                write!(f, "Grid<{inner}, {width}, {height}>")
118            }
119            Self::GridView(inner) => write!(f, "Grid<{inner}>"),
120        }
121    }
122}