From 11cde0b6d371b318a7017a7925875b4393e0a315 Mon Sep 17 00:00:00 2001 From: theMackabu Date: Sat, 9 Dec 2023 14:03:51 -0800 Subject: [PATCH] add global helpers --- Cargo.lock | 1 + Cargo.toml | 3 ++- app.routes | 6 ++--- src/main.rs | 72 ++++++++++++++++++++++++++++++++++++++++++++--------- 4 files changed, 66 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3efae3b..dfc018a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1315,6 +1315,7 @@ dependencies = [ "rhai-url", "serde", "serde_json", + "smartstring", "termcolor", ] diff --git a/Cargo.toml b/Cargo.toml index 3b34482..348c640 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ serde_json = "1.0.108" fancy-regex = "0.12.0" serde = { version = "1.0.193", features = ["derive"] } reqwest = { version = "0.11.22", features = ["blocking"] } -rhai = { version = "1.16.3", features = ["serde_json", "serde"] } \ No newline at end of file +rhai = { version = "1.16.3", features = ["serde_json", "serde"] } +smartstring = "1.0.1" diff --git a/app.routes b/app.routes index 00dbfc8..9e860d8 100644 --- a/app.routes +++ b/app.routes @@ -13,13 +13,13 @@ example() { example/json() { let res = http::get("https://httpbin.org//json"); let body = #{ - response: res.json, + response: res.json(), info: #{ length: res.length, status: res.status, error: res.error, body: res.body, - } + }, }; json(body) @@ -31,7 +31,7 @@ example/post() { url: url, }; - json(http::post("https://httpbin.org//post", body).json) + json(http::post("https://httpbin.org//post", body).raw()) } test.json() { diff --git a/src/main.rs b/src/main.rs index ad9c2b4..dcb53cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ use fancy_regex::{Captures, Error, Regex}; use lazy_static::lazy_static; -use macros_rs::{crashln, string, ternary}; +use macros_rs::{crashln, str, string, ternary}; use reqwest::blocking::Client; +use serde::{Deserialize, Serialize}; +use smartstring::alias::String as SmString; use std::{fs, path::PathBuf}; -use rhai::{packages::Package, plugin::*, Engine, FnNamespace, Map, ParseError, Scope, AST}; +use rhai::{packages::Package, plugin::*, Dynamic, Engine, FnNamespace, Map, ParseError, Scope, AST}; use rhai_fs::FilesystemPackage; use rhai_url::UrlPackage; @@ -71,7 +73,7 @@ mod status { #[export_module] mod http { - #[derive(Debug, Clone)] + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Http { pub length: Option, pub status: u16, @@ -79,6 +81,25 @@ mod http { pub body: Option, } + impl From for Map { + fn from(http: Http) -> Self { + let mut map = Map::new(); + map.insert(SmString::from("status"), Dynamic::from(http.status as i64)); + + if let Some(length) = http.length { + map.insert(SmString::from("length"), Dynamic::from(length as i64)); + } + if let Some(err) = http.err { + map.insert(SmString::from("err"), Dynamic::from(err)); + } + if let Some(body) = http.body { + map.insert(SmString::from("body"), Dynamic::from(body)); + } + + return map; + } + } + pub fn get(url: String) -> Http { let client = Client::new(); let response = @@ -149,21 +170,48 @@ mod http { } } - // add err handling - #[rhai_fn(get = "json")] - pub fn json(res: Http) -> Map { serde_json::from_str(&res.body.unwrap()).unwrap() } - #[rhai_fn(get = "status")] pub fn status(res: Http) -> i64 { res.status as i64 } + #[rhai_fn(global, pure, return_raw, name = "raw")] + pub fn raw(res: &mut Http) -> Result> { Ok(res.clone().into()) } + #[rhai_fn(get = "length")] - pub fn length(res: Http) -> i64 { res.length.unwrap() as i64 } + pub fn length(res: Http) -> i64 { + match res.length { + Some(len) => len as i64, + None => 0, + } + } + + #[rhai_fn(get = "body", return_raw)] + pub fn body(res: Http) -> Result> { + match res.body { + Some(body) => Ok(body.to_string()), + None => Ok(string!("")), + } + } - #[rhai_fn(get = "body")] - pub fn body(res: Http) -> String { res.body.unwrap_or(string!("")) } + #[rhai_fn(global, pure, return_raw, name = "json")] + pub fn json(res: &mut Http) -> Result> { + let body = str!(res.body.clone().unwrap()); + match serde_json::from_str(body) { + Ok(map) => Ok(map), + Err(err) => Err(format!("{}", &err).into()), + } + } - #[rhai_fn(get = "error")] - pub fn error(res: Http) -> String { res.err.unwrap_or(string!("")) } + #[rhai_fn(get = "error", return_raw)] + pub fn error(res: Http) -> Result> { + let err = match res.err { + Some(err) => format!("\"{err}\""), + None => string!("null"), + }; + match serde_json::from_str(&format!("{{\"message\":{err}}}")) { + Ok(msg) => Ok(msg), + Err(err) => Err(format!("{}", &err).into()), + } + } } #[get("{url:.*}")] -- GitLab