dprint_config/json.rs
1//! Configuration for the dprint JSON plugin.
2//!
3//! See: <https://dprint.dev/plugins/json/config/>
4
5use alloc::collections::BTreeMap;
6use alloc::string::String;
7use alloc::vec::Vec;
8
9use schemars::JsonSchema;
10use serde::{Deserialize, Serialize};
11use serde_json::Value;
12
13/// See: <https://dprint.dev/plugins/json/config/>
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
15pub enum NewLineKind {
16 /// For each file, uses the newline kind found at the end of the last line.
17 #[serde(rename = "auto")]
18 Auto,
19 /// Uses carriage return, line feed.
20 #[serde(rename = "crlf")]
21 Crlf,
22 /// Uses line feed.
23 #[serde(rename = "lf")]
24 Lf,
25 /// Uses the system standard (ex. crlf on Windows).
26 #[serde(rename = "system")]
27 System,
28}
29
30/// See: <https://dprint.dev/plugins/json/config/>
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
32pub enum TrailingCommas {
33 /// Always format with trailing commas. Beware: trailing commas can cause many JSON parsers to fail.
34 #[serde(rename = "always")]
35 Always,
36 /// Use trailing commas in JSONC files and do not use trailing commas in JSON files.
37 #[serde(rename = "jsonc")]
38 Jsonc,
39 /// Keep the trailing comma if it exists.
40 #[serde(rename = "maintain")]
41 Maintain,
42 /// Never format with trailing commas.
43 #[serde(rename = "never")]
44 Never,
45}
46
47/// Configuration for the dprint [JSON](https://dprint.dev/plugins/json/config/) plugin.
48///
49/// See: <https://dprint.dev/plugins/json/config/>
50#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
51#[schemars(title = "JSON Plugin Configuration")]
52pub struct JsonConfig {
53 /// Whether the configuration is not allowed to be overridden or extended.
54 #[serde(default, skip_serializing_if = "Option::is_none")]
55 pub locked: Option<bool>,
56
57 /// File patterns to associate with this plugin.
58 #[serde(default, skip_serializing_if = "Option::is_none")]
59 pub associations: Option<Vec<String>>,
60
61 /// If arrays and objects should collapse to a single line if it would be below the line width.
62 ///
63 /// Default: `false`
64 ///
65 /// See: <https://dprint.dev/plugins/json/config/#arraypreferSingleLine>
66 #[serde(
67 default,
68 rename = "array.preferSingleLine",
69 skip_serializing_if = "Option::is_none"
70 )]
71 pub array_prefer_single_line: Option<bool>,
72
73 /// Forces a space after slashes. For example: `// comment` instead of `//comment`
74 ///
75 /// Default: `true`
76 ///
77 /// See: <https://dprint.dev/plugins/json/config/#commentLineforceSpaceAfterSlashes>
78 #[serde(
79 default,
80 rename = "commentLine.forceSpaceAfterSlashes",
81 skip_serializing_if = "Option::is_none"
82 )]
83 pub comment_line_force_space_after_slashes: Option<bool>,
84
85 /// Top level configuration that sets the configuration to what is used in Deno.
86 ///
87 /// Default: `false`
88 ///
89 /// See: <https://dprint.dev/plugins/json/config/#deno>
90 #[serde(default, skip_serializing_if = "Option::is_none")]
91 pub deno: Option<bool>,
92
93 /// The text to use for an ignore comment (ex. `// dprint-ignore`).
94 ///
95 /// Default: `"dprint-ignore"`
96 ///
97 /// See: <https://dprint.dev/plugins/json/config/#ignoreNodeCommentText>
98 #[serde(
99 default,
100 rename = "ignoreNodeCommentText",
101 alias = "ignore-node-comment-text",
102 skip_serializing_if = "Option::is_none"
103 )]
104 pub ignore_node_comment_text: Option<String>,
105
106 /// The number of characters for an indent.
107 ///
108 /// Default: `2`
109 ///
110 /// See: <https://dprint.dev/plugins/json/config/#indentWidth>
111 #[serde(
112 default,
113 rename = "indentWidth",
114 alias = "indent-width",
115 skip_serializing_if = "Option::is_none"
116 )]
117 pub indent_width: Option<u32>,
118
119 /// When `trailingCommas` is `jsonc`, treat these files as JSONC and use trailing commas (ex. `["tsconfig.json", ".vscode/settings.json"]`).
120 ///
121 /// See: <https://dprint.dev/plugins/json/config/#jsonTrailingCommaFiles>
122 #[serde(
123 default,
124 rename = "jsonTrailingCommaFiles",
125 alias = "json-trailing-comma-files",
126 skip_serializing_if = "Option::is_none"
127 )]
128 pub json_trailing_comma_files: Option<Vec<String>>,
129
130 /// The width of a line the printer will try to stay under. Note that the printer may exceed this width in certain cases.
131 ///
132 /// Default: `120`
133 ///
134 /// See: <https://dprint.dev/plugins/json/config/#lineWidth>
135 #[serde(
136 default,
137 rename = "lineWidth",
138 alias = "line-width",
139 skip_serializing_if = "Option::is_none"
140 )]
141 pub line_width: Option<u32>,
142
143 /// The kind of newline to use.
144 ///
145 /// Default: `"lf"`
146 ///
147 /// See: <https://dprint.dev/plugins/json/config/#newLineKind>
148 #[serde(
149 default,
150 rename = "newLineKind",
151 alias = "new-line-kind",
152 skip_serializing_if = "Option::is_none"
153 )]
154 pub new_line_kind: Option<NewLineKind>,
155
156 /// If arrays and objects should collapse to a single line if it would be below the line width.
157 ///
158 /// Default: `false`
159 ///
160 /// See: <https://dprint.dev/plugins/json/config/#objectpreferSingleLine>
161 #[serde(
162 default,
163 rename = "object.preferSingleLine",
164 skip_serializing_if = "Option::is_none"
165 )]
166 pub object_prefer_single_line: Option<bool>,
167
168 /// If arrays and objects should collapse to a single line if it would be below the line width.
169 ///
170 /// Default: `false`
171 ///
172 /// See: <https://dprint.dev/plugins/json/config/#preferSingleLine>
173 #[serde(
174 default,
175 rename = "preferSingleLine",
176 alias = "prefer-single-line",
177 skip_serializing_if = "Option::is_none"
178 )]
179 pub prefer_single_line: Option<bool>,
180
181 /// Whether to use trailing commas.
182 ///
183 /// Default: `"jsonc"`
184 ///
185 /// See: <https://dprint.dev/plugins/json/config/#trailingCommas>
186 #[serde(
187 default,
188 rename = "trailingCommas",
189 alias = "trailing-commas",
190 skip_serializing_if = "Option::is_none"
191 )]
192 pub trailing_commas: Option<TrailingCommas>,
193
194 /// Whether to use tabs (true) or spaces (false).
195 ///
196 /// Default: `false`
197 ///
198 /// See: <https://dprint.dev/plugins/json/config/#useTabs>
199 #[serde(
200 default,
201 rename = "useTabs",
202 alias = "use-tabs",
203 skip_serializing_if = "Option::is_none"
204 )]
205 pub use_tabs: Option<bool>,
206
207 /// Additional plugin-specific settings not covered by the typed fields.
208 #[serde(flatten)]
209 pub extra: BTreeMap<String, Value>,
210}