From 7544d3e80b3ba4228bee9138914da850d54a5e8f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 20 Jan 2017 15:03:42 +0100 Subject: [PATCH] Minify r_entry_header_insert() --- libimagruby/src/store.rs | 108 ++------------------------------------- 1 file changed, 5 insertions(+), 103 deletions(-) diff --git a/libimagruby/src/store.rs b/libimagruby/src/store.rs index a1e14feb..5a73b2e2 100644 --- a/libimagruby/src/store.rs +++ b/libimagruby/src/store.rs @@ -198,34 +198,7 @@ pub mod store { fn r_entry_header_insert(spec: RString, obj: AnyObject) -> Boolean { use toml::Value; use ruru::types::ValueType; - - fn do_insert(eh: &mut EntryHeader, spec: &str, v: Value) -> Boolean { - match eh.insert(spec, v) { - Ok(b) => Boolean::new(b), - Err(e) => { - VM::raise(Class::from_existing("RuntimeError"), e.description()); - return Boolean::new(false); - } - } - } - - fn to_value(obj: AnyObject) -> Result { - match obj.value().ty() { - ValueType::Fixnum => - Ok(Value::Integer(obj.try_convert_to::().unwrap().to_i64())), - - ValueType::Float => - Ok(Value::Float(obj.try_convert_to::().unwrap().to_f64())), - - ValueType::RString => - Ok(Value::String(obj.try_convert_to::().unwrap().to_string())), - - ValueType::Symbol => - Ok(Value::String(obj.try_convert_to::().unwrap().to_string())), - - _ => Err(()), - } - } + use ruby_utils::IntoToml; if let Err(ref error) = spec { // raise exception if "spec" is not a String VM::raise(error.to_exception(), error.description()); @@ -234,85 +207,14 @@ pub mod store { let spec = spec.unwrap().to_string(); // safe because of check above. let obj = obj.unwrap(); // possibly not safe... TODO - match obj.value().ty() { - ValueType::Fixnum => { - let i = obj.try_convert_to::().unwrap().to_i64(); - do_insert(itself.get_data(&*ENTRY_HEADER_WRAPPER), &spec, Value::Integer(i)) - } - ValueType::Float => { - let f = obj.try_convert_to::().unwrap().to_f64(); - do_insert(itself.get_data(&*ENTRY_HEADER_WRAPPER), &spec, Value::Float(f)) - } - - ValueType::RString => { - let s = obj.try_convert_to::().unwrap().to_string(); - do_insert(itself.get_data(&*ENTRY_HEADER_WRAPPER), &spec, Value::String(s)) - } - - ValueType::Symbol => { - let s = obj.try_convert_to::().unwrap().to_string(); - do_insert(itself.get_data(&*ENTRY_HEADER_WRAPPER), &spec, Value::String(s)) - } - - ValueType::Array => { - let vals = obj.try_convert_to::() - .unwrap() - .into_iter() - .map(to_value) - .map(|el| { - let e_class = Class::from_existing("ArgumentError"); - let err = "Arrays can only hold 'Fixnum', 'Float', 'String' or 'Symbol' in this API"; - el.map_err(|_| VM::raise(e_class, err)) - }) - .filter_map(|e| e.ok()) - .collect::>(); - - do_insert(itself.get_data(&*ENTRY_HEADER_WRAPPER), &spec, Value::Array(vals)) - } - - ValueType::Hash => { - let mut btm = BTreeMap::new(); - obj.try_convert_to::() - .unwrap() - .each(|key, value| { - let key = match key.value().ty() { - ValueType::RString => obj.try_convert_to::().unwrap().to_string(), - ValueType::Symbol => obj.try_convert_to::().unwrap().to_string(), - _ => { - let e_class = Class::from_existing("ArgumentError"); - let err = "Hash must have 'String' or 'Symbol' as Key"; - VM::raise(e_class, err); - return; // from closure - } - }; - let value = match to_value(value) { - Err(e) => { - let e_class = Class::from_existing("ArgumentError"); - let err = "Hash must have 'Fixnum', 'Float', 'String' or 'Symbol' as value in this API"; - VM::raise(e_class, err); - return; // from closure - } - Ok(v) => v, - }; - - btm.insert(key, value); - }); - - do_insert(itself.get_data(&*ENTRY_HEADER_WRAPPER), &spec, Value::Table(btm)) - } - - ValueType::Nil => { - VM::raise(Class::from_existing("ArgumentError"), "Unexpected Argument 'nil'"); - return Boolean::new(false); - } - - _ => { - VM::raise(Class::from_existing("ArgumentError"), "Unexpected Argument Type"); + match itself.get_data(&*ENTRY_HEADER_WRAPPER).insert(&spec, obj.into_toml()) { + Ok(b) => Boolean::new(b), + Err(e) => { + VM::raise(Class::from_existing("RuntimeError"), e.description()); return Boolean::new(false); } } - } );