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 Default for ExecuteRequest {
71 /// Provides a default `ExecuteRequest` configuration.
72 ///
73 /// Defaults to:
74 /// - `channel`: `Stable`
75 /// - `mode`: `Debug`
76 /// - `edition`: `2024`
77 /// - `crate_type`: `Binary`
78 /// - `tests`: `false`
79 /// - `backtrace`: `false`
80 /// - `code`: A simple "Hello, world!" program
81 ///
82 /// # Returns
83 ///
84 /// A `ExecuteRequest` instance with standard execution defaults
85 fn default() -> Self {
86 Self {
87 channel: Channel::Stable,
88 mode: Mode::Debug,
89 edition: Edition::Edition2024,
90 crate_type: CrateType::Binary,
91 tests: false,
92 backtrace: false,
93 code: "fn main() { println!(\"Hello, world!\"); }".to_owned(),
94 }
95 }
96}
97
98/// Response structure returned after executing Rust code.
99///
100/// Contains execution success status, exit details, and output streams.
101#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
102pub struct ExecuteResponse {
103 /// Indicates whether the execution was successful.
104 pub success: bool,
105
106 /// Details about the process exit (exit code, signals, etc.).
107 #[serde(rename = "exitDetail")]
108 pub exit_detail: String,
109
110 /// Standard output generated by the program.
111 pub stdout: String,
112
113 /// Standard error output, including runtime errors and panics.
114 pub stderr: String,
115}