From bfa83b6fab9224f6a3a91a7526c7805e2cbcddac Mon Sep 17 00:00:00 2001 From: mgros Date: Fri, 25 Sep 2026 03:42:38 +0200 Subject: [PATCH] clean up unused types in definitions and remove outdated docstring --- src/definitions.rs | 53 ++++++++++------------------------------------ 1 file changed, 11 insertions(+), 42 deletions(-) diff --git a/src/definitions.rs b/src/definitions.rs index b7c45fca9..33b833912 100644 --- a/src/definitions.rs +++ b/src/definitions.rs @@ -33,44 +33,13 @@ use std::path::{Path, PathBuf}; use std::collections::{HashMap, HashSet}; use crate::shim_filesystem::read_to_string_shim; -/// An enum to paper over the different types of data access needed. -/// -/// Having a Rc> seems a bit complicated in terms of types but... -/// 1. The rust book seems to endorse the Rc>> approach when there are multiple owners of mutable date. -/// See towards the end -/// 2. When a file is read, we need to clear and add data to the structure being read (reassigning could work for clearing). -/// When we use the data, we either want to index into it or test if an item is there. -/// The structures we use are either a Vec or a HashMap, so we need to abstract that away in `FromFileVariable`. -/// Unfortunately, traits don't quite work as an option here: -/// * Vec implements extends (`add`), but there is no test/contains -/// * Hashmap implements `index`, but panics if the item isn't there -/// -/// Because of the above limitations, we introduce the enum [`Contains`] which dispatches appropriately to Vec/Hashmap #[derive(Debug, Clone)] -pub enum Contains { +pub enum DefinitionCollection { Vec(Rc>>), Set(Rc>>), Map(Rc>>), } -impl Contains { - // fn add(&mut self, item: String) { - // match self { - // Contains::Vec(v) => { v.borrow_mut().push(item); }, - // Contains::Set(s) => { s.borrow_mut().insert(item); } - // } - // } - - // fn clear(&mut self) { - // match self { - // Contains::Vec(v) => { v.borrow_mut().clear(); }, - // Contains::Set(s) => { s.borrow_mut().clear(); } - // } - // } -} -pub type CollectionFromFile = Contains; -type VariableDefHashMap = HashMap; - /// Global structure containing all of the definitions. /// Each field in the structure corresponds to a named value read in from the `definitions.yaml` files. /// @@ -80,7 +49,7 @@ type VariableDefHashMap = HashMap; /// There should only be one instance of this structure ([`DEFINITIONS`]) // FIX: this probably can done with a macro to remove all the repetition pub struct Definitions { - pub name_to_var_mapping: VariableDefHashMap, + pub name_to_var_mapping: HashMap, } impl Default for Definitions { @@ -100,7 +69,7 @@ impl Definitions { pub fn get_hashset(&self, name: &str) -> Option>> { let names = self.name_to_var_mapping.get(name); - if let Some(Contains::Set(set)) = names { + if let Some(DefinitionCollection::Set(set)) = names { return Some(set.borrow()); } return None; @@ -108,7 +77,7 @@ impl Definitions { pub fn get_hashmap(&self, name: &str) -> Option>> { let names = self.name_to_var_mapping.get(name); - if let Some(Contains::Map(map)) = names { + if let Some(DefinitionCollection::Map(map)) = names { return Some(map.borrow()); } return None; @@ -116,7 +85,7 @@ impl Definitions { pub fn get_vec(&self, name: &str) -> Option>> { let names = self.name_to_var_mapping.get(name); - if let Some(Contains::Vec(vec)) = names { + if let Some(DefinitionCollection::Vec(vec)) = names { return Some(vec.borrow()); } return None; @@ -172,12 +141,12 @@ pub fn read_definitions_file(use_speech_defs: bool) -> Result> { // let name_to_mapping = defs.name_to_var_mapping.borrow_mut(); for set_name in used_set_names { if defs.get_hashset(set_name).is_none() { - defs.name_to_var_mapping.insert(set_name.to_string(), Contains::Set( Rc::new( RefCell::new( HashSet::with_capacity(0) ) ) )); + defs.name_to_var_mapping.insert(set_name.to_string(), DefinitionCollection::Set( Rc::new( RefCell::new( HashSet::with_capacity(0) ) ) )); } } if defs.get_hashset("FunctionNames").is_none() { let all_functions = build_all_functions_set(defs); - defs.name_to_var_mapping.insert("FunctionNames".to_string(), Contains::Set( Rc::new( RefCell::new( all_functions ) ) )); + defs.name_to_var_mapping.insert("FunctionNames".to_string(), DefinitionCollection::Set( Rc::new( RefCell::new( all_functions ) ) )); } } @@ -240,19 +209,19 @@ fn build_values(definition: &Yaml, use_speech_defs: bool, path: &Path) -> Result } let result = if let Some(vec) = value.as_vec() { - Contains::Vec( Rc::new( RefCell::new( get_vec_values(vec)? ) ) ) + DefinitionCollection::Vec( Rc::new( RefCell::new( get_vec_values(vec)? ) ) ) } else { let dict = value.as_hash().ok_or_else(|| anyhow!("definition list value '{}' is not an array or dictionary", yaml_to_type(value)))?; if dict.is_empty() { - Contains::Set( Rc::new( RefCell::new( HashSet::with_capacity(0) ) ) ) + DefinitionCollection::Set( Rc::new( RefCell::new( HashSet::with_capacity(0) ) ) ) } else { // peak and see if this is a set or a map let (_, entry_value) = dict.iter().next().unwrap(); if entry_value.is_null() { - Contains::Set( Rc::new( RefCell::new( get_set_values(dict) + DefinitionCollection::Set( Rc::new( RefCell::new( get_set_values(dict) .with_context(||format!("while reading value '{def_name}'"))? ) ) ) } else { - Contains::Map( Rc::new( RefCell::new( get_map_values(dict) + DefinitionCollection::Map( Rc::new( RefCell::new( get_map_values(dict) .with_context(||format!("while reading value '{def_name}'"))? ) ) ) } }