Do the work of converting a single YAML def into the vec/hashset
(definition: &Yaml)
| 245 | |
| 246 | // Do the work of converting a single YAML def into the vec/hashset |
| 247 | fn build_values(definition: &Yaml) -> Result<()> { |
| 248 | // Rule::Definition |
| 249 | let dictionary = crate::speech::as_hash_checked(definition)?; |
| 250 | if dictionary.len()!=1 { |
| 251 | bail!("Should only be one definition rule: {}", yaml_to_type(definition)); |
| 252 | } |
| 253 | let (key, value) = dictionary.iter().next().unwrap(); |
| 254 | let name = key.as_str().ok_or_else(|| format!("definition list name '{}' is not a string", yaml_to_type(key)))?; |
| 255 | let values = value.as_vec().ok_or_else(|| format!("definition list value '{}' is not an array", yaml_to_type(value)))?; |
| 256 | |
| 257 | return DEFINITIONS.with(|definitions| { |
| 258 | let name_definition_map = &mut definitions.borrow_mut().name_to_var_mapping; |
| 259 | let collection = name_definition_map.entry(name.to_string()).or_insert_with_key(|key| { |
| 260 | if key.starts_with("Numbers") || key.ends_with("_vec") { |
| 261 | Contains::Vec( Rc::new( RefCell::new( vec![] ) ) ) |
| 262 | } else { |
| 263 | Contains::Set( Rc::new( RefCell::new( HashSet::new() ) ) ) |
| 264 | } |
| 265 | }); |
| 266 | match collection { |
| 267 | Contains::Vec(v) => v.borrow_mut().clear(), |
| 268 | Contains::Set(s) => s.borrow_mut().clear(), |
| 269 | }; |
| 270 | for yaml_value in values { |
| 271 | let value = yaml_value.as_str() |
| 272 | .ok_or_else(|| format!("list entry '{}' is not a string", yaml_to_type(yaml_value)))? |
| 273 | .to_string(); |
| 274 | match collection { |
| 275 | Contains::Vec(v) => { v.borrow_mut().push(value); }, |
| 276 | Contains::Set(s) => { s.borrow_mut().insert(value); }, |
| 277 | } |
| 278 | } |
| 279 | return Ok( () ); |
| 280 | }); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | #[cfg(test)] |