swamp_semantic/
inst_cache.rs1use seq_fmt::comma;
7use seq_map::{SeqMap, SeqMapError};
8use swamp_types::Type;
9
10#[derive(Debug, Clone)]
12pub struct InstantiationCache {
13 pub cache: SeqMap<String, Type>,
14}
15
16impl Default for InstantiationCache {
17 fn default() -> Self {
18 Self::new()
19 }
20}
21
22impl InstantiationCache {
23 #[must_use]
24 pub fn new() -> Self {
25 Self {
26 cache: SeqMap::default(),
27 }
28 }
29
30 #[must_use]
31 pub fn complete_name(path: &[String], base_name: &str, argument_types: &[Type]) -> String {
32 format!(
33 "{}::{}<{}>",
34 path.join("::"),
35 base_name,
36 comma(argument_types)
37 )
38 }
39
40 pub fn add(
41 &mut self,
42 path: &[String],
43 name: &str,
44 ty: Type,
45 argument_type: &[Type],
46 ) -> Result<(), SeqMapError> {
47 if let Type::Blueprint(_) = ty {
48 panic!("can not add blueprint to cache");
49 }
50 let converted_name = Self::complete_name(path, name, argument_type);
51 self.cache.insert(converted_name, ty)
52 }
53
54 #[must_use]
55 pub fn is_empty(&self) -> bool {
56 self.cache.is_empty()
57 }
58
59 #[must_use]
60 pub fn get(&self, path: &[String], base_name: &str, argument_type: &[Type]) -> Option<&Type> {
61 let name = Self::complete_name(path, base_name, argument_type);
62
63 self.cache.get(&name)
64 }
65}