aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJokler <jokler.contact@gmail.com>2018-06-28 05:48:01 +0200
committerJokler <jokler.contact@gmail.com>2018-06-28 05:48:01 +0200
commit01735d63f2462162fccb914dffbbcd06244f85af (patch)
tree5ee4171a5e15c9d0f0ed32172c2e0cb747303202
parentb3b53b4075ae626b16aa5e0c4781244c74e8dfb8 (diff)
downloadfrippy-01735d63f2462162fccb914dffbbcd06244f85af.tar.gz
frippy-01735d63f2462162fccb914dffbbcd06244f85af.zip
Factoids: Add a json.decode function to lua
-rw-r--r--Cargo.lock42
-rw-r--r--Cargo.toml4
-rw-r--r--src/lib.rs1
-rw-r--r--src/plugins/factoids/mod.rs1
-rw-r--r--src/plugins/factoids/sandbox.lua1
-rw-r--r--src/plugins/factoids/utils.rs52
6 files changed, 71 insertions, 30 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 610b067..31baab9 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -103,9 +103,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -159,7 +159,7 @@ dependencies = [
"quine-mc_cluskey 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-normalization 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -443,8 +443,8 @@ dependencies = [
"regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.8.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rlua 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode_names 0.1.7 (git+https://github.com/Jokler/unicode_names?branch=update-to-latest-unicode)",
]
@@ -615,7 +615,7 @@ dependencies = [
"futures 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"native-tls 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -701,7 +701,7 @@ version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -723,10 +723,10 @@ dependencies = [
"libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log-mdc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
"serde-value 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_yaml 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)",
"typemap 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1155,8 +1155,8 @@ dependencies = [
"log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"mime_guess 2.0.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)",
"native-tls 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_urlencoded 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1246,7 +1246,7 @@ version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -1256,7 +1256,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "serde"
-version = "1.0.66"
+version = "1.0.67"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@@ -1265,7 +1265,7 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ordered-float 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -1280,12 +1280,12 @@ dependencies = [
[[package]]
name = "serde_json"
-version = "1.0.20"
+version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -1295,7 +1295,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -1306,7 +1306,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
"linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
"yaml-rust 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -1614,7 +1614,7 @@ name = "toml"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
- "serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -1949,10 +1949,10 @@ dependencies = [
"checksum security-framework-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "5421621e836278a0b139268f36eee0dc7e389b784dc3f79d8f11aabadf41bead"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
-"checksum serde 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "e9a2d9a9ac5120e0f768801ca2b58ad6eec929dc9d1d616c162f208869c2ce95"
+"checksum serde 1.0.67 (registry+https://github.com/rust-lang/crates.io-index)" = "4d385f7330d82659e6504cd41142b2ad3b9ff8861b722a80bc2efaa6d2be1f60"
"checksum serde-value 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "52903ade2290cbd61a0937a66a268f26cebf246e3ddd7964a8babb297111fb0d"
"checksum serde_derive 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "0a90213fa7e0f5eac3f7afe2d5ff6b088af515052cc7303bd68c7e3b91a3fb79"
-"checksum serde_json 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "fc97cccc2959f39984524026d760c08ef0dd5f0f5948c8d31797dbfae458c875"
+"checksum serde_json 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)" = "84b8035cabe9b35878adec8ac5fe03d5f6bc97ff6edd7ccb96b44c1276ba390e"
"checksum serde_urlencoded 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e703cef904312097cfceab9ce131ff6bbe09e8c964a0703345a5f49238757bc1"
"checksum serde_yaml 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ef8099d3df28273c99a1728190c7a9f19d444c941044f64adf986bee7ec53051"
"checksum siphasher 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0df90a788073e8d0235a67e50441d47db7c8ad9debd91cbf43736a2a92d36537"
diff --git a/Cargo.toml b/Cargo.toml
index 830c547..081ca0c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -31,8 +31,8 @@ rlua = "0.13.0"
reqwest = "0.8.6"
regex = "1.0.0"
lazy_static = "1.0.1"
-serde = "1.0.66"
-serde_json = "1.0.20"
+serde = "1.0.67"
+serde_json = "1.0.22"
chrono = "0.4.3"
glob = "0.2.11"
circular-queue = "0.2.0"
diff --git a/src/lib.rs b/src/lib.rs
index 0a8af6e..9b49644 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -52,6 +52,7 @@ extern crate humantime;
extern crate irc;
extern crate regex;
extern crate reqwest;
+extern crate serde_json;
extern crate time;
pub mod error;
diff --git a/src/plugins/factoids/mod.rs b/src/plugins/factoids/mod.rs
index eb9c10d..6ff2132 100644
--- a/src/plugins/factoids/mod.rs
+++ b/src/plugins/factoids/mod.rs
@@ -208,6 +208,7 @@ impl<T: Database> Factoids<T> {
globals.set("factoid", code)?;
globals.set("download", lua.create_function(download)?)?;
+ globals.set("json_decode", lua.create_function(json_decode)?)?;
globals.set("sleep", lua.create_function(sleep)?)?;
globals.set("args", args)?;
globals.set("input", command.tokens.join(" "))?;
diff --git a/src/plugins/factoids/sandbox.lua b/src/plugins/factoids/sandbox.lua
index abc55ad..a927535 100644
--- a/src/plugins/factoids/sandbox.lua
+++ b/src/plugins/factoids/sandbox.lua
@@ -32,6 +32,7 @@ local sandbox_env = {
trim = trim,
eval = nil,
sleep = nil,
+ json = {decode = json_decode},
args = args,
input = input,
user = user,
diff --git a/src/plugins/factoids/utils.rs b/src/plugins/factoids/utils.rs
index 48ec385..c3bd629 100644
--- a/src/plugins/factoids/utils.rs
+++ b/src/plugins/factoids/utils.rs
@@ -1,15 +1,22 @@
-extern crate reqwest;
-
use std::thread;
use std::time::Duration;
+use serde_json::{self, Value as SerdeValue};
+
use super::rlua::Error as LuaError;
-use super::rlua::Lua;
+use super::rlua::Error::RuntimeError;
+use super::rlua::{Lua, Value as LuaValue};
+
use utils::error::ErrorKind::Connection;
use utils::Url;
use failure::Fail;
+pub fn sleep(_: &Lua, dur: u64) -> Result<(), LuaError> {
+ thread::sleep(Duration::from_millis(dur));
+ Ok(())
+}
+
pub fn download(_: &Lua, url: String) -> Result<String, LuaError> {
let url = Url::from(url).max_kib(1024);
match url.request() {
@@ -20,7 +27,7 @@ pub fn download(_: &Lua, url: String) -> Result<String, LuaError> {
_ => e.to_string(),
};
- Err(LuaError::RuntimeError(format!(
+ Err(RuntimeError(format!(
"Failed to download {} - {}",
url.as_str(),
error
@@ -29,7 +36,38 @@ pub fn download(_: &Lua, url: String) -> Result<String, LuaError> {
}
}
-pub fn sleep(_: &Lua, dur: u64) -> Result<(), LuaError> {
- thread::sleep(Duration::from_millis(dur));
- Ok(())
+fn convert_value(lua: &Lua, sval: SerdeValue) -> Result<LuaValue, LuaError> {
+ let lval = match sval {
+ SerdeValue::Null => LuaValue::Nil,
+ SerdeValue::Bool(b) => LuaValue::Boolean(b),
+ SerdeValue::String(s) => LuaValue::String(lua.create_string(&s)?),
+ SerdeValue::Number(n) => {
+ let f = n.as_f64().ok_or(RuntimeError(String::from("Failed to convert number into double",)))?;
+ LuaValue::Number(f)
+ }
+ SerdeValue::Array(arr) => {
+ let table = lua.create_table()?;
+ for (i, val) in arr.into_iter().enumerate() {
+ table.set(i + 1, convert_value(lua, val)?)?;
+ }
+
+ LuaValue::Table(table)
+ }
+ SerdeValue::Object(obj) => {
+ let table = lua.create_table()?;
+ for (key, val) in obj {
+ table.set(key, convert_value(lua, val)?)?;
+ }
+
+ LuaValue::Table(table)
+ }
+ };
+
+ Ok(lval)
+}
+
+pub fn json_decode(lua: &Lua, json: String) -> Result<LuaValue, LuaError> {
+ let ser_val: SerdeValue = serde_json::from_str(&json).map_err(|e| RuntimeError(e.to_string()))?;
+
+ convert_value(lua, ser_val)
}