From 14b7d5dfc3267479a38514e6c21190fb6516bef5 Mon Sep 17 00:00:00 2001
From: theMackabu <theMackabu@gmail.com>
Date: Mon, 9 Sep 2024 22:52:42 -0700
Subject: [PATCH] use ToDynamic

---
 Cargo.lock  | 12 ++++++++++++
 Cargo.toml  |  1 +
 src/http.rs | 30 +++---------------------------
 3 files changed, 16 insertions(+), 27 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index fca6941..490b606 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2486,6 +2486,17 @@ dependencies = [
  "thin-vec",
 ]
 
+[[package]]
+name = "rhai-dynamic"
+version = "0.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dd22808adf12fcb12925f7ee8d3bf8a91717b0c62c27ebbc8065fcc5d3454124"
+dependencies = [
+ "quote",
+ "smartstring",
+ "syn 2.0.77",
+]
+
 [[package]]
 name = "rhai-fs"
 version = "0.1.3"
@@ -2695,6 +2706,7 @@ dependencies = [
  "redis",
  "reqwest",
  "rhai",
+ "rhai-dynamic",
  "rhai-fs",
  "rhai-url",
  "ron",
diff --git a/Cargo.toml b/Cargo.toml
index 150634c..0d5de60 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -35,6 +35,7 @@ tokio-wrap = "0.0.3"
 smartstring = "1.0.1"
 serde_json = "1.0.127"
 pest_derive = "2.7.11"
+rhai-dynamic = "0.0.1"
 global_placeholders = "0.1.0"
 tracing-bunyan-formatter = "0.3.9"
 
diff --git a/src/http.rs b/src/http.rs
index 9b340e1..da3b8d8 100644
--- a/src/http.rs
+++ b/src/http.rs
@@ -8,7 +8,7 @@ use crate::{
 
 use mime::Mime;
 use reqwest::blocking::Client as ReqwestClient;
-use smartstring::alias::String as SmString;
+use rhai_dynamic::ToDynamic;
 use std::{collections::HashMap, io, sync::Arc};
 
 use rhai::{exported_module as export, plugin::*, Dynamic, Engine, Map, Scope};
@@ -113,7 +113,7 @@ async fn handler(req: HttpRequest, config: Data<Arc<Config>>) -> Result<impl Res
         }
     }
 
-    #[derive(Clone)]
+    #[derive(Clone, ToDynamic)]
     struct Request {
         path: String,
         url: String,
@@ -121,35 +121,11 @@ async fn handler(req: HttpRequest, config: Data<Arc<Config>>) -> Result<impl Res
         query: String,
     }
 
-    #[derive(Clone)]
+    #[derive(Clone, ToDynamic)]
     struct Internal {
         version: String,
     }
 
-    // convert to macro
-    impl Internal {
-        fn to_dynamic(&self) -> Dynamic {
-            let mut map = Map::new();
-
-            map.insert(SmString::from("version"), Dynamic::from(self.version.clone()));
-
-            Dynamic::from(map)
-        }
-    }
-
-    impl Request {
-        fn to_dynamic(&self) -> Dynamic {
-            let mut map = Map::new();
-
-            map.insert(SmString::from("path"), Dynamic::from(self.path.clone()));
-            map.insert(SmString::from("url"), Dynamic::from(self.url.clone()));
-            map.insert(SmString::from("version"), Dynamic::from(self.version.clone()));
-            map.insert(SmString::from("query"), Dynamic::from(self.query.clone()));
-
-            Dynamic::from(map)
-        }
-    }
-
     let request = Request {
         url: app.url.to_string(),
         path: app.path.to_owned(),
-- 
GitLab