playground_api/endpoints/clippy.rs
1use super::{Channel, CrateType, Edition};
2use serde::{Deserialize, Serialize};
3use std::borrow::Cow;
4
5/// Represents a request to run Clippy (Rust linter) on the given Rust code.
6///
7/// Contains configuration options like the Rust channel, crate type, edition,
8/// and the actual source code to analyze.
9#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
10pub struct ClippyRequest<'a> {
11 /// The Rust release channel to use (stable, beta, nightly).
12 pub channel: Channel,
13
14 /// The type of crate to analyze (binary or library).
15 #[serde(rename = "crateType")]
16 pub crate_type: CrateType,
17
18 /// The Rust edition to use (2015, 2018, 2021, 2024).
19 pub edition: Edition,
20
21 /// The Rust source code to be linted by Clippy.
22 pub code: Cow<'a, str>,
23}
24
25impl<'a> ClippyRequest<'a> {
26 /// Creates a new `ClippyRequest`.
27 ///
28 /// # Arguments
29 ///
30 /// * `channel` - The Rust channel to run Clippy on.
31 /// * `crate_type` - The crate type (binary or library).
32 /// * `edition` - The Rust edition to compile with.
33 /// * `code` - The source code to analyze.
34 ///
35 /// # Returns
36 ///
37 /// A `ClippyRequest` instance configured with the given parameters.
38 pub fn new(
39 channel: Channel,
40 crate_type: CrateType,
41 edition: Edition,
42 code: Cow<'a, str>,
43 ) -> Self {
44 Self {
45 channel,
46 crate_type,
47 edition,
48 code,
49 }
50 }
51}
52
53impl<'b> super::Request for ClippyRequest<'b> {
54 fn endpoint<'a>(&self) -> super::Endpoints<'a> {
55 super::Endpoints::Clippy
56 }
57}
58
59impl<'a> Default for ClippyRequest<'a> {
60 /// Provides a default `ClippyRequest` configuration.
61 ///
62 /// Defaults to:
63 /// - `channel`: `Stable`
64 /// - `crate_type`: `Binary`
65 /// - `edition`: `2024`
66 /// - `code`: A basic "Hello, world!" program
67 ///
68 /// # Returns
69 ///
70 /// A `ClippyRequest` instance with default values for linting basic Rust code.
71 fn default() -> Self {
72 Self {
73 channel: Channel::Stable,
74 crate_type: CrateType::Binary,
75 edition: Edition::Edition2024,
76 code: Cow::Borrowed("fn main() { println!(\"Hello, world!\"); }"),
77 }
78 }
79}
80
81/// Represents the response from running Clippy on submitted code.
82///
83/// Includes success status, exit details, and output streams.
84#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
85pub struct ClippyResponse<'a> {
86 /// Whether Clippy completed successfully without errors.
87 pub success: bool,
88
89 /// Details about the process exit (exit code, signals, etc.).
90 pub exit_detail: Cow<'a, str>,
91
92 /// Standard output from Clippy (usually empty or informational).
93 pub stdout: Cow<'a, str>,
94
95 /// Standard error output containing Clippy warnings, errors, and suggestions.
96 pub stderr: Cow<'a, str>,
97}
98
99impl<'a> super::Response for ClippyResponse<'a> {}