2018-10-08 11:43:47 +00:00
|
|
|
import json, socket
|
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
|
|
|
EVAL_URL = "https://play.rust-lang.org/execute"
|
|
|
|
FN_TEMPLATE = """
|
|
|
|
fn main() {
|
2018-10-08 11:49:36 +00:00
|
|
|
println!("{:?}", {
|
2018-10-08 11:43:47 +00:00
|
|
|
%s
|
|
|
|
});
|
|
|
|
}
|
|
|
|
"""
|
2018-10-10 12:41:58 +00:00
|
|
|
API_ARGS = {
|
|
|
|
"channel": "nightly",
|
|
|
|
"crateType": "bin",
|
|
|
|
"mode": "debug",
|
|
|
|
"tests": False,
|
|
|
|
"execute": True,
|
|
|
|
"target": "ast",
|
|
|
|
"backtrace": False
|
|
|
|
}
|
2018-10-08 11:43:47 +00:00
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
|
|
|
_name = "Rust"
|
|
|
|
@utils.hook("received.command.rust", min_args=1)
|
|
|
|
def eval(self, event):
|
|
|
|
"""
|
|
|
|
:help: Evaluate a rust statement
|
|
|
|
:usage: <statement>
|
|
|
|
"""
|
2018-10-10 12:41:58 +00:00
|
|
|
args = API_ARGS.copy()
|
|
|
|
args["code"] = FN_TEMPLATE % event["args"]
|
2018-10-08 11:43:47 +00:00
|
|
|
try:
|
2018-12-11 22:26:38 +00:00
|
|
|
page = utils.http.request(EVAL_URL, json_data=args,
|
2018-10-10 12:41:58 +00:00
|
|
|
method="POST", json=True)
|
2018-10-08 11:43:47 +00:00
|
|
|
except socket.timeout:
|
2018-10-16 14:09:58 +00:00
|
|
|
raise utils.EventError("%s: eval timed out" %
|
2018-10-08 11:43:47 +00:00
|
|
|
event["user"].nickname)
|
|
|
|
|
2018-12-11 22:26:38 +00:00
|
|
|
err_or_out = "stdout" if page.data["success"] else "stderr"
|
2018-10-08 11:55:10 +00:00
|
|
|
event[err_or_out].write("%s: %s" % (event["user"].nickname,
|
2018-12-11 22:26:38 +00:00
|
|
|
page.data[err_or_out].strip("\n")))
|