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 to expand macros in a given Rust code snippet.
///
/// Includes the code and the selected Rust edition.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MacroExpansionRequest<'a> {
    /// The Rust code containing macros to expand.
    pub code: Cow<'a, str>,
    /// The Rust edition to use for macro expansion.
    pub edition: Edition,
}

impl<'a> MacroExpansionRequest<'a> {
    /// Creates a new [`MacroExpansionRequest`] with the given code and edition.
    ///
    /// # Arguments
    ///
    /// * `code` - The Rust code to analyze.
    /// * `edition` - The Rust edition to use (e.g., 2018, 2021, 2024).
    ///
    /// # Returns
    ///
    /// A new [`MacroExpansionRequest`] instance.
    pub fn new(code: Cow<'a, str>, edition: Edition) -> Self {
        Self { code, edition }
    }
}

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

impl<'a> Default for MacroExpansionRequest<'a> {
    /// Returns a default [`MacroExpansionRequest`] with a simple `println!` example
    /// and the 2024 edition.
    fn default() -> Self {
        Self {
            code: Cow::Borrowed("fn main() { println!(\"Hello, world!\"); }"),
            edition: Edition::Edition2024,
        }
    }
}

/// A response from the Rust playground's macro expansion service.
///
/// Contains the macro-expanded output and status information.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct MacroExpansionResponse<'a> {
    /// Indicates whether macro expansion was successful.
    pub success: bool,
    /// Detailed information about the macro expansion process.
    pub exit_detail: Cow<'a, str>,
    /// The standard output from the macro expansion.
    pub stdout: Cow<'a, str>,
    /// The standard error from the macro expansion.
    pub stderr: Cow<'a, str>,
}

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