bitbot-3.11-fork/modules/rust.py

63 lines
1.9 KiB
Python
Raw Normal View History

#--depends-on commands
2018-10-08 11:43:47 +00:00
import json, socket
from src import ModuleManager, utils
2019-09-16 11:11:03 +00:00
API_CRATE = "https://crates.io/api/v1/crates/%s"
URL_CRATE = "https://crates.io/crates/%s"
2018-10-08 11:43:47 +00:00
EVAL_URL = "https://play.rust-lang.org/execute"
FN_TEMPLATE = """
fn main() {
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):
@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:
page = utils.http.request(EVAL_URL, post_data=args,
method="POST", content_type="application/json").json()
2018-10-08 11:43:47 +00:00
except socket.timeout:
raise utils.EventError("%s: eval timed out" %
2018-10-08 11:43:47 +00:00
event["user"].nickname)
err_or_out = "stdout" if page["success"] else "stderr"
event[err_or_out].write("%s: %s" % (event["user"].nickname,
page[err_or_out].strip("\n")))
2019-09-16 11:11:03 +00:00
@utils.hook("received.command.crate")
@utils.kwarg("min_args", 1)
@utils.kwarg("help", "Look up a given Rust crate on crates.io")
@utils.kwarg("usage", "<crate-name>")
def crate(self, event):
query = event["args_split"][0]
request = utils.http.Request(API_CRATE % query)
2019-09-16 11:11:03 +00:00
response = utils.http.request(request)
if response.code == 200:
crate = response.json()["crate"]
2019-09-16 11:11:03 +00:00
name = crate["id"]
url = URL_CRATE % name
event["stdout"].write("%s %s: %s - %s" % (
name, crate["max_version"], crate["description"], url))
else:
event["stderr"].write("Crate '%s' not found" % query)