1#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
2pub enum Shape {
3 Namespace,
4 Type,
5 Callable,
6 Value,
7 Annotation,
8 Ref,
9}
10
11impl Shape {
12 pub const ALL: &'static [Shape] = &[
13 Shape::Namespace,
14 Shape::Type,
15 Shape::Callable,
16 Shape::Value,
17 Shape::Annotation,
18 Shape::Ref,
19 ];
20
21 pub fn as_bytes(self) -> &'static [u8] {
22 match self {
23 Shape::Namespace => b"namespace",
24 Shape::Type => b"type",
25 Shape::Callable => b"callable",
26 Shape::Value => b"value",
27 Shape::Annotation => b"annotation",
28 Shape::Ref => b"ref",
29 }
30 }
31
32 pub fn as_str(self) -> &'static str {
33 match self {
34 Shape::Namespace => "namespace",
35 Shape::Type => "type",
36 Shape::Callable => "callable",
37 Shape::Value => "value",
38 Shape::Annotation => "annotation",
39 Shape::Ref => "ref",
40 }
41 }
42
43 pub fn for_kind(kind: &[u8]) -> Shape {
44 shape_of(kind).unwrap_or(Shape::Ref)
45 }
46}
47
48impl std::str::FromStr for Shape {
49 type Err = String;
50 fn from_str(s: &str) -> Result<Self, Self::Err> {
51 Self::ALL
52 .iter()
53 .copied()
54 .find(|sh| sh.as_str() == s)
55 .ok_or_else(|| format!("unknown shape `{s}`"))
56 }
57}
58
59const SHAPE_TABLE: &[(&[u8], Shape, bool)] = &[
60 (b"module", Shape::Namespace, true),
61 (b"namespace", Shape::Namespace, true),
62 (b"schema", Shape::Namespace, true),
63 (b"impl", Shape::Namespace, true),
64 (b"class", Shape::Type, true),
65 (b"struct", Shape::Type, true),
66 (b"interface", Shape::Type, true),
67 (b"trait", Shape::Type, true),
68 (b"enum", Shape::Type, true),
69 (b"record", Shape::Type, true),
70 (b"annotation_type", Shape::Type, true),
71 (b"table", Shape::Type, true),
72 (b"type", Shape::Type, false),
73 (b"view", Shape::Type, false),
74 (b"delegate", Shape::Type, false),
75 (b"function", Shape::Callable, true),
76 (b"method", Shape::Callable, true),
77 (b"constructor", Shape::Callable, true),
78 (b"fn", Shape::Callable, true),
79 (b"func", Shape::Callable, true),
80 (b"macro", Shape::Callable, true),
81 (b"procedure", Shape::Callable, true),
82 (b"async_function", Shape::Callable, true),
83 (b"test", Shape::Callable, true),
84 (b"field", Shape::Value, false),
85 (b"property", Shape::Value, false),
86 (b"event", Shape::Value, false),
87 (b"enum_constant", Shape::Value, false),
88 (b"const", Shape::Value, false),
89 (b"static", Shape::Value, false),
90 (b"path", Shape::Value, false),
91 (b"var", Shape::Value, false),
92 (b"param", Shape::Value, false),
93 (b"local", Shape::Value, false),
94 (b"comment", Shape::Annotation, false),
95];
96
97pub fn shape_of(kind: &[u8]) -> Option<Shape> {
98 SHAPE_TABLE
99 .iter()
100 .find(|(k, _, _)| *k == kind)
101 .map(|(_, s, _)| *s)
102}
103
104pub fn opens_scope(kind: &[u8]) -> bool {
105 SHAPE_TABLE
106 .iter()
107 .find(|(k, _, _)| *k == kind)
108 .is_some_and(|(_, _, opens)| *opens)
109}
110
111pub fn known_kinds() -> impl Iterator<Item = &'static [u8]> {
112 SHAPE_TABLE.iter().map(|(k, _, _)| *k)
113}
114
115#[cfg(test)]
116mod tests {
117 use super::*;
118
119 #[test]
120 fn shape_table_has_no_duplicate_kind() {
121 let mut seen = std::collections::HashSet::new();
122 for (k, _, _) in SHAPE_TABLE {
123 assert!(seen.insert(*k), "duplicate kind in SHAPE_TABLE: {k:?}");
124 }
125 }
126
127 #[test]
128 fn unknown_kind_has_no_shape() {
129 assert!(shape_of(b"definitely_not_a_kind").is_none());
130 assert!(!opens_scope(b"definitely_not_a_kind"));
131 }
132
133 #[test]
134 fn internal_kinds_are_classified() {
135 assert_eq!(shape_of(b"module"), Some(Shape::Namespace));
136 assert_eq!(shape_of(b"comment"), Some(Shape::Annotation));
137 assert_eq!(shape_of(b"local"), Some(Shape::Value));
138 assert_eq!(shape_of(b"param"), Some(Shape::Value));
139 }
140
141 #[test]
142 fn comment_is_the_only_annotation() {
143 let annotations: Vec<_> = SHAPE_TABLE
144 .iter()
145 .filter(|(_, s, _)| *s == Shape::Annotation)
146 .map(|(k, _, _)| *k)
147 .collect();
148 assert_eq!(annotations, vec![b"comment".as_slice()]);
149 }
150
151 #[test]
152 fn annotation_never_opens_scope() {
153 for (_, shape, opens) in SHAPE_TABLE {
154 if *shape == Shape::Annotation {
155 assert!(!opens, "annotation kind must not open a scope");
156 }
157 }
158 }
159
160 #[test]
161 fn values_never_open_scope() {
162 for (k, shape, opens) in SHAPE_TABLE {
163 if *shape == Shape::Value {
164 assert!(!opens, "value kind {k:?} must not open a scope");
165 }
166 }
167 }
168
169 #[test]
170 fn callables_always_open_scope() {
171 for (k, shape, opens) in SHAPE_TABLE {
172 if *shape == Shape::Callable {
173 assert!(*opens, "callable kind {k:?} must open a scope");
174 }
175 }
176 }
177
178 #[test]
179 fn namespaces_always_open_scope() {
180 for (k, shape, opens) in SHAPE_TABLE {
181 if *shape == Shape::Namespace {
182 assert!(*opens, "namespace kind {k:?} must open a scope");
183 }
184 }
185 }
186
187 #[test]
188 fn type_containers_open_scope_aliases_do_not() {
189 let containers: &[&[u8]] = &[
190 b"class",
191 b"struct",
192 b"interface",
193 b"trait",
194 b"enum",
195 b"record",
196 b"annotation_type",
197 b"table",
198 ];
199 let aliases: &[&[u8]] = &[b"type", b"view", b"delegate"];
200 for k in containers {
201 assert!(opens_scope(k), "type container {k:?} must open a scope");
202 }
203 for k in aliases {
204 assert!(!opens_scope(k), "type alias {k:?} must not open a scope");
205 }
206 }
207
208 #[test]
209 fn shape_str_round_trip_is_lowercase_word() {
210 for shape in [
211 Shape::Namespace,
212 Shape::Type,
213 Shape::Callable,
214 Shape::Value,
215 Shape::Annotation,
216 ] {
217 let s = shape.as_str();
218 assert!(s.chars().all(|c| c.is_ascii_lowercase()));
219 assert!(!s.is_empty());
220 }
221 }
222}