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::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    // Primitives
16    Byte,
17    Int,
18    Codepoint,
19    Float,
20    String(Rc<Type>, Rc<Type>), // basically an "alias" for Vec<Byte>
21    StringStorage(Rc<Type>, Rc<Type>, usize), // basically an "alias" for Vec<Byte; N>
22    Bool,
23    Unit, // Empty or nothing
24
25    // Aggregate type, Containers
26    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    // Fixed capacity `[T; N]
38    FixedCapacityAndLengthArray(Rc<Type>, usize),
39
40    // View `[T]`
41    SliceView(Rc<Type>),
42
43    // Collections
44    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    Any,
63}
64
65impl Display for TypeKind {
66    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
67        match self {
68            Self::Any => write!(f, "Any"),
69            Self::Byte => write!(f, "byte"),
70            Self::Codepoint => write!(f, "codepoint"),
71            Self::Int => write!(f, "int"),
72            Self::Float => write!(f, "float"),
73            Self::String(..) => write!(f, "string"),
74            Self::StringStorage(_, size, _) => write!(f, "string[{size}]"),
75            Self::Bool => write!(f, "bool"),
76            Self::Unit => write!(f, "()"),
77            Self::Tuple(types) => write!(f, "({})", seq_fmt::comma(types)),
78            Self::NamedStruct(named_struct) => write!(f, "{named_struct}", ),
79            Self::AnonymousStruct(anon_struct) => write!(f, "{anon_struct}"),
80            Self::Range(range) => {
81                if let Self::AnonymousStruct(anon_struct) = &*range.kind {
82                    write!(f, "range{anon_struct}")
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}