From 114e8f1ac0bec724a39c78427607ebd452bade95 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 20 Jan 2017 15:33:56 +0100 Subject: [PATCH] Simplify ruby utils by implementing them directly on the Array and Hash --- libimagruby/src/ruby_utils.rs | 110 +++++++++++++++++++++++----------- 1 file changed, 76 insertions(+), 34 deletions(-) diff --git a/libimagruby/src/ruby_utils.rs b/libimagruby/src/ruby_utils.rs index 94b922f7..b40f684d 100644 --- a/libimagruby/src/ruby_utils.rs +++ b/libimagruby/src/ruby_utils.rs @@ -59,43 +59,15 @@ impl AsToml for AnyObject { VM::raise(rte, "Cannot translate type '' to fit into TOML"); Value::Boolean(false) }, - ValueType::Float => - Value::Float(self.try_convert_to::().unwrap().to_f64()), - ValueType::RString => - Value::String(self.try_convert_to::().unwrap().to_string()), + ValueType::Float => self.try_convert_to::().unwrap().as_toml(), + ValueType::RString => self.try_convert_to::().unwrap().as_toml(), ValueType::Regexp => { let rte = Class::from_existing("TypeError"); VM::raise(rte, "Cannot translate type '' to fit into TOML"); Value::Boolean(false) }, - ValueType::Array => { - let vals = self - .try_convert_to::() - .unwrap() - .into_iter() - .map(|v| v.as_toml()) - .collect::>(); - - Value::Array(vals) - }, - ValueType::Hash => { - let mut btm = BTreeMap::new(); - self.try_convert_to::() - .unwrap() - .each(|key, value| { - let key = match key.as_toml() { - Value::String(s) => s, - _ => { - let rte = Class::from_existing("TypeError"); - VM::raise(rte, "Can only have String or Symbol as Key for TOML maps"); - String::new() - } - }; - let value = value.as_toml(); - btm.insert(key, value); - }); - Value::Table(btm) - }, + ValueType::Array => self.try_convert_to::().unwrap().as_toml(), + ValueType::Hash => self.try_convert_to::().unwrap().as_toml(), ValueType::Struct => { let rte = Class::from_existing("TypeError"); VM::raise(rte, "Cannot translate type '' to fit into TOML"); @@ -134,8 +106,8 @@ impl AsToml for AnyObject { ValueType::Nil => Value::Boolean(false), ValueType::True => Value::Boolean(true), ValueType::False => Value::Boolean(false), - ValueType::Symbol => Value::String(self.try_convert_to::().unwrap().to_string()), - ValueType::Fixnum => Value::Integer(self.try_convert_to::().unwrap().to_i64()), + ValueType::Symbol => self.try_convert_to::().unwrap().as_toml(), + ValueType::Fixnum => self.try_convert_to::().unwrap().as_toml(), ValueType::Undef => { let rte = Class::from_existing("TypeError"); VM::raise(rte, "Cannot translate type '' to fit into TOML"); @@ -166,3 +138,73 @@ impl AsToml for AnyObject { } +impl AsToml for Hash { + + fn as_toml(&self) -> Value { + let mut btm = BTreeMap::new(); + self.try_convert_to::() + .unwrap() + .each(|key, value| { + let key = match key.as_toml() { + Value::String(s) => s, + _ => { + let rte = Class::from_existing("TypeError"); + VM::raise(rte, "Can only have String or Symbol as Key for TOML maps"); + String::new() + } + }; + let value = value.as_toml(); + btm.insert(key, value); + }); + Value::Table(btm) + } + +} + +impl AsToml for Array { + + fn as_toml(&self) -> Value { + let vals = self + .try_convert_to::() + .unwrap() + .into_iter() + .map(|v| v.as_toml()) + .collect::>(); + + Value::Array(vals) + } + +} + +impl AsToml for RString { + + fn as_toml(&self) -> Value { + Value::String(self.try_convert_to::().unwrap().to_string()) + } + +} + +impl AsToml for Float { + + fn as_toml(&self) -> Value { + Value::Float(self.try_convert_to::().unwrap().to_f64()) + } + +} + +impl AsToml for Symbol { + + fn as_toml(&self) -> Value { + Value::String(self.try_convert_to::().unwrap().to_string()) + } + +} + +impl AsToml for Fixnum { + + fn as_toml(&self) -> Value { + Value::Integer(self.try_convert_to::().unwrap().to_i64()) + } + +} +