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 super::Request for CompileRequest {
99    fn endpoint<'a>(&self) -> super::Endpoints<'a> {
100        super::Endpoints::Compile
101    }
102}
103
104impl Default for CompileRequest {
105    /// Provides a default `CompileRequest` configuration.
106    ///
107    /// Defaults to:
108    /// - Target: `Assembly`
109    /// - Assembly flavor: `AT&T`
110    /// - Demangling: `Demangle`
111    /// - Process: `Filter`
112    /// - Channel: `Stable`
113    /// - Mode: `Debug`
114    /// - Edition: `2024`
115    /// - Crate type: `Binary`
116    /// - Tests: disabled
117    /// - Backtrace: disabled
118    /// - Code: `fn main() { println!("Hello, world!"); }`
119    ///
120    /// # Returns
121    ///
122    /// A `CompileRequest` instance with default values for compiling basic Rust code.
123    fn default() -> Self {
124        Self {
125            target: CompileTarget::Assembly,
126            assembly_flavor: Some(AssemblyFlavor::Att),
127            demangle_assembly: Some(DemangleAssembly::Demangle),
128            process_assembly: Some(ProcessAssembly::Filter),
129            channel: Channel::Stable,
130            mode: Mode::Debug,
131            edition: Edition::Edition2024,
132            crate_type: CrateType::Binary,
133            tests: false,
134            backtrace: false,
135            code: "fn main() { println!(\"Hello, world!\"); }".to_owned(),
136        }
137    }
138}
139
140/// Response structure returned after compiling Rust code.
141///
142/// Includes compilation success status, process exit details, and outputs.
143#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
144pub struct CompileResponse {
145    /// Indicates if the compilation was successful.
146    pub success: bool,
147
148    /// Details about the compiler process exit (code, signals, etc.).
149    #[serde(rename = "exitDetail")]
150    pub exit_detail: String,
151
152    /// The original source code sent for compilation.
153    pub code: String,
154
155    /// Standard output from the compiler.
156    pub stdout: String,
157
158    /// Standard error output, including warnings and errors.
159    pub stderr: String,
160}
161
162impl super::Response for CompileResponse {}
163
164/// Specifies the assembly syntax flavor for assembly output.
165///
166/// - `Att`: AT&T syntax (common on Unix-like systems).
167/// - `Intel`: Intel syntax (common on Windows).
168#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
169#[cfg_attr(feature = "poise-bot", derive(poise::ChoiceParameter))]
170#[serde(rename_all = "lowercase")]
171pub enum AssemblyFlavor {
172    Att,
173    Intel,
174}
175
176/// Determines whether assembly output is demangled or mangled.
177///
178/// - `Demangle`: Convert symbol names to human-readable form.
179/// - `Mangle`: Keep symbol names mangled (default compiler format).
180#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
181#[cfg_attr(feature = "poise-bot", derive(poise::ChoiceParameter))]
182#[serde(rename_all = "lowercase")]
183pub enum DemangleAssembly {
184    Demangle,
185    Mangle,
186}
187
188/// Controls processing of assembly output.
189///
190/// - `Filter`: Filter assembly output for readability.
191/// - `Raw`: Return raw assembly output without filtering.
192#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
193#[cfg_attr(feature = "poise-bot", derive(poise::ChoiceParameter))]
194#[serde(rename_all = "lowercase")]
195pub enum ProcessAssembly {
196    Filter,
197    Raw,
198}
199
200/// Defines the compilation target output format.
201///
202/// Variants:
203/// - `Assembly`: Direct assembly output.
204/// - `Hir`: High-level Intermediate Representation.
205/// - `LlvmIr`: LLVM Intermediate Representation.
206/// - `Mir`: Mid-level Intermediate Representation.
207/// - `Wasm`: WebAssembly output.
208#[derive(Debug, Deserialize, Serialize, Clone, Copy, PartialEq, Eq)]
209#[cfg_attr(feature = "poise-bot", derive(poise::ChoiceParameter))]
210#[serde(rename_all = "lowercase")]
211pub enum CompileTarget {
212    #[serde(rename = "asm")]
213    Assembly,
214    Hir,
215    LlvmIr,
216    Mir,
217    Wasm,
218}