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