playground-api 0.5.1

Simple API-binding for The Rust Playground
Documentation
use super::{Channel, CrateType, Edition, Mode};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;

/// Request structure to execute Rust code on the playground.
///
/// Specifies compilation parameters and the source code to run.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ExecuteRequest<'a> {
    /// The Rust release channel to use (stable, beta, nightly).
    pub channel: Channel,

    /// The compilation mode: debug or release.
    pub mode: Mode,

    /// The Rust edition to compile and run with (2015, 2018, 2021, 2024).
    pub edition: Edition,

    /// The crate type: binary or library.
    #[serde(rename = "crateType")]
    pub crate_type: CrateType,

    /// Whether to include test code during execution.
    pub tests: bool,

    /// Whether to enable backtrace output on runtime errors.
    #[serde(default)]
    pub backtrace: bool,

    /// The Rust source code to compile and execute.
    pub code: Cow<'a, str>,
}

impl<'a> ExecuteRequest<'a> {
    /// Creates a new `ExecuteRequest`.
    ///
    /// # Arguments
    ///
    /// * `channel` - Rust release channel.
    /// * `mode` - Compilation mode (debug/release).
    /// * `edition` - Rust edition.
    /// * `crate_type` - Crate type (binary or library).
    /// * `tests` - Whether to run test code.
    /// * `backtrace` - Whether to enable backtraces.
    /// * `code` - Source code to execute.
    ///
    /// # Returns
    ///
    /// An `ExecuteRequest` initialized with the given parameters.
    pub fn new(
        channel: Channel,
        mode: Mode,
        edition: Edition,
        crate_type: CrateType,
        tests: bool,
        backtrace: bool,
        code: Cow<'a, str>,
    ) -> Self {
        Self {
            channel,
            mode,
            edition,
            crate_type,
            tests,
            backtrace,
            code,
        }
    }
}

impl<'b> super::Request for ExecuteRequest<'b> {
    fn endpoint<'a>(&self) -> super::Endpoints<'a> {
        super::Endpoints::Execute
    }
}

impl<'a> Default for ExecuteRequest<'a> {
    /// Provides a default `ExecuteRequest` configuration.
    ///
    /// Defaults to:
    /// - `channel`: `Stable`
    /// - `mode`: `Debug`
    /// - `edition`: `2024`
    /// - `crate_type`: `Binary`
    /// - `tests`: `false`
    /// - `backtrace`: `false`
    /// - `code`: A simple "Hello, world!" program
    ///
    /// # Returns
    ///
    /// A `ExecuteRequest` instance with standard execution defaults
    fn default() -> Self {
        Self {
            channel: Channel::Stable,
            mode: Mode::Debug,
            edition: Edition::Edition2024,
            crate_type: CrateType::Binary,
            tests: false,
            backtrace: false,
            code: Cow::Borrowed("fn main() { println!(\"Hello, world!\"); }"),
        }
    }
}

/// Response structure returned after executing Rust code.
///
/// Contains execution success status, exit details, and output streams.
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub struct ExecuteResponse<'a> {
    /// Indicates whether the execution was successful.
    pub success: bool,

    /// Details about the process exit (exit code, signals, etc.).
    #[serde(rename = "exitDetail")]
    pub exit_detail: Cow<'a, str>,

    /// Standard output generated by the program.
    pub stdout: Cow<'a, str>,

    /// Standard error output, including runtime errors and panics.
    pub stderr: Cow<'a, str>,
}

impl<'a> super::Response for ExecuteResponse<'a> {}