diff --git a/Cargo.lock b/Cargo.lock index fca6941df8952c84f96322bb5688810e035eb18b..490b606276f5ba4528c2f316b03d03216933bf69 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 150634cd17e13b002a4e5070e91e66e516aee183..0d5de607bb4323857f1135b1677b2133a324d12b 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 9b340e11b6bc010771ecb7f41df94eed5c671e44..da3b8d8dee044bf4f697691d27c989127bfcf7ca 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(),