Minify r_entry_header_insert()

This commit is contained in:
Matthias Beyer 2017-01-20 15:03:42 +01:00
parent 24c77be046
commit 7544d3e80b

View file

@ -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<Value, ()> {
match obj.value().ty() {
ValueType::Fixnum =>
Ok(Value::Integer(obj.try_convert_to::<Fixnum>().unwrap().to_i64())),
ValueType::Float =>
Ok(Value::Float(obj.try_convert_to::<Float>().unwrap().to_f64())),
ValueType::RString =>
Ok(Value::String(obj.try_convert_to::<RString>().unwrap().to_string())),
ValueType::Symbol =>
Ok(Value::String(obj.try_convert_to::<Symbol>().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::<Fixnum>().unwrap().to_i64();
do_insert(itself.get_data(&*ENTRY_HEADER_WRAPPER), &spec, Value::Integer(i))
}
ValueType::Float => {
let f = obj.try_convert_to::<Float>().unwrap().to_f64();
do_insert(itself.get_data(&*ENTRY_HEADER_WRAPPER), &spec, Value::Float(f))
}
ValueType::RString => {
let s = obj.try_convert_to::<RString>().unwrap().to_string();
do_insert(itself.get_data(&*ENTRY_HEADER_WRAPPER), &spec, Value::String(s))
}
ValueType::Symbol => {
let s = obj.try_convert_to::<Symbol>().unwrap().to_string();
do_insert(itself.get_data(&*ENTRY_HEADER_WRAPPER), &spec, Value::String(s))
}
ValueType::Array => {
let vals = obj.try_convert_to::<Array>()
.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::<Vec<Value>>();
do_insert(itself.get_data(&*ENTRY_HEADER_WRAPPER), &spec, Value::Array(vals))
}
ValueType::Hash => {
let mut btm = BTreeMap::new();
obj.try_convert_to::<Hash>()
.unwrap()
.each(|key, value| {
let key = match key.value().ty() {
ValueType::RString => obj.try_convert_to::<RString>().unwrap().to_string(),
ValueType::Symbol => obj.try_convert_to::<Symbol>().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);
}
}
}
);