Skip to content

Commit 6a329cc

Browse files
committed
success compilation command
1 parent f356805 commit 6a329cc

6 files changed

Lines changed: 123 additions & 13 deletions

File tree

src/bin/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ async fn main() -> Result<(), Error> {
3232
commands::miri_code_block(),
3333
commands::macro_expansion_code_block(),
3434
commands::clippy_code_block(),
35+
commands::compile_code_block(),
3536
commands::publish(),
3637
commands::explain(),
3738
commands::krate(),

src/commands/cargo.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod clippy;
22
mod code_block;
33
mod common;
4+
mod compile;
45
mod crates;
56
mod file;
67
mod gist;
@@ -12,6 +13,7 @@ mod run;
1213
mod version;
1314

1415
pub use clippy::clippy_code_block;
16+
pub use compile::compile_code_block;
1517
pub use macro_expansion::macro_expansion_code_block;
1618
pub use miri::miri_code_block;
1719
pub use publish::publish;

src/commands/cargo/clippy.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,20 @@ async fn clippy_file(
7272
fn parse_clippy(input: &str) -> ClippyRequest<'_> {
7373
let mut req = ClippyRequest::default();
7474

75-
input.split_whitespace().for_each(|arg| match arg {
76-
"stable" => req.channel = Channel::Stable,
77-
"beta" => req.channel = Channel::Beta,
78-
"nightly" => req.channel = Channel::Nightly,
79-
"bin" => req.crate_type = CrateType::Binary,
80-
"lib" => req.crate_type = CrateType::Library,
81-
"2015" => req.edition = Edition::Edition2015,
82-
"2018" => req.edition = Edition::Edition2018,
83-
"2021" => req.edition = Edition::Edition2021,
84-
"2024" => req.edition = Edition::Edition2024,
85-
_ => {}
86-
});
75+
input
76+
.split_whitespace()
77+
.for_each(|arg| match arg.to_lowercase().as_str() {
78+
"stable" => req.channel = Channel::Stable,
79+
"beta" => req.channel = Channel::Beta,
80+
"nightly" => req.channel = Channel::Nightly,
81+
"bin" => req.crate_type = CrateType::Binary,
82+
"lib" => req.crate_type = CrateType::Library,
83+
"2015" => req.edition = Edition::Edition2015,
84+
"2018" => req.edition = Edition::Edition2018,
85+
"2021" => req.edition = Edition::Edition2021,
86+
"2024" => req.edition = Edition::Edition2024,
87+
_ => {}
88+
});
8789

8890
req
8991
}

src/commands/cargo/code_block.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ where
2121
req.with_code(code);
2222

2323
let res: Res = ctx.data().playground_client.post(&req).await?;
24+
println!("{res:?}");
2425
let out = res.output();
2526

2627
let bot_res = BotResponse::new(&out, "code_block", None, tool_name);

src/commands/cargo/compile.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use crate::{Context, Error};
2+
use playground_api::endpoints::{
3+
AssemblyFlavor, Channel, CompileRequest, CompileResponse, CompileTarget, CrateType,
4+
DemangleAssembly, Edition, Mode, ProcessAssembly,
5+
};
6+
use std::borrow::Cow;
7+
8+
const COMPILE_RES: CompileResponse = CompileResponse {
9+
success: false,
10+
stdout: Cow::Borrowed(""),
11+
stderr: Cow::Borrowed(""),
12+
exit_detail: Cow::Borrowed(""),
13+
code: Cow::Borrowed(""),
14+
};
15+
16+
#[poise::command(
17+
prefix_command,
18+
slash_command,
19+
rename = "compile",
20+
category = "cargo",
21+
subcommands("compile_gist", "compile_file"),
22+
broadcast_typing,
23+
track_edits
24+
)]
25+
pub async fn compile_code_block(ctx: Context<'_>, #[rest] input: String) -> Result<(), Error> {
26+
super::code_block(ctx, &input, parse_compile, true, COMPILE_RES, "compile").await
27+
}
28+
29+
#[poise::command(
30+
slash_command,
31+
rename = "gist",
32+
category = "cargo",
33+
member_cooldown = 60
34+
)]
35+
async fn compile_gist(ctx: Context<'_>) -> Result<(), Error> {
36+
Ok(())
37+
}
38+
39+
#[poise::command(
40+
slash_command,
41+
rename = "gist",
42+
category = "cargo",
43+
member_cooldown = 60
44+
)]
45+
async fn compile_file(ctx: Context<'_>) -> Result<(), Error> {
46+
Ok(())
47+
}
48+
49+
fn parse_compile(input: &str) -> CompileRequest<'_> {
50+
let mut req = CompileRequest::default();
51+
52+
input
53+
.split_whitespace()
54+
.for_each(|arg| match arg.to_lowercase().as_str() {
55+
"asm" | "assembly" => req.target = CompileTarget::Assembly,
56+
"hir" => req.target = CompileTarget::Hir,
57+
"llvmir" => req.target = CompileTarget::LlvmIr,
58+
"mir" => req.target = CompileTarget::Mir,
59+
"wasm" => req.target = CompileTarget::Wasm,
60+
"att" => req.assembly_flavor = Some(AssemblyFlavor::Att),
61+
"intel" => req.assembly_flavor = Some(AssemblyFlavor::Intel),
62+
"demangle" => req.demangle_assembly = Some(DemangleAssembly::Demangle),
63+
"mangle" => req.demangle_assembly = Some(DemangleAssembly::Mangle),
64+
"filter" => req.process_assembly = Some(ProcessAssembly::Filter),
65+
"raw" => req.process_assembly = Some(ProcessAssembly::Raw),
66+
"stable" => req.channel = Channel::Stable,
67+
"beta" => req.channel = Channel::Beta,
68+
"nightly" => req.channel = Channel::Nightly,
69+
"debug" => req.mode = Mode::Debug,
70+
"release" | "-r" => req.mode = Mode::Release,
71+
"2015" => req.edition = Edition::Edition2015,
72+
"2018" => req.edition = Edition::Edition2018,
73+
"2021" => req.edition = Edition::Edition2021,
74+
"2024" => req.edition = Edition::Edition2024,
75+
"binary" | "bin" => req.crate_type = CrateType::Binary,
76+
"library" | "lib" => req.crate_type = CrateType::Library,
77+
"tests" => req.tests = true,
78+
"backtrace" => req.backtrace = true,
79+
_ => {}
80+
});
81+
82+
req
83+
}
84+
85+
impl<'wc> super::WithCode<'wc> for CompileRequest<'wc> {
86+
fn with_code(&mut self, code: impl Into<Cow<'wc, str>>) {
87+
self.code = code.into();
88+
}
89+
}
90+
91+
impl<'a> super::Output for CompileResponse<'a> {
92+
fn success(&self) -> bool {
93+
self.success
94+
}
95+
96+
#[expect(clippy::misnamed_getters)]
97+
fn stdout(&self) -> &str {
98+
&self.code
99+
}
100+
101+
fn stderr(&self) -> &str {
102+
&self.stderr
103+
}
104+
}

src/common/separate_cargo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use regex::Regex;
22
use std::sync::LazyLock;
33

44
static REGEX: LazyLock<Regex> = LazyLock::new(|| unsafe {
5-
Regex::new(r"(?sm)^(.*?^\s*(?:Running|error: could not compile)[^\n]*\n?)(.*)")
5+
Regex::new(r"(?sm)^(.*?^\s*(?:Running|error: could not compile|Finished)[^\n]*\n?)(.*)")
66
.unwrap_unchecked()
77
});
88

0 commit comments

Comments
 (0)