playground-api 0.5.1

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

/// A request structure for running Rust code under Miri, the Rust interpreter for detecting undefined behavior.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MiriRequest<'a> {
    /// The Rust source code to be interpreted.
    pub code: Cow<'a, str>,

    /// The Rust edition to use (e.g., 2021, 2024).
    pub edition: Edition,

    /// Whether the request is for running tests instead of the main function.
    pub tests: bool,

    /// Optional memory aliasing model used by Miri (e.g., `stacked` or `tree`).
    #[serde(rename = "aliasingModel")]
    pub aliasing_model: Option<AliasingModel>,
}

impl<'a> MiriRequest<'a> {
    /// Creates a new [`MiriRequest`] with the provided parameters.
    ///
    /// # Parameters
    /// - `code`: The Rust source code.
    /// - `edition`: The Rust edition to use.
    /// - `tests`: Whether to run tests.
    /// - `aliasing_model`: Optional aliasing model for memory interpretation.
    pub fn new(
        code: Cow<'a, str>,
        edition: Edition,
        tests: bool,
        aliasing_model: Option<AliasingModel>,
    ) -> Self {
        Self {
            code,
            edition,
            tests,
            aliasing_model,
        }
    }
}

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

impl<'a> Default for MiriRequest<'a> {
    /// Returns a default [`MiriRequest`] using `Edition2024`, no tests,
    /// and the `Stacked` aliasing model.
    fn default() -> Self {
        Self {
            code: Cow::Borrowed("fn main() { println!(\"Hello, world!\"); }"),
            edition: Edition::Edition2024,
            tests: false,
            aliasing_model: Some(AliasingModel::Stacked),
        }
    }
}

/// The response returned after executing a Miri request.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MiriResponse<'a> {
    /// Indicates whether execution was successful.
    pub success: bool,

    /// Additional detail about how the process exited.
    pub exit_detail: Cow<'a, str>,

    /// The standard output from the Miri execution.
    pub stdout: Cow<'a, str>,

    /// The standard error from the Miri execution.
    pub stderr: Cow<'a, str>,
}

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

/// The aliasing model used by Miri to simulate pointer and memory behavior.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "poise-bot", derive(poise::ChoiceParameter))]
#[serde(rename_all = "lowercase")]
pub enum AliasingModel {
    /// Uses the Stacked Borrows model for aliasing checks.
    Stacked,

    /// Uses the Tree Borrows model for aliasing checks.
    Tree,
}