MCPcopy Create free account
hub / github.com/daisy/MathCAT / build_values

Function build_values

src/definitions.rs:247–281  ·  view source on GitHub ↗

Do the work of converting a single YAML def into the vec/hashset

(definition: &Yaml)

Source from the content-addressed store, hash-verified

245
246// Do the work of converting a single YAML def into the vec/hashset
247fn 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)]

Callers 2

test_read_defFunction · 0.85

Calls 4

as_hash_checkedFunction · 0.85
lenMethod · 0.80
as_strMethod · 0.80
pushMethod · 0.45

Tested by 1

test_read_defFunction · 0.68