Skip to content

Commit 64087f7

Browse files
committed
add arguments to godbolt
1 parent 015d697 commit 64087f7

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
@@ -148,12 +148,15 @@ fn app() -> Result<(), Error> {
148148
});
149149
}
150150

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

179177
// Slow mode.

0 commit comments

Comments
 (0)