playground_api/endpoints/
format.rs

1use super::{Channel, CrateType, Edition};
2use serde::{Deserialize, Serialize};
3
4/// Request structure to format Rust source code via the playground.
5///
6/// Specifies formatting options and the source code to format.
7#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
8pub struct FormatRequest {
9    /// The Rust release channel to use for formatting (stable, beta, nightly).
10    pub channel: Channel,
11
12    /// The crate type (binary or library) of the code to format.
13    pub crate_type: CrateType,
14
15    /// The Rust edition to apply for formatting rules.
16    pub edition: Edition,
17
18    /// The Rust source code that needs formatting.
19    pub code: String,
20}
21
22impl FormatRequest {
23    /// Creates a new `FormatRequest`.
24    ///
25    /// # Arguments
26    ///
27    /// * `channel` - Rust release channel.
28    /// * `crate_type` - Crate type (binary or library).
29    /// * `edition` - Rust edition to format for.
30    /// * `code` - Source code to be formatted.
31    ///
32    /// # Returns
33    ///
34    /// A `FormatRequest` initialized with the given parameters.
35    pub fn new(channel: Channel, crate_type: CrateType, edition: Edition, code: String) -> Self {
36        Self {
37            channel,
38            crate_type,
39            edition,
40            code,
41        }
42    }
43}
44
45impl super::Request for FormatRequest {
46    fn endpoint<'a>(&self) -> super::Endpoints<'a> {
47        super::Endpoints::Format
48    }
49}
50
51impl Default for FormatRequest {
52    /// Provides a default `FormatRequest` configuration.
53    ///
54    /// Defaults to:
55    /// - `channel`: `Stable`
56    /// - `crate_type`: `Binary`
57    /// - `edition`: `2024`
58    /// - `code`: A simple "Hello, world!" program
59    ///
60    /// # Returns
61    ///
62    /// A `FormatRequest` instance with sensible defaults for formatting Rust code.
63    fn default() -> Self {
64        Self {
65            channel: Channel::Stable,
66            crate_type: CrateType::Binary,
67            edition: Edition::Edition2024,
68            code: "fn main() { println!(\"Hello, world!\"); }".to_owned(),
69        }
70    }
71}
72
73/// Response structure returned after formatting Rust code.
74///
75/// Contains success status, exit details, and the formatted code.
76#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
77pub struct FormatResponse {
78    /// Indicates whether formatting was successful.
79    pub success: bool,
80
81    /// Details about the formatting process exit (exit code, etc.).
82    #[serde(rename = "exitDetail")]
83    pub exit_detail: String,
84
85    /// The resulting formatted Rust source code.
86    pub code: String,
87}
88
89impl super::Response for FormatResponse {}
90