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    Short,
18    Int,
19    Codepoint,
20    Float,
21    StringView(Rc<Type>, Rc<Type>), // basically an "alias" for Vec<Byte>
22    StringStorage(Rc<Type>, Rc<Type>, usize), // basically an "alias" for Vec<Byte; N>
23    Bool,
24    Unit, // Empty or nothing
25
26    Pointer(Rc<Type>),
27
28    // Aggregate type, Containers
29    Tuple(Vec<Rc<Type>>),
30    NamedStruct(NamedStructType),
31    AnonymousStruct(AnonymousStructType),
32    Range(TypeRef),
33
34    Enum(EnumType),
35
36    Function(Signature),
37
38    Optional(Rc<Type>),
39
40    // Fixed capacity `[T; N]
41    FixedCapacityAndLengthArray(Rc<Type>, usize),
42
43    // View `[T]`
44    SliceView(Rc<Type>),
45
46    // Collections
47    VecStorage(Rc<Type>, usize),
48    DynamicLengthVecView(Rc<Type>),
49
50    StackStorage(Rc<Type>, usize),
51    StackView(Rc<Type>),
52
53    QueueStorage(Rc<Type>, usize),
54    QueueView(Rc<Type>),
55
56    MapStorage(Rc<Type>, Rc<Type>, usize),
57    DynamicLengthMapView(Rc<Type>, Rc<Type>),
58
59    SparseView(Rc<Type>),
60    SparseStorage(Rc<Type>, usize),
61
62    GridStorage(Rc<Type>, usize, usize),
63    GridView(Rc<Type>),
64
65    Any,
66    Never,
67}
68
69impl Display for TypeKind {
70    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
71        match self {
72            Self::Never => write!(f, "!"),
73            Self::Pointer(inner) => write!(f, "Ptr<inner>"),
74            Self::Any => write!(f, "Any"),
75            Self::Byte => write!(f, "byte"),
76            Self::Short => write!(f, "short"),
77            Self::Codepoint => write!(f, "codepoint"),
78            Self::Int => write!(f, "int"),
79            Self::Float => write!(f, "float"),
80            Self::StringView(..) => write!(f, "string"),
81            Self::StringStorage(_, size, _) => write!(f, "string[{size}]"),
82            Self::Bool => write!(f, "bool"),
83            Self::Unit => write!(f, "()"),
84            Self::Tuple(types) => write!(f, "({})", seq_fmt::comma(types)),
85            Self::NamedStruct(named_struct) => write!(f, "{named_struct}",),
86            Self::AnonymousStruct(anon_struct) => write!(f, "{anon_struct}"),
87            Self::Range(range) => {
88                if let Self::AnonymousStruct(anon_struct) = &*range.kind {
89                    write!(f, "range{anon_struct}")
90                } else {
91                    write!(f, "range<invalid>")
92                }
93            }
94            Self::Enum(enum_type) => write!(f, "{enum_type}"),
95            Self::Function(signature) => write!(f, "{signature}"),
96            Self::Optional(inner) => write!(f, "option<{inner}>"),
97            Self::FixedCapacityAndLengthArray(inner, size) => {
98                write!(f, "[{inner}; {size}]")
99            }
100            Self::SliceView(inner) => write!(f, "[{inner}]"),
101            Self::VecStorage(inner, capacity) => {
102                write!(f, "Vec<{inner}, {capacity}>")
103            }
104            Self::DynamicLengthVecView(inner) => write!(f, "Vec<{inner}>"),
105            Self::StackStorage(inner, capacity) => {
106                write!(f, "Stack<{inner}, {capacity}>")
107            }
108            Self::StackView(inner) => write!(f, "Stack<{inner}>"),
109            Self::QueueStorage(inner, capacity) => {
110                write!(f, "Queue<{inner}, {capacity}>")
111            }
112            Self::QueueView(inner) => write!(f, "Queue<{inner}>"),
113            Self::MapStorage(key, value, capacity) => {
114                write!(f, "Map<{key}, {value}, {capacity}>")
115            }
116            Self::DynamicLengthMapView(key, value) => {
117                write!(f, "Map<{key}, {value}>")
118            }
119            Self::SparseView(inner) => write!(f, "Sparse<{inner}>"),
120            Self::SparseStorage(inner, capacity) => {
121                write!(f, "Sparse<{inner}, {capacity}>")
122            }
123            Self::GridStorage(inner, width, height) => {
124                write!(f, "Grid<{inner}, {width}, {height}>")
125            }
126            Self::GridView(inner) => write!(f, "Grid<{inner}>"),
127        }
128    }
129}