use std::collections::HashMap; use std::rc::Rc; pub struct ConstantsTable { string_counter: usize, strings_to_names: HashMap, Rc>, } impl ConstantsTable { pub fn new() -> Self { Self { string_counter: 0, strings_to_names: HashMap::new(), } } pub fn get_or_insert(&mut self, s: &str) -> Rc { if self.strings_to_names.contains_key(s) { self.strings_to_names.get(s).unwrap().clone() } else { let name: Rc = Rc::from(format!("s_{}", self.string_counter).as_str()); self.string_counter += 1; self.strings_to_names.insert(s.into(), name.clone()); name } } pub fn string_constants(&self) -> HashMap, Rc> { let mut constants = HashMap::new(); self.strings_to_names.iter().for_each(|(content, name)| { constants.insert(name.clone(), content.clone()); }); constants } }