From ef6c40215ed01dcf77724bfe7c61cb61451287b9 Mon Sep 17 00:00:00 2001 From: Artemy Date: Mon, 29 Aug 2022 22:13:08 +0300 Subject: [PATCH] feat: toNumber conversion --- src/interpreter.rs | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/interpreter.rs b/src/interpreter.rs index c0707c9..ca7cf0b 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -325,13 +325,20 @@ impl Interpreter { self.error("Unsupported data type for the `arr` argument, must be an array"); } }, - "toString" => { - return serde_json::Value::String( - serde_json::to_string_pretty(&self.eval_node(value)) - .expect("Some error"), - ) - } - + "toString" => match value { + Value::Object(_) => { + return Value::String( + serde_json::to_string_pretty(&self.eval_node(value)) + .expect("Some error"), + ) + } + _ => { + return serde_json::Value::String( + serde_json::to_string_pretty(&value).expect("Some error"), + ) + } + }, + "toNumber" => return self.toNumber(value), "obj" => match value { Value::Object(value) => { return self.calc_obj(value); @@ -387,7 +394,25 @@ impl Interpreter { } return Value::Null; } - + fn toNumber(&mut self, value: &Value) -> Value { + match value { + Value::Object(_) => { + let value = &self.eval_node(value); + return self.toNumber(value); + } + Value::String(value) => { + return json!(value.parse::().unwrap_or_else(|_| { + self.error("Cannot be converted to a number"); + panic!() + },)) + } + Value::Bool(value) => return json!(*value as i32), + _ => { + self.error("Cannot be converted to a number"); + } + } + Value::Null + } fn import(&mut self, value: &Map) { let path = value .get("path")