playground_api/endpoints/compile.rs
1use super::{Channel, CrateType, Edition, Mode};
2use serde::{Deserialize, Serialize};
3use std::borrow::Cow;
4
5/// Request structure for compiling Rust code via the playground API.
6///
7/// Contains configuration for target output, compilation channel, mode,
8/// crate type, edition, and the source code to compile.
9#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
10pub struct CompileRequest<'a> {
11 /// The output target format of the compilation (e.g., Assembly, MIR).
12 pub target: CompileTarget,
13
14 /// What flavour the assembly output should have (only required when target is [`CompileTarget::Assembly`])
15 #[serde(rename = "assemblyFlavor")]
16 pub assembly_flavor: Option<AssemblyFlavor>,
17
18 /// If the assembly output should be demangled or not (only required when target is [`CompileTarget::Assembly`])
19 #[serde(rename = "demangleAssembly")]
20 pub demangle_assembly: Option<DemangleAssembly>,
21
22 /// If the output should be processed or not (only required when target is [`CompileTarget::Assembly`])
23 #[serde(rename = "processAssembly")]
24 pub process_assembly: Option<ProcessAssembly>,
25
26 /// The Rust release channel to use (stable, beta, nightly).
27 pub channel: Channel,
28
29 /// The compilation mode: debug or release.
30 pub mode: Mode,
31
32 /// The Rust edition to use (2015, 2018, 2021, 2024).
33 pub edition: Edition,
34
35 /// The crate type: binary or library.
36 #[serde(rename = "crateType")]
37 pub crate_type: CrateType,
38
39 /// Whether to include test code during compilation.
40 pub tests: bool,
41
42 /// Whether to enable backtrace output on errors.
43 pub backtrace: bool,
44
45 /// The Rust source code to compile.
46 pub code: Cow<'a, str>,
47}
48
49impl<'a> CompileRequest<'a> {
50 /// Creates a new `CompileRequest` instance.
51 ///
52 /// # Arguments
53 ///
54 /// * `target` - The compilation target (e.g., Assembly, LLVM IR, Wasm).
55 /// * `assembly_flavor` - The assembly flavor used when targeting assembly (e.g., AT&T or Intel). Optional.
56 /// * `demangle_assembly` - Whether to demangle symbols in assembly output. Optional.
57 /// * `process_assembly` - Whether to filter or output raw assembly. Optional.
58 /// * `channel` - The Rust release channel (e.g., stable, beta, nightly).
59 /// * `mode` - Compilation mode (e.g., debug or release).
60 /// * `edition` - The Rust edition to compile against.
61 /// * `crate_type` - The crate type (binary or library).
62 /// * `tests` - Whether to compile with test harness.
63 /// * `backtrace` - Whether to enable backtrace support.
64 /// * `code` - The Rust source code to compile.
65 ///
66 /// # Returns
67 ///
68 /// A fully constructed `CompileRequest`.
69 #[allow(clippy::too_many_arguments)]
70 pub fn new(
71 target: CompileTarget,
72 assembly_flavor: Option<AssemblyFlavor>,
73 demangle_assembly: Option<DemangleAssembly>,
74 process_assembly: Option<ProcessAssembly>,
75 channel: Channel,
76 mode: Mode,
77 edition: Edition,
78 crate_type: CrateType,
79 tests: bool,
80 backtrace: bool,
81 code: Cow<'a, str>,
82 ) -> Self {
83 Self {
84 target,
85 assembly_flavor,
86 demangle_assembly,
87 process_assembly,
88 channel,
89 mode,
90 edition,
91 crate_type,
92 tests,
93 backtrace,
94 code,
95 }
96 }
97}
98
99impl<'b> super::Request for CompileRequest<'b> {
100 fn endpoint<'a>(&self) -> super::Endpoints<'a> {
101 super::Endpoints::Compile
102 }
103}
104
105impl<'a> Default for CompileRequest<'a> {
106 /// Provides a default `CompileRequest` configuration.
107 ///
108 /// Defaults to:
109 /// - Target: `Assembly`
110 /// - Assembly flavor: `AT&T`
111 /// - Demangling: `Demangle`
112 /// - Process: `Filter`
113 /// - Channel: `Stable`
114 /// - Mode: `Debug`
115 /// - Edition: `2024`
116 /// - Crate type: `Binary`
117 /// - Tests: disabled
118 /// - Backtrace: disabled
119 /// - Code: `fn main() { println!("Hello, world!"); }`
120 ///
121 /// # Returns
122 ///
123 /// A `CompileRequest` instance with default values for compiling basic Rust code.
124 fn default() -> Self {
125 Self {
126 target: CompileTarget::Assembly,
127 assembly_flavor: Some(AssemblyFlavor::Att),
128 demangle_assembly: Some(DemangleAssembly::Demangle),
129 process_assembly: Some(ProcessAssembly::Filter),
130 channel: Channel::Stable,
131 mode: Mode::Debug,
132 edition: Edition::Edition2024,
133 crate_type: CrateType::Binary,
134 tests: false,
135 backtrace: false,
136 code: Cow::Borrowed("fn main() { println!(\"Hello, world!\"); }"),
137 }
138 }
139}
140
141/// Response structure returned after compiling Rust code.
142///
143/// Includes compilation success status, process exit details, and outputs.
144#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
145pub struct CompileResponse<'a> {
146 /// Indicates if the compilation was successful.
147 pub success: bool,
148
149 /// Details about the compiler process exit (code, signals, etc.).
150 #[serde(rename = "exitDetail")]
151 pub exit_detail: Cow<'a, str>,
152
153 /// The original source code sent for compilation.
154 pub code: Cow<'a, str>,
155
156 /// Standard output from the compiler.
157 pub stdout: Cow<'a, str>,
158
159 /// Standard error output, including warnings and errors.
160 pub stderr: Cow<'a, str>,
161}
162
163impl<'a> super::Response for CompileResponse<'a> {}
164
165/// Specifies the assembly syntax flavor for assembly output.
166///
167/// - `Att`: AT&T syntax (common on Unix-like systems).
168/// - `Intel`: Intel syntax (common on Windows).
169#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
170#[cfg_attr(feature = "poise-bot", derive(poise::ChoiceParameter))]
171#[serde(rename_all = "lowercase")]
172pub enum AssemblyFlavor {
173 Att,
174 Intel,
175}
176
177/// Determines whether assembly output is demangled or mangled.
178///
179/// - `Demangle`: Convert symbol names to human-readable form.
180/// - `Mangle`: Keep symbol names mangled (default compiler format).
181#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
182#[cfg_attr(feature = "poise-bot", derive(poise::ChoiceParameter))]
183#[serde(rename_all = "lowercase")]
184pub enum DemangleAssembly {
185 Demangle,
186 Mangle,
187}
188
189/// Controls processing of assembly output.
190///
191/// - `Filter`: Filter assembly output for readability.
192/// - `Raw`: Return raw assembly output without filtering.
193#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
194#[cfg_attr(feature = "poise-bot", derive(poise::ChoiceParameter))]
195#[serde(rename_all = "lowercase")]
196pub enum ProcessAssembly {
197 Filter,
198 Raw,
199}
200
201/// Defines the compilation target output format.
202///
203/// Variants:
204/// - `Assembly`: Direct assembly output.
205/// - `Hir`: High-level Intermediate Representation.
206/// - `LlvmIr`: LLVM Intermediate Representation.
207/// - `Mir`: Mid-level Intermediate Representation.
208/// - `Wasm`: WebAssembly output.
209#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
210#[cfg_attr(feature = "poise-bot", derive(poise::ChoiceParameter))]
211#[serde(rename_all = "lowercase")]
212pub enum CompileTarget {
213 #[serde(rename = "asm")]
214 Assembly,
215 Hir,
216 LlvmIr,
217 Mir,
218 Wasm,
219}