playground_api/endpoints/
macro_expansion.rs

1use super::Edition;
2use serde::{Deserialize, Serialize};
3
4/// A request to expand macros in a given Rust code snippet.
5///
6/// Includes the code and the selected Rust edition.
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
8pub struct MacroExpansionRequest {
9    /// The Rust code containing macros to expand.
10    pub code: String,
11    /// The Rust edition to use for macro expansion.
12    pub edition: Edition,
13}
14
15impl MacroExpansionRequest {
16    /// Creates a new [`MacroExpansionRequest`] with the given code and edition.
17    ///
18    /// # Arguments
19    ///
20    /// * `code` - The Rust code to analyze.
21    /// * `edition` - The Rust edition to use (e.g., 2018, 2021, 2024).
22    ///
23    /// # Returns
24    ///
25    /// A new [`MacroExpansionRequest`] instance.
26    pub fn new(code: String, edition: Edition) -> Self {
27        Self { code, edition }
28    }
29}
30
31impl super::Request for MacroExpansionRequest {
32    fn endpoint<'a>(&self) -> super::Endpoints<'a> {
33        super::Endpoints::MacroExpansion
34    }
35}
36
37impl Default for MacroExpansionRequest {
38    /// Returns a default [`MacroExpansionRequest`] with a simple `println!` example
39    /// and the 2024 edition.
40    fn default() -> Self {
41        Self {
42            code: "fn main() { println!(\"Hello, world!\"); }".to_owned(),
43            edition: Edition::Edition2024,
44        }
45    }
46}
47
48/// A response from the Rust playground's macro expansion service.
49///
50/// Contains the macro-expanded output and status information.
51#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
52pub struct MacroExpansionResponse {
53    /// Indicates whether macro expansion was successful.
54    pub success: bool,
55    /// Detailed information about the macro expansion process.
56    pub exit_detail: String,
57    /// The standard output from the macro expansion.
58    pub stdout: String,
59    /// The standard error from the macro expansion.
60    pub stderr: String,
61}
62
63impl super::Response for MacroExpansionResponse {}