playground_api/endpoints/
execute.rs

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