From 663848172eb351bebe2e4fbd671aade0b29e0036 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sun, 3 Jul 2022 20:30:18 +0100 Subject: [PATCH] Simplify code for conversion between Boa and SurrealDB --- lib/src/fnc/script.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/src/fnc/script.rs b/lib/src/fnc/script.rs index 5873d7b5..bb956ee9 100644 --- a/lib/src/fnc/script.rs +++ b/lib/src/fnc/script.rs @@ -106,6 +106,7 @@ impl Class for JsRecord { } pub fn run(ctx: &Context, src: &str, arg: Vec, doc: Option<&Value>) -> Result { + // Check the context let _ = ctx.check()?; // Create an execution context let mut ctx = Boa::default(); @@ -126,7 +127,7 @@ pub fn run(ctx: &Context, src: &str, arg: Vec, doc: Option<&Value>) -> Re // Attempt to execute the script match ctx.eval(src.as_bytes()) { // The script executed successfully - Ok(ref v) => Ok(v.into()), + Ok(v) => Ok(v.into()), // There was an error running the script Err(e) => Err(Error::InvalidScript { message: e.display().to_string(), @@ -134,12 +135,6 @@ pub fn run(ctx: &Context, src: &str, arg: Vec, doc: Option<&Value>) -> Re } } -impl From for JsValue { - fn from(v: Value) -> Self { - JsValue::from(&v) - } -} - impl From<&Datetime> for Date { fn from(v: &Datetime) -> Self { let mut obj = Self::default(); @@ -157,6 +152,12 @@ impl From<&Datetime> for Date { } } +impl From for JsValue { + fn from(v: Value) -> Self { + JsValue::from(&v) + } +} + impl From<&Value> for JsValue { fn from(v: &Value) -> Self { match v { @@ -212,6 +213,12 @@ impl From<&Value> for JsValue { } } +impl From for Value { + fn from(v: JsValue) -> Self { + Value::from(&v) + } +} + impl From<&JsValue> for Value { fn from(v: &JsValue) -> Self { match v {