|
| 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 | +} |
0 commit comments