swamp_types/
type_kind.rs

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