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