Skip to main content

code_moniker_core/core/
shape.rs

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