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 Default for ClippyRequest {
48 /// Provides a default `ClippyRequest` configuration.
49 ///
50 /// Defaults to:
51 /// - `channel`: `Stable`
52 /// - `crate_type`: `Binary`
53 /// - `edition`: `2024`
54 /// - `code`: A basic "Hello, world!" program
55 ///
56 /// # Returns
57 ///
58 /// A `ClippyRequest` instance with default values for linting basic Rust code.
59 fn default() -> Self {
60 Self {
61 channel: Channel::Stable,
62 crate_type: CrateType::Binary,
63 edition: Edition::Edition2024,
64 code: "fn main() { println!(\"Hello, world!\"); }".to_owned(),
65 }
66 }
67}
68
69/// Represents the response from running Clippy on submitted code.
70///
71/// Includes success status, exit details, and output streams.
72#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
73pub struct ClippyResponse {
74 /// Whether Clippy completed successfully without errors.
75 pub success: bool,
76
77 /// Details about the process exit (exit code, signals, etc.).
78 pub exit_detail: String,
79
80 /// Standard output from Clippy (usually empty or informational).
81 pub stdout: String,
82
83 /// Standard error output containing Clippy warnings, errors, and suggestions.
84 pub stderr: String,
85}