playground_api/endpoints/
execute.rs

1use super::{Channel, CrateType, Edition, Mode};
2use serde::{Deserialize, Serialize};
3
4/// Request structure to execute Rust code on the playground.
5///
6/// Specifies compilation parameters and the source code to run.
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
8pub struct ExecuteRequest {
9    /// The Rust release channel to use (stable, beta, nightly).
10    pub channel: Channel,
11
12    /// The compilation mode: debug or release.
13    pub mode: Mode,
14
15    /// The Rust edition to compile and run with (2015, 2018, 2021, 2024).
16    pub edition: Edition,
17
18    /// The crate type: binary or library.
19    #[serde(rename = "crateType")]
20    pub crate_type: CrateType,
21
22    /// Whether to include test code during execution.
23    pub tests: bool,
24
25    /// Whether to enable backtrace output on runtime errors.
26    #[serde(default)]
27    pub backtrace: bool,
28
29    /// The Rust source code to compile and execute.
30    pub code: String,
31}
32
33impl ExecuteRequest {
34    /// Creates a new `ExecuteRequest`.
35    ///
36    /// # Arguments
37    ///
38    /// * `channel` - Rust release channel.
39    /// * `mode` - Compilation mode (debug/release).
40    /// * `edition` - Rust edition.
41    /// * `crate_type` - Crate type (binary or library).
42    /// * `tests` - Whether to run test code.
43    /// * `backtrace` - Whether to enable backtraces.
44    /// * `code` - Source code to execute.
45    ///
46    /// # Returns
47    ///
48    /// An `ExecuteRequest` initialized with the given parameters.
49    pub fn new(
50        channel: Channel,
51        mode: Mode,
52        edition: Edition,
53        crate_type: CrateType,
54        tests: bool,
55        backtrace: bool,
56        code: String,
57    ) -> Self {
58        Self {
59            channel,
60            mode,
61            edition,
62            crate_type,
63            tests,
64            backtrace,
65            code,
66        }
67    }
68}
69
70impl super::Request for ExecuteRequest {
71    fn endpoint<'a>(&self) -> super::Endpoints<'a> {
72        super::Endpoints::Execute
73    }
74}
75
76impl Default for ExecuteRequest {
77    /// Provides a default `ExecuteRequest` configuration.
78    ///
79    /// Defaults to:
80    /// - `channel`: `Stable`
81    /// - `mode`: `Debug`
82    /// - `edition`: `2024`
83    /// - `crate_type`: `Binary`
84    /// - `tests`: `false`
85    /// - `backtrace`: `false`
86    /// - `code`: A simple "Hello, world!" program
87    ///
88    /// # Returns
89    ///
90    /// A `ExecuteRequest` instance with standard execution defaults
91    fn default() -> Self {
92        Self {
93            channel: Channel::Stable,
94            mode: Mode::Debug,
95            edition: Edition::Edition2024,
96            crate_type: CrateType::Binary,
97            tests: false,
98            backtrace: false,
99            code: "fn main() { println!(\"Hello, world!\"); }".to_owned(),
100        }
101    }
102}
103
104/// Response structure returned after executing Rust code.
105///
106/// Contains execution success status, exit details, and output streams.
107#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
108pub struct ExecuteResponse {
109    /// Indicates whether the execution was successful.
110    pub success: bool,
111
112    /// Details about the process exit (exit code, signals, etc.).
113    #[serde(rename = "exitDetail")]
114    pub exit_detail: String,
115
116    /// Standard output generated by the program.
117    pub stdout: String,
118
119    /// Standard error output, including runtime errors and panics.
120    pub stderr: String,
121}
122
123impl super::Response for ExecuteResponse {}