Skip to content

Commit 9209fd6

Browse files
committed
actually get playground link
1 parent 0166fa6 commit 9209fd6

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/playground.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ impl PlaygroundCode {
3131
tests: false,
3232
}
3333
}
34+
35+
fn url_from_gist(&self, gist: &str) -> String {
36+
let version = match self.channel {
37+
Channel::Nightly => "nightly",
38+
Channel::Beta => "beta",
39+
Channel::Stable => "stable",
40+
};
41+
42+
let edition = match self.edition {
43+
Edition::E2015 => "2015",
44+
Edition::E2018 => "2018",
45+
};
46+
47+
let mode = match self.mode {
48+
Mode::Debug => "debug",
49+
Mode::Release => "release",
50+
};
51+
52+
format!(
53+
"https://play.rust-lang.org/?version={}&mode={}&edition={}&gist={}",
54+
version, mode, edition, gist
55+
)
56+
}
3457
}
3558

3659
#[derive(Debug, Serialize)]
@@ -92,28 +115,30 @@ fn run_code(args: &Args, code: &str) -> Result<String> {
92115
Ok(if result.len() > 1994 {
93116
format!(
94117
"Output too large. Playground link: {}",
95-
get_playground_link(args, code)?
118+
get_playground_link(args, code, &request)?
96119
)
97120
} else {
98121
format!("```{}```", result)
99122
})
100123
}
101124

102-
fn get_playground_link(args: &Args, code: &str) -> Result<String> {
125+
fn get_playground_link(args: &Args, code: &str, request: &PlaygroundCode) -> Result<String> {
103126
let mut payload = HashMap::new();
104127
payload.insert("code", code);
105128

106129
let resp = args
107130
.http
108-
.get("https://play.rust-lang.org/meta/gist/")
131+
.post("https://play.rust-lang.org/meta/gist/")
109132
.header(header::REFERER, "https://discord.gg/rust-lang")
110133
.json(&payload)
111134
.send()?;
112135

113-
let resp = resp.text()?;
114-
debug!("{:?}", resp);
136+
let resp: HashMap<String, String> = resp.json()?;
137+
debug!("gist response: {:?}", resp);
115138

116-
Ok(resp)
139+
resp.get("id")
140+
.map(|id| request.url_from_gist(id))
141+
.ok_or("no gist found".into())
117142
}
118143

119144
pub fn run(args: Args) -> Result<()> {

0 commit comments

Comments
 (0)