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