From 0b975b72de3a6ab4b8c76e97eac6ba7f8a05b3e5 Mon Sep 17 00:00:00 2001 From: jesopo Date: Mon, 8 Oct 2018 12:43:47 +0100 Subject: [PATCH] Add !rust in modules/rust_eval.py --- modules/rust_eval.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 modules/rust_eval.py diff --git a/modules/rust_eval.py b/modules/rust_eval.py new file mode 100644 index 00000000..a1615c66 --- /dev/null +++ b/modules/rust_eval.py @@ -0,0 +1,40 @@ +import json, socket +from src import ModuleManager, utils + +EVAL_URL = "https://play.rust-lang.org/execute" +FN_TEMPLATE = """ +fn main() { + println!(\"{:?}\", { + %s + }); +} +""" + +class Module(ModuleManager.BaseModule): + _name = "Rust" + @utils.hook("received.command.rust", min_args=1) + def eval(self, event): + """ + :help: Evaluate a rust statement + :usage: + """ + try: + page = utils.http.get_url(EVAL_URL, post_data=json.dumps({ + "code": FN_TEMPLATE % event["args"], + "channel": "nightly", + "crateType": "bin", + "mode": "debug", + "tests": False, + "execute": True, + "target": "ast", + "backtrace": False + }), method="POST", json=True) + except socket.timeout: + event["stderr"].write("%s: eval timed out" % + event["user"].nickname) + return + + if page["success"]: + event["stdout"].write(page["stdout"]) + else: + event["stderr"].write(page["stderr"])