1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! Source: `Analysis/src/TypePath.cpp:742-967` (hand-ported)
use crate::enums::pack_field::PackField;
use crate::enums::type_field::TypeField;
use crate::enums::variant::Variant;
use crate::functions::to_human_readable_index::to_human_readable_index;
use crate::records::path::Path;
use crate::type_aliases::component::Component;
use alloc::string::String;
use core::fmt::Write as _;
#[derive(Clone, Copy, PartialEq, Eq)]
enum State {
Initial,
Normal,
Property,
PendingIs,
PendingAs,
PendingWhich,
}
pub fn to_string_human(path: &Path) -> String {
let mut result = String::new();
let mut state = State::Initial;
let mut last = false;
let mut count: usize = 0;
for component in &path.components {
count += 1;
if count == path.components.len() {
last = true;
}
match component {
Component::Property(c) => {
if state == State::PendingIs {
result.push_str(", ");
}
match state {
State::Initial | State::PendingIs => {
if c.is_read {
result.push_str("accessing `");
} else {
result.push_str("writing to `");
}
}
State::Property => {
// if the previous state was a property, then we're doing
// a sequence of indexing
result.push('.');
}
_ => {}
}
result.push_str(&c.name);
state = State::Property;
}
Component::Index(c) => {
if state == State::Initial && !last {
result.push_str("in ");
} else if state == State::PendingIs {
result.push_str(" has ");
} else if state == State::Property {
result.push_str("` has ");
}
let _ = write!(result, "the {}", to_human_readable_index(c.index));
match c.variant {
Variant::Pack => result.push_str(" entry in the type pack"),
Variant::Union => result.push_str(" component of the union"),
Variant::Intersection => result.push_str(" component of the intersection"),
}
if state == State::PendingWhich {
result.push_str(" which");
}
if state == State::PendingIs || state == State::Property {
state = State::PendingAs;
} else {
state = State::PendingIs;
}
}
Component::TypeField(c) => {
if state == State::Initial && !last {
result.push_str("in ");
} else if state == State::PendingIs {
result.push_str(", ");
} else if state == State::Property {
result.push_str("` has ");
}
match c {
TypeField::Table => {
result.push_str("the table portion");
if state == State::Property {
state = State::PendingAs;
} else {
state = State::PendingIs;
}
}
TypeField::Metatable => {
result.push_str("the metatable portion");
if state == State::Property {
state = State::PendingAs;
} else {
state = State::PendingIs;
}
}
TypeField::LowerBound => {
result.push_str("the lower bound of ");
state = State::Normal;
}
TypeField::UpperBound => {
result.push_str("the upper bound of ");
state = State::Normal;
}
TypeField::IndexLookup => {
result.push_str("the index type");
if state == State::Property {
state = State::PendingAs;
} else {
state = State::PendingIs;
}
}
TypeField::IndexResult => {
result.push_str("the result of indexing");
if state == State::Property {
state = State::PendingAs;
} else {
state = State::PendingIs;
}
}
TypeField::Negated => {
result.push_str("the negation ");
state = State::Normal;
}
TypeField::Variadic => {
result.push_str("the variadic ");
state = State::Normal;
}
}
}
Component::PackField(c) => {
if state == State::PendingIs {
result.push_str(", ");
} else if state == State::Property {
result.push_str("`, ");
}
match c {
PackField::Arguments => {
if state == State::Initial {
result.push_str("it ");
} else if state == State::PendingIs {
result.push_str("the function ");
}
result.push_str("takes");
}
PackField::Returns => {
if state == State::Initial {
result.push_str("it ");
} else if state == State::PendingIs {
result.push_str("the function ");
}
result.push_str("returns");
}
PackField::Tail => {
if state == State::Initial {
result.push_str("it has ");
}
result.push_str("a tail of");
}
}
if state == State::PendingIs {
result.push(' ');
state = State::PendingWhich;
} else {
result.push(' ');
state = State::Normal;
}
}
Component::PackSlice(c) => {
let _ = write!(
result,
"the portion of the type pack starting at index {} to the end",
c.start_index
);
}
Component::Reduction(_c) => {
if state == State::Initial {
result.push_str("it ");
}
result.push_str("reduces to ");
state = State::Normal;
}
Component::GenericPackMapping(_c) => {
result.push_str("is a generic pack mapped to ");
}
}
}
match state {
State::Property => {
result.push_str("` results in ");
}
State::PendingWhich => {
// pending `which` becomes `is` if it's at the end
result.push_str("is ");
}
State::PendingIs => {
result.push_str(" is ");
}
State::PendingAs => {
result.push_str(" as ");
}
_ => {}
}
result
}