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