Skip to content

Commit e9051e1

Browse files
committed
add arguments to godbolt
1 parent 2d029fa commit e9051e1

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

src/godbolt.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::{api, commands::Args};
12
pub enum Compilation {
23
Success { asm: String },
34
Error { stderr: String },
@@ -32,17 +33,39 @@ struct GodboltResponse {
3233
asm: GodboltOutput,
3334
}
3435

36+
pub fn help(args: Args) -> Result<(), crate::Error> {
37+
let message = "Compile Rust code using https://rust.godbolt.org/. Full optimizations are applied unless overriden.
38+
```?godbolt flags={} rustc={} ``\u{200B}`code``\u{200B}` ```
39+
Optional arguments:
40+
\tflags: flags to pass to rustc invocation. Defaults to \"-Copt-level=3 --edition=2018\".
41+
\trustc: compiler version to invoke. Defaults to `nightly`. Possible values: `nightly`, `beta` or full version like `1.45.2`.
42+
";
43+
44+
api::send_reply(&args, &message)?;
45+
Ok(())
46+
}
47+
3548
/// Compile a given Rust source code file on Godbolt using the latest nightly compiler with
36-
/// full optimizations (-O3)
49+
/// full optimizations (-O3) by default
3750
/// Returns a multiline string with the pretty printed assembly
3851
pub fn compile_rust_source(
3952
http: &reqwest::blocking::Client,
4053
source_code: &str,
54+
flags: &str,
55+
rustc: &str,
4156
) -> Result<Compilation, crate::Error> {
57+
let cv = rustc_to_godbolt(rustc);
58+
let cv = match cv {
59+
Ok(c) => c,
60+
Err(e) => {
61+
return Ok(Compilation::Error { stderr: e });
62+
}
63+
};
64+
4265
let response: GodboltResponse = http
4366
.execute(
44-
http.post("https://godbolt.org/api/compiler/nightly/compile")
45-
.query(&[("options", "-Copt-level=3")])
67+
http.post(&format!("https://godbolt.org/api/compiler/{}/compile", cv))
68+
.query(&[("options", &flags)])
4669
.header(reqwest::header::ACCEPT, "application/json")
4770
.body(source_code.to_owned())
4871
.build()?,
@@ -61,3 +84,21 @@ pub fn compile_rust_source(
6184
}
6285
})
6386
}
87+
88+
// converts a rustc version number to a godbolt compiler id
89+
fn rustc_to_godbolt(rustc_version: &str) -> Result<String, String> {
90+
match rustc_version {
91+
"beta" => Ok("beta".to_string()),
92+
"nightly" => Ok("nightly".to_string()),
93+
// this heuristic is barebones but catches most obviously wrong things
94+
// it doesn't know anything about valid rustc versions
95+
ver if ver.contains('.') && !ver.contains(|c: char| c.is_alphabetic()) => {
96+
let mut godbolt_version = "r".to_string();
97+
for segment in ver.split('.') {
98+
godbolt_version.push_str(segment);
99+
}
100+
Ok(godbolt_version)
101+
}
102+
other => Err(format!("invalid rustc version: `{}`", other)),
103+
}
104+
}

src/main.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,15 @@ fn app() -> Result<(), Error> {
149149
});
150150
}
151151

152-
cmds.add("?godbolt ```\ncode```", |args| {
152+
cmds.add("?godbolt flags={} version={} ```\ncode```", |args| {
153+
let flags = args.params.get("flags").unwrap_or(&"false");
154+
let rustc = args.params.get("rustc").unwrap_or(&"nightly");
155+
153156
let code = args
154157
.params
155158
.get("code")
156159
.ok_or("Unable to retrieve param: code")?;
157-
let (lang, text) = match godbolt::compile_rust_source(args.http, code)? {
160+
let (lang, text) = match godbolt::compile_rust_source(args.http, code, flags, rustc)? {
158161
godbolt::Compilation::Success { asm } => ("x86asm", asm),
159162
godbolt::Compilation::Error { stderr } => ("rust", stderr),
160163
};
@@ -169,12 +172,7 @@ fn app() -> Result<(), Error> {
169172
Ok(())
170173
});
171174
cmds.help("?godbolt", "View assembly using Godbolt", |args| {
172-
api::send_reply(
173-
&args,
174-
"Compile Rust code using https://rust.godbolt.org. Full optimizations are applied. \
175-
```?godbolt ``\u{200B}`code``\u{200B}` ```",
176-
)?;
177-
Ok(())
175+
godbolt::help(args)
178176
});
179177

180178
// Slow mode.

0 commit comments

Comments
 (0)