playground-api 0.5.1

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

/// Request structure to format Rust source code via the playground.
///
/// Specifies formatting options and the source code to format.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct FormatRequest<'a> {
    /// The Rust release channel to use for formatting (stable, beta, nightly).
    pub channel: Channel,

    /// The crate type (binary or library) of the code to format.
    pub crate_type: CrateType,

    /// The Rust edition to apply for formatting rules.
    pub edition: Edition,

    /// The Rust source code that needs formatting.
    pub code: Cow<'a, str>,
}

impl<'a> FormatRequest<'a> {
    /// Creates a new `FormatRequest`.
    ///
    /// # Arguments
    ///
    /// * `channel` - Rust release channel.
    /// * `crate_type` - Crate type (binary or library).
    /// * `edition` - Rust edition to format for.
    /// * `code` - Source code to be formatted.
    ///
    /// # Returns
    ///
    /// A `FormatRequest` initialized with the given parameters.
    pub fn new(
        channel: Channel,
        crate_type: CrateType,
        edition: Edition,
        code: Cow<'a, str>,
    ) -> Self {
        Self {
            channel,
            crate_type,
            edition,
            code,
        }
    }
}

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

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

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

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

    /// The resulting formatted Rust source code.
    pub code: Cow<'a, str>,
}

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