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 Default for FormatRequest {
46    /// Provides a default `FormatRequest` configuration.
47    ///
48    /// Defaults to:
49    /// - `channel`: `Stable`
50    /// - `crate_type`: `Binary`
51    /// - `edition`: `2024`
52    /// - `code`: A simple "Hello, world!" program
53    ///
54    /// # Returns
55    ///
56    /// A `FormatRequest` instance with sensible defaults for formatting Rust code.
57    fn default() -> Self {
58        Self {
59            channel: Channel::Stable,
60            crate_type: CrateType::Binary,
61            edition: Edition::Edition2024,
62            code: "fn main() { println!(\"Hello, world!\"); }".to_owned(),
63        }
64    }
65}
66
67/// Response structure returned after formatting Rust code.
68///
69/// Contains success status, exit details, and the formatted code.
70#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
71pub struct FormatResponse {
72    /// Indicates whether formatting was successful.
73    pub success: bool,
74
75    /// Details about the formatting process exit (exit code, etc.).
76    #[serde(rename = "exitDetail")]
77    pub exit_detail: String,
78
79    /// The resulting formatted Rust source code.
80    pub code: String,
81}