@owenlamont/ryl 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/.github/CODEOWNERS +1 -0
  2. package/.github/dependabot.yml +13 -0
  3. package/.github/workflows/ci.yml +107 -0
  4. package/.github/workflows/release.yml +613 -0
  5. package/.github/workflows/update_dependencies.yml +61 -0
  6. package/.github/workflows/update_linters.yml +56 -0
  7. package/.pre-commit-config.yaml +87 -0
  8. package/.yamllint +4 -0
  9. package/AGENTS.md +200 -0
  10. package/Cargo.lock +908 -0
  11. package/Cargo.toml +32 -0
  12. package/LICENSE +21 -0
  13. package/README.md +230 -0
  14. package/bin/ryl.js +1 -0
  15. package/clippy.toml +1 -0
  16. package/docs/config-presets.md +100 -0
  17. package/img/benchmark-5x5-5runs.svg +2176 -0
  18. package/package.json +28 -0
  19. package/pyproject.toml +42 -0
  20. package/ruff.toml +107 -0
  21. package/rumdl.toml +20 -0
  22. package/rust-toolchain.toml +3 -0
  23. package/rustfmt.toml +3 -0
  24. package/scripts/benchmark_perf_vs_yamllint.py +400 -0
  25. package/scripts/coverage-missing.ps1 +80 -0
  26. package/scripts/coverage-missing.sh +60 -0
  27. package/src/bin/discover_config_bin.rs +24 -0
  28. package/src/cli_support.rs +33 -0
  29. package/src/conf/mod.rs +85 -0
  30. package/src/config.rs +2099 -0
  31. package/src/decoder.rs +326 -0
  32. package/src/discover.rs +31 -0
  33. package/src/lib.rs +19 -0
  34. package/src/lint.rs +558 -0
  35. package/src/main.rs +535 -0
  36. package/src/migrate.rs +233 -0
  37. package/src/rules/anchors.rs +517 -0
  38. package/src/rules/braces.rs +77 -0
  39. package/src/rules/brackets.rs +77 -0
  40. package/src/rules/colons.rs +475 -0
  41. package/src/rules/commas.rs +372 -0
  42. package/src/rules/comments.rs +299 -0
  43. package/src/rules/comments_indentation.rs +243 -0
  44. package/src/rules/document_end.rs +175 -0
  45. package/src/rules/document_start.rs +84 -0
  46. package/src/rules/empty_lines.rs +152 -0
  47. package/src/rules/empty_values.rs +255 -0
  48. package/src/rules/float_values.rs +259 -0
  49. package/src/rules/flow_collection.rs +562 -0
  50. package/src/rules/hyphens.rs +104 -0
  51. package/src/rules/indentation.rs +803 -0
  52. package/src/rules/key_duplicates.rs +218 -0
  53. package/src/rules/key_ordering.rs +303 -0
  54. package/src/rules/line_length.rs +326 -0
  55. package/src/rules/mod.rs +25 -0
  56. package/src/rules/new_line_at_end_of_file.rs +23 -0
  57. package/src/rules/new_lines.rs +95 -0
  58. package/src/rules/octal_values.rs +121 -0
  59. package/src/rules/quoted_strings.rs +577 -0
  60. package/src/rules/span_utils.rs +37 -0
  61. package/src/rules/trailing_spaces.rs +65 -0
  62. package/src/rules/truthy.rs +420 -0
  63. package/tests/brackets_carriage_return.rs +114 -0
  64. package/tests/build_global_cfg_error.rs +23 -0
  65. package/tests/cli_anchors_rule.rs +143 -0
  66. package/tests/cli_braces_rule.rs +104 -0
  67. package/tests/cli_brackets_rule.rs +104 -0
  68. package/tests/cli_colons_rule.rs +65 -0
  69. package/tests/cli_commas_rule.rs +104 -0
  70. package/tests/cli_comments_indentation_rule.rs +61 -0
  71. package/tests/cli_comments_rule.rs +67 -0
  72. package/tests/cli_config_data_error.rs +30 -0
  73. package/tests/cli_config_flags.rs +66 -0
  74. package/tests/cli_config_migrate.rs +229 -0
  75. package/tests/cli_document_end_rule.rs +92 -0
  76. package/tests/cli_document_start_rule.rs +92 -0
  77. package/tests/cli_empty_lines_rule.rs +87 -0
  78. package/tests/cli_empty_values_rule.rs +68 -0
  79. package/tests/cli_env_config.rs +34 -0
  80. package/tests/cli_exit_and_errors.rs +41 -0
  81. package/tests/cli_file_encoding.rs +203 -0
  82. package/tests/cli_float_values_rule.rs +64 -0
  83. package/tests/cli_format_options.rs +316 -0
  84. package/tests/cli_global_cfg_relaxed.rs +20 -0
  85. package/tests/cli_hyphens_rule.rs +104 -0
  86. package/tests/cli_indentation_rule.rs +65 -0
  87. package/tests/cli_invalid_project_config.rs +39 -0
  88. package/tests/cli_key_duplicates_rule.rs +104 -0
  89. package/tests/cli_key_ordering_rule.rs +59 -0
  90. package/tests/cli_line_length_rule.rs +85 -0
  91. package/tests/cli_list_files.rs +29 -0
  92. package/tests/cli_new_line_rule.rs +141 -0
  93. package/tests/cli_new_lines_rule.rs +119 -0
  94. package/tests/cli_octal_values_rule.rs +60 -0
  95. package/tests/cli_quoted_strings_rule.rs +47 -0
  96. package/tests/cli_toml_config.rs +119 -0
  97. package/tests/cli_trailing_spaces_rule.rs +77 -0
  98. package/tests/cli_truthy_rule.rs +83 -0
  99. package/tests/cli_yaml_files_negation.rs +45 -0
  100. package/tests/colons_rule.rs +303 -0
  101. package/tests/common/compat.rs +114 -0
  102. package/tests/common/fake_env.rs +93 -0
  103. package/tests/common/mod.rs +1 -0
  104. package/tests/conf_builtin.rs +9 -0
  105. package/tests/config_anchors.rs +84 -0
  106. package/tests/config_braces.rs +121 -0
  107. package/tests/config_brackets.rs +127 -0
  108. package/tests/config_commas.rs +79 -0
  109. package/tests/config_comments.rs +65 -0
  110. package/tests/config_comments_indentation.rs +20 -0
  111. package/tests/config_deep_merge_nonstring_key.rs +24 -0
  112. package/tests/config_document_end.rs +54 -0
  113. package/tests/config_document_start.rs +55 -0
  114. package/tests/config_empty_lines.rs +48 -0
  115. package/tests/config_empty_values.rs +35 -0
  116. package/tests/config_env_errors.rs +23 -0
  117. package/tests/config_env_invalid_inline.rs +15 -0
  118. package/tests/config_env_missing.rs +63 -0
  119. package/tests/config_env_shim.rs +301 -0
  120. package/tests/config_explicit_file_parse_error.rs +55 -0
  121. package/tests/config_extended_features.rs +225 -0
  122. package/tests/config_extends_inline.rs +185 -0
  123. package/tests/config_extends_sequence.rs +18 -0
  124. package/tests/config_find_project_home_boundary.rs +54 -0
  125. package/tests/config_find_project_two_files_in_cwd.rs +47 -0
  126. package/tests/config_float_values.rs +34 -0
  127. package/tests/config_from_yaml_paths.rs +32 -0
  128. package/tests/config_hyphens.rs +51 -0
  129. package/tests/config_ignore_errors.rs +243 -0
  130. package/tests/config_ignore_overrides.rs +83 -0
  131. package/tests/config_indentation.rs +65 -0
  132. package/tests/config_invalid_globs.rs +16 -0
  133. package/tests/config_invalid_types.rs +19 -0
  134. package/tests/config_key_duplicates.rs +34 -0
  135. package/tests/config_key_ordering.rs +70 -0
  136. package/tests/config_line_length.rs +65 -0
  137. package/tests/config_locale.rs +111 -0
  138. package/tests/config_merge.rs +26 -0
  139. package/tests/config_new_lines.rs +89 -0
  140. package/tests/config_octal_values.rs +33 -0
  141. package/tests/config_quoted_strings.rs +195 -0
  142. package/tests/config_rule_level.rs +147 -0
  143. package/tests/config_rules_non_string_keys.rs +23 -0
  144. package/tests/config_scalar_overrides.rs +27 -0
  145. package/tests/config_to_toml.rs +110 -0
  146. package/tests/config_toml_coverage.rs +80 -0
  147. package/tests/config_toml_discovery.rs +304 -0
  148. package/tests/config_trailing_spaces.rs +152 -0
  149. package/tests/config_truthy.rs +77 -0
  150. package/tests/config_yaml_files.rs +62 -0
  151. package/tests/config_yaml_files_all_non_string.rs +15 -0
  152. package/tests/config_yaml_files_empty.rs +30 -0
  153. package/tests/coverage_commas.rs +46 -0
  154. package/tests/decoder_decode.rs +338 -0
  155. package/tests/discover_config_bin_all.rs +66 -0
  156. package/tests/discover_config_bin_env_invalid_yaml.rs +26 -0
  157. package/tests/discover_config_bin_project_config_parse_error.rs +24 -0
  158. package/tests/discover_config_bin_user_global_error.rs +26 -0
  159. package/tests/discover_module.rs +30 -0
  160. package/tests/discover_per_file_dir.rs +10 -0
  161. package/tests/discover_per_file_project_config_error.rs +21 -0
  162. package/tests/float_values.rs +43 -0
  163. package/tests/lint_multi_errors.rs +32 -0
  164. package/tests/main_yaml_ok_filtering.rs +30 -0
  165. package/tests/migrate_module.rs +259 -0
  166. package/tests/resolve_ctx_empty_parent.rs +16 -0
  167. package/tests/rule_anchors.rs +442 -0
  168. package/tests/rule_braces.rs +258 -0
  169. package/tests/rule_brackets.rs +217 -0
  170. package/tests/rule_commas.rs +205 -0
  171. package/tests/rule_comments.rs +197 -0
  172. package/tests/rule_comments_indentation.rs +127 -0
  173. package/tests/rule_document_end.rs +118 -0
  174. package/tests/rule_document_start.rs +60 -0
  175. package/tests/rule_empty_lines.rs +96 -0
  176. package/tests/rule_empty_values.rs +102 -0
  177. package/tests/rule_float_values.rs +109 -0
  178. package/tests/rule_hyphens.rs +65 -0
  179. package/tests/rule_indentation.rs +455 -0
  180. package/tests/rule_key_duplicates.rs +76 -0
  181. package/tests/rule_key_ordering.rs +207 -0
  182. package/tests/rule_line_length.rs +200 -0
  183. package/tests/rule_new_lines.rs +51 -0
  184. package/tests/rule_octal_values.rs +53 -0
  185. package/tests/rule_quoted_strings.rs +290 -0
  186. package/tests/rule_trailing_spaces.rs +41 -0
  187. package/tests/rule_truthy.rs +236 -0
  188. package/tests/user_global_invalid_yaml.rs +32 -0
  189. package/tests/yamllint_compat_anchors.rs +280 -0
  190. package/tests/yamllint_compat_braces.rs +411 -0
  191. package/tests/yamllint_compat_brackets.rs +364 -0
  192. package/tests/yamllint_compat_colons.rs +298 -0
  193. package/tests/yamllint_compat_colors.rs +80 -0
  194. package/tests/yamllint_compat_commas.rs +375 -0
  195. package/tests/yamllint_compat_comments.rs +167 -0
  196. package/tests/yamllint_compat_comments_indentation.rs +281 -0
  197. package/tests/yamllint_compat_config.rs +170 -0
  198. package/tests/yamllint_compat_document_end.rs +243 -0
  199. package/tests/yamllint_compat_document_start.rs +136 -0
  200. package/tests/yamllint_compat_empty_lines.rs +117 -0
  201. package/tests/yamllint_compat_empty_values.rs +179 -0
  202. package/tests/yamllint_compat_float_values.rs +216 -0
  203. package/tests/yamllint_compat_hyphens.rs +223 -0
  204. package/tests/yamllint_compat_indentation.rs +398 -0
  205. package/tests/yamllint_compat_key_duplicates.rs +139 -0
  206. package/tests/yamllint_compat_key_ordering.rs +170 -0
  207. package/tests/yamllint_compat_line_length.rs +375 -0
  208. package/tests/yamllint_compat_list.rs +127 -0
  209. package/tests/yamllint_compat_new_line.rs +133 -0
  210. package/tests/yamllint_compat_newline_types.rs +185 -0
  211. package/tests/yamllint_compat_octal_values.rs +172 -0
  212. package/tests/yamllint_compat_quoted_strings.rs +154 -0
  213. package/tests/yamllint_compat_syntax.rs +200 -0
  214. package/tests/yamllint_compat_trailing_spaces.rs +162 -0
  215. package/tests/yamllint_compat_truthy.rs +130 -0
  216. package/tests/yamllint_compat_yaml_files.rs +81 -0
  217. package/typos.toml +2 -0
@@ -0,0 +1,803 @@
1
+ use crate::config::YamlLintConfig;
2
+
3
+ pub const ID: &str = "indentation";
4
+
5
+ #[derive(Debug, Clone, PartialEq, Eq)]
6
+ pub struct Violation {
7
+ pub line: usize,
8
+ pub column: usize,
9
+ pub message: String,
10
+ }
11
+
12
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
13
+ pub struct Config {
14
+ spaces: SpacesSetting,
15
+ indent_sequences: IndentSequencesSetting,
16
+ check_multi_line_strings: bool,
17
+ }
18
+
19
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
20
+ pub enum SpacesSetting {
21
+ Fixed(usize),
22
+ Consistent,
23
+ }
24
+
25
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
26
+ pub enum IndentSequencesSetting {
27
+ True,
28
+ False,
29
+ Whatever,
30
+ Consistent,
31
+ }
32
+
33
+ impl Config {
34
+ #[must_use]
35
+ pub fn resolve(cfg: &YamlLintConfig) -> Self {
36
+ let spaces =
37
+ cfg.rule_option(ID, "spaces")
38
+ .map_or(SpacesSetting::Consistent, |node| {
39
+ node.as_integer()
40
+ .map_or(SpacesSetting::Consistent, |value| {
41
+ let non_negative = value.max(0);
42
+ let fixed =
43
+ usize::try_from(non_negative).unwrap_or(usize::MAX);
44
+ SpacesSetting::Fixed(fixed)
45
+ })
46
+ });
47
+
48
+ let indent_sequences = cfg.rule_option(ID, "indent-sequences").map_or(
49
+ IndentSequencesSetting::True,
50
+ |node| {
51
+ if let Some(choice) = node.as_str() {
52
+ return if choice == "whatever" {
53
+ IndentSequencesSetting::Whatever
54
+ } else {
55
+ IndentSequencesSetting::Consistent
56
+ };
57
+ }
58
+
59
+ if node.as_bool() == Some(false) {
60
+ IndentSequencesSetting::False
61
+ } else {
62
+ IndentSequencesSetting::True
63
+ }
64
+ },
65
+ );
66
+
67
+ let check_multi_line_strings = cfg
68
+ .rule_option(ID, "check-multi-line-strings")
69
+ .and_then(saphyr::YamlOwned::as_bool)
70
+ .unwrap_or(false);
71
+
72
+ Self {
73
+ spaces,
74
+ indent_sequences,
75
+ check_multi_line_strings,
76
+ }
77
+ }
78
+
79
+ #[must_use]
80
+ pub const fn new_for_tests(
81
+ spaces: SpacesSetting,
82
+ indent_sequences: IndentSequencesSetting,
83
+ check_multi_line_strings: bool,
84
+ ) -> Self {
85
+ Self {
86
+ spaces,
87
+ indent_sequences,
88
+ check_multi_line_strings,
89
+ }
90
+ }
91
+ }
92
+
93
+ #[must_use]
94
+ pub fn check(buffer: &str, cfg: &Config) -> Vec<Violation> {
95
+ let mut analyzer = Analyzer::new(buffer, cfg);
96
+ analyzer.run();
97
+ analyzer.diagnostics
98
+ }
99
+
100
+ struct Analyzer<'a> {
101
+ cfg: &'a Config,
102
+ lines: Vec<&'a str>,
103
+ contexts: Vec<Context>,
104
+ sequence_expectations: Vec<Option<bool>>,
105
+ spaces: SpacesRuntime,
106
+ indent_seq: IndentSequencesRuntime,
107
+ pending_child: Option<ContextKind>,
108
+ multiline: Option<MultilineState>,
109
+ compact_sequence_parent_indent: Option<usize>,
110
+ compact_flow_mapping: Option<CompactFlowMapping>,
111
+ prev_line_kind: Option<LineKind>,
112
+ diagnostics: Vec<Violation>,
113
+ }
114
+
115
+ impl<'a> Analyzer<'a> {
116
+ fn new(text: &'a str, cfg: &'a Config) -> Self {
117
+ let lines: Vec<&str> = text.split_inclusive(['\n']).collect();
118
+ Self {
119
+ cfg,
120
+ lines,
121
+ contexts: vec![Context {
122
+ indent: 0,
123
+ kind: ContextKind::Root,
124
+ }],
125
+ sequence_expectations: vec![None],
126
+ spaces: SpacesRuntime::new(cfg.spaces),
127
+ indent_seq: IndentSequencesRuntime::new(cfg.indent_sequences),
128
+ pending_child: None,
129
+ multiline: None,
130
+ compact_sequence_parent_indent: None,
131
+ compact_flow_mapping: None,
132
+ prev_line_kind: None,
133
+ diagnostics: Vec::new(),
134
+ }
135
+ }
136
+
137
+ fn run(&mut self) {
138
+ for line_index in 0..self.lines.len() {
139
+ let line_number = line_index + 1;
140
+ let raw_line = self.lines[line_index];
141
+ self.process_line(line_number, raw_line);
142
+ }
143
+ }
144
+
145
+ fn process_line(&mut self, line_number: usize, raw: &str) {
146
+ let line = raw.trim_end_matches(['\r', '\n']);
147
+ let (indent, content) = split_indent(line);
148
+ if self
149
+ .compact_sequence_parent_indent
150
+ .is_some_and(|parent| indent <= parent)
151
+ {
152
+ self.compact_sequence_parent_indent = None;
153
+ }
154
+ if self
155
+ .compact_flow_mapping
156
+ .is_some_and(|state| indent <= state.parent_indent)
157
+ {
158
+ self.compact_flow_mapping = None;
159
+ }
160
+
161
+ if let Some(state) = &self.multiline
162
+ && indent <= state.base_indent
163
+ && !content.trim().is_empty()
164
+ {
165
+ self.multiline = None;
166
+ }
167
+
168
+ if content.trim().is_empty() {
169
+ self.prev_line_kind = Some(LineKind::Other);
170
+ return;
171
+ }
172
+
173
+ if let Some(state) = self.multiline.as_mut() {
174
+ if !self.cfg.check_multi_line_strings {
175
+ self.prev_line_kind = Some(LineKind::Other);
176
+ return;
177
+ }
178
+ let expected = state.expected_indent(indent, &mut self.spaces);
179
+ if indent != expected {
180
+ self.diagnostics.push(Violation {
181
+ line: line_number,
182
+ column: indent + 1,
183
+ message: format!(
184
+ "wrong indentation: expected {expected} but found {indent}"
185
+ ),
186
+ });
187
+ }
188
+ return;
189
+ }
190
+
191
+ if content.trim_start().starts_with('#') {
192
+ return;
193
+ }
194
+
195
+ let analysis = LineAnalysis::analyze(content);
196
+ let compact_mapping_continuation =
197
+ self.is_compact_mapping_continuation(indent, analysis);
198
+
199
+ let Some(pushing_child) = self.update_context_for_indent(
200
+ line_number,
201
+ indent,
202
+ analysis,
203
+ compact_mapping_continuation,
204
+ ) else {
205
+ return;
206
+ };
207
+
208
+ if pushing_child
209
+ && analysis.is_sequence_entry()
210
+ && let Some(ctx) = self.contexts.last_mut()
211
+ {
212
+ ctx.kind = ContextKind::Sequence;
213
+ }
214
+
215
+ if analysis.is_sequence_entry()
216
+ && self
217
+ .compact_sequence_parent_indent
218
+ .is_none_or(|parent| indent <= parent)
219
+ {
220
+ self.check_sequence_indent(indent, line_number);
221
+ }
222
+
223
+ if analysis.is_mapping_key()
224
+ && let Some(ctx) = self.contexts.last_mut()
225
+ {
226
+ ctx.kind = analysis.context_kind();
227
+ }
228
+
229
+ if analysis.starts_multiline {
230
+ self.multiline = Some(MultilineState::new(indent));
231
+ }
232
+
233
+ if analysis.opens_child_context() {
234
+ self.pending_child = Some(analysis.context_kind());
235
+ } else {
236
+ self.pending_child = None;
237
+ }
238
+
239
+ if is_compact_sequence_start(content) {
240
+ self.compact_sequence_parent_indent = Some(indent);
241
+ }
242
+ if let Some(continuation_indent) =
243
+ compact_flow_mapping_continuation_indent(content, indent)
244
+ {
245
+ self.compact_flow_mapping = Some(CompactFlowMapping {
246
+ parent_indent: indent,
247
+ continuation_indent,
248
+ });
249
+ }
250
+
251
+ self.prev_line_kind = Some(analysis.kind);
252
+ }
253
+
254
+ fn current_indent(&self) -> usize {
255
+ self.contexts.last().map_or(0, |ctx| ctx.indent)
256
+ }
257
+
258
+ fn update_context_for_indent(
259
+ &mut self,
260
+ line_number: usize,
261
+ indent: usize,
262
+ analysis: LineAnalysis,
263
+ compact_mapping_continuation: bool,
264
+ ) -> Option<bool> {
265
+ while self.current_indent() > indent {
266
+ self.contexts.pop();
267
+ self.sequence_expectations.pop();
268
+ }
269
+
270
+ let parent_indent = self.current_indent();
271
+ if indent > parent_indent {
272
+ if matches!(analysis.kind, LineKind::Other)
273
+ && matches!(
274
+ self.prev_line_kind,
275
+ Some(LineKind::Sequence | LineKind::Mapping { .. })
276
+ )
277
+ {
278
+ return None;
279
+ }
280
+ let kind = self
281
+ .pending_child
282
+ .take()
283
+ .unwrap_or_else(|| analysis.context_kind());
284
+ self.contexts.push(Context { indent, kind });
285
+ self.sequence_expectations.push(None);
286
+ if !compact_mapping_continuation {
287
+ self.spaces.observe_increase(
288
+ parent_indent,
289
+ indent,
290
+ line_number,
291
+ &mut self.diagnostics,
292
+ );
293
+ }
294
+ Some(true)
295
+ } else {
296
+ if !compact_mapping_continuation {
297
+ self.spaces
298
+ .observe_indent(indent, line_number, &mut self.diagnostics);
299
+ }
300
+ self.pending_child = None;
301
+ Some(false)
302
+ }
303
+ }
304
+
305
+ fn is_compact_mapping_continuation(
306
+ &self,
307
+ indent: usize,
308
+ analysis: LineAnalysis,
309
+ ) -> bool {
310
+ if !analysis.is_mapping_key() {
311
+ return false;
312
+ }
313
+ if self
314
+ .compact_flow_mapping
315
+ .is_some_and(|state| state.continuation_indent == indent)
316
+ {
317
+ return true;
318
+ }
319
+ self.contexts.iter().rev().any(|ctx| {
320
+ let ContextKind::Mapping { sequence_offset } = ctx.kind else {
321
+ return false;
322
+ };
323
+ sequence_offset > 0 && ctx.indent.saturating_add(sequence_offset) == indent
324
+ })
325
+ }
326
+
327
+ fn find_mapping_parent_indent(
328
+ &self,
329
+ current_indent: usize,
330
+ ) -> Option<(usize, usize)> {
331
+ let mut saw_mapping = false;
332
+ let mut last_mapping_index = None;
333
+ for (idx, ctx) in self.contexts.iter().enumerate().rev() {
334
+ let ContextKind::Mapping { sequence_offset } = ctx.kind else {
335
+ continue;
336
+ };
337
+ saw_mapping = true;
338
+ last_mapping_index = Some(idx);
339
+ let base_indent = ctx.indent.saturating_add(sequence_offset);
340
+ if base_indent <= current_indent {
341
+ return Some((idx, base_indent));
342
+ }
343
+ }
344
+ if saw_mapping {
345
+ Some((last_mapping_index.unwrap(), current_indent))
346
+ } else {
347
+ None
348
+ }
349
+ }
350
+
351
+ fn check_sequence_indent(&mut self, indent: usize, line_number: usize) {
352
+ let Some((ctx_index, parent_indent)) = self.find_mapping_parent_indent(indent)
353
+ else {
354
+ return;
355
+ };
356
+
357
+ let is_indented = indent > parent_indent;
358
+ let expected = self
359
+ .spaces
360
+ .expected_step()
361
+ .map(|step| parent_indent.saturating_add(step));
362
+
363
+ let state = &mut self.sequence_expectations[ctx_index];
364
+ if let Some(message) =
365
+ self.indent_seq
366
+ .check(parent_indent, indent, is_indented, expected, state)
367
+ {
368
+ self.diagnostics.push(Violation {
369
+ line: line_number,
370
+ column: indent + 1,
371
+ message,
372
+ });
373
+ }
374
+ }
375
+ }
376
+
377
+ #[derive(Debug, Clone, Copy)]
378
+ struct Context {
379
+ indent: usize,
380
+ kind: ContextKind,
381
+ }
382
+
383
+ #[derive(Debug, Clone, Copy)]
384
+ struct CompactFlowMapping {
385
+ parent_indent: usize,
386
+ continuation_indent: usize,
387
+ }
388
+
389
+ #[derive(Debug, Clone, Copy)]
390
+ enum ContextKind {
391
+ Root,
392
+ Mapping { sequence_offset: usize },
393
+ Sequence,
394
+ Other,
395
+ }
396
+
397
+ #[derive(Debug, Clone, Copy)]
398
+ struct LineAnalysis {
399
+ kind: LineKind,
400
+ starts_multiline: bool,
401
+ is_sequence_entry: bool,
402
+ }
403
+
404
+ #[derive(Debug, Clone, Copy)]
405
+ enum LineKind {
406
+ Mapping {
407
+ opens_block: bool,
408
+ sequence_offset: usize,
409
+ },
410
+ Sequence,
411
+ Other,
412
+ }
413
+
414
+ impl LineAnalysis {
415
+ fn analyze(content: &str) -> Self {
416
+ let stripped = strip_trailing_comment(content);
417
+ let (core, _comment) = stripped;
418
+ let trimmed = core.trim();
419
+ let is_sequence_entry = is_sequence_entry(trimmed);
420
+ let (is_mapping_key, opens_block) = classify_mapping(trimmed);
421
+ let kind = if is_mapping_key {
422
+ LineKind::Mapping {
423
+ opens_block,
424
+ sequence_offset: sequence_prefix_width(trimmed),
425
+ }
426
+ } else if is_sequence_entry {
427
+ LineKind::Sequence
428
+ } else {
429
+ LineKind::Other
430
+ };
431
+ let starts_multiline = detect_multiline_indicator(trimmed);
432
+ Self {
433
+ kind,
434
+ starts_multiline,
435
+ is_sequence_entry,
436
+ }
437
+ }
438
+
439
+ const fn context_kind(self) -> ContextKind {
440
+ match self.kind {
441
+ LineKind::Mapping {
442
+ sequence_offset, ..
443
+ } => ContextKind::Mapping { sequence_offset },
444
+ LineKind::Sequence => ContextKind::Sequence,
445
+ LineKind::Other => ContextKind::Other,
446
+ }
447
+ }
448
+
449
+ const fn opens_child_context(self) -> bool {
450
+ matches!(
451
+ self.kind,
452
+ LineKind::Mapping {
453
+ opens_block: true,
454
+ ..
455
+ }
456
+ )
457
+ }
458
+
459
+ const fn is_mapping_key(self) -> bool {
460
+ matches!(self.kind, LineKind::Mapping { .. })
461
+ }
462
+
463
+ const fn is_sequence_entry(self) -> bool {
464
+ self.is_sequence_entry
465
+ }
466
+ }
467
+
468
+ #[derive(Debug, Clone, Copy)]
469
+ struct MultilineState {
470
+ base_indent: usize,
471
+ expected_indent: Option<usize>,
472
+ }
473
+
474
+ impl MultilineState {
475
+ const fn new(base_indent: usize) -> Self {
476
+ Self {
477
+ base_indent,
478
+ expected_indent: None,
479
+ }
480
+ }
481
+
482
+ fn expected_indent(&mut self, indent: usize, spaces: &mut SpacesRuntime) -> usize {
483
+ if let Some(expected) = self.expected_indent {
484
+ expected
485
+ } else {
486
+ let expected = spaces.current_or_set(self.base_indent, indent);
487
+ self.expected_indent = Some(expected);
488
+ expected
489
+ }
490
+ }
491
+ }
492
+
493
+ struct SpacesRuntime {
494
+ setting: SpacesSetting,
495
+ value: Option<usize>,
496
+ }
497
+
498
+ impl SpacesRuntime {
499
+ const fn new(setting: SpacesSetting) -> Self {
500
+ Self {
501
+ setting,
502
+ value: None,
503
+ }
504
+ }
505
+
506
+ const fn expected_step(&self) -> Option<usize> {
507
+ match self.setting {
508
+ SpacesSetting::Fixed(value) => Some(value),
509
+ SpacesSetting::Consistent => self.value,
510
+ }
511
+ }
512
+
513
+ fn current_or_set(&mut self, base: usize, found: usize) -> usize {
514
+ match self.setting {
515
+ SpacesSetting::Fixed(v) => base.saturating_add(v),
516
+ SpacesSetting::Consistent => {
517
+ let delta = found.saturating_sub(base);
518
+ if let Some(val) = self.value {
519
+ base.saturating_add(val)
520
+ } else {
521
+ let value = delta.max(1);
522
+ self.value = Some(value);
523
+ base.saturating_add(value)
524
+ }
525
+ }
526
+ }
527
+ }
528
+
529
+ fn observe_increase(
530
+ &mut self,
531
+ base: usize,
532
+ found: usize,
533
+ line: usize,
534
+ diagnostics: &mut Vec<Violation>,
535
+ ) {
536
+ match self.setting {
537
+ SpacesSetting::Fixed(value) => {
538
+ let delta = found.saturating_sub(base);
539
+ if !delta.is_multiple_of(value) {
540
+ let expected = base.saturating_add(value);
541
+ diagnostics.push(Violation {
542
+ line,
543
+ column: found + 1,
544
+ message: format!(
545
+ "wrong indentation: expected {expected} but found {found}"
546
+ ),
547
+ });
548
+ }
549
+ }
550
+ SpacesSetting::Consistent => {
551
+ let delta = found.saturating_sub(base);
552
+ if let Some(val) = self.value {
553
+ if !delta.is_multiple_of(val) {
554
+ let expected = base.saturating_add(val);
555
+ diagnostics.push(Violation {
556
+ line,
557
+ column: found + 1,
558
+ message: format!(
559
+ "wrong indentation: expected {expected} but found {found}"
560
+ ),
561
+ });
562
+ }
563
+ } else {
564
+ self.value = Some(delta);
565
+ }
566
+ }
567
+ }
568
+ }
569
+
570
+ fn observe_indent(
571
+ &self,
572
+ indent: usize,
573
+ line: usize,
574
+ diagnostics: &mut Vec<Violation>,
575
+ ) {
576
+ match self.setting {
577
+ SpacesSetting::Fixed(value) => {
578
+ if !indent.is_multiple_of(value) {
579
+ diagnostics.push(Violation {
580
+ line,
581
+ column: indent + 1,
582
+ message: format!(
583
+ "wrong indentation: expected {} but found {}",
584
+ indent / value * value,
585
+ indent
586
+ ),
587
+ });
588
+ }
589
+ }
590
+ SpacesSetting::Consistent => {
591
+ if let Some(val) = self.value
592
+ && !indent.is_multiple_of(val)
593
+ {
594
+ let exp = indent / val * val;
595
+ diagnostics.push(Violation {
596
+ line,
597
+ column: indent + 1,
598
+ message: format!(
599
+ "wrong indentation: expected {exp} but found {indent}"
600
+ ),
601
+ });
602
+ }
603
+ }
604
+ }
605
+ }
606
+ }
607
+
608
+ struct IndentSequencesRuntime {
609
+ setting: IndentSequencesSetting,
610
+ }
611
+
612
+ impl IndentSequencesRuntime {
613
+ const fn new(setting: IndentSequencesSetting) -> Self {
614
+ Self { setting }
615
+ }
616
+
617
+ fn check(
618
+ &self,
619
+ parent_indent: usize,
620
+ found_indent: usize,
621
+ is_indented: bool,
622
+ expected_indent: Option<usize>,
623
+ state: &mut Option<bool>,
624
+ ) -> Option<String> {
625
+ match self.setting {
626
+ IndentSequencesSetting::True => {
627
+ if !is_indented {
628
+ let expected = expected_indent.unwrap_or(parent_indent + 2);
629
+ return Some(format!(
630
+ "wrong indentation: expected {expected} but found {found_indent}"
631
+ ));
632
+ }
633
+ if let Some(expected) = expected_indent
634
+ && found_indent != expected
635
+ {
636
+ return Some(format!(
637
+ "wrong indentation: expected {expected} but found {found_indent}"
638
+ ));
639
+ }
640
+ None
641
+ }
642
+ IndentSequencesSetting::False => {
643
+ if is_indented {
644
+ Some(format!(
645
+ "wrong indentation: expected {parent_indent} but found {found_indent}"
646
+ ))
647
+ } else {
648
+ None
649
+ }
650
+ }
651
+ IndentSequencesSetting::Whatever => None,
652
+ IndentSequencesSetting::Consistent => {
653
+ if let Some(expected) = expected_indent
654
+ && is_indented
655
+ && found_indent != expected
656
+ {
657
+ return Some(format!(
658
+ "wrong indentation: expected {expected} but found {found_indent}"
659
+ ));
660
+ }
661
+ match state {
662
+ Some(expected) if *expected == is_indented => None,
663
+ Some(expected) => {
664
+ let exp_indent = if *expected {
665
+ parent_indent + 2
666
+ } else {
667
+ parent_indent
668
+ };
669
+ Some(format!(
670
+ "wrong indentation: expected {exp_indent} but found {found_indent}"
671
+ ))
672
+ }
673
+ None => {
674
+ *state = Some(is_indented);
675
+ None
676
+ }
677
+ }
678
+ }
679
+ }
680
+ }
681
+ }
682
+
683
+ fn split_indent(line: &str) -> (usize, &str) {
684
+ let mut count = 0;
685
+ for ch in line.chars() {
686
+ match ch {
687
+ ' ' | '\t' => count += 1,
688
+ _ => break,
689
+ }
690
+ }
691
+ let content = &line[count..];
692
+ (count, content)
693
+ }
694
+
695
+ fn strip_trailing_comment(line: &str) -> (&str, Option<&str>) {
696
+ let mut in_single = false;
697
+ let mut in_double = false;
698
+ let mut escaped = false;
699
+ for (idx, ch) in line.char_indices() {
700
+ match ch {
701
+ '\\' => escaped = !escaped,
702
+ '\'' if !escaped && !in_double => in_single = !in_single,
703
+ '"' if !escaped && !in_single => in_double = !in_double,
704
+ '#' if !in_single && !in_double => {
705
+ let core = line[..idx].trim_end();
706
+ return (core, Some(&line[idx..]));
707
+ }
708
+ _ => escaped = false,
709
+ }
710
+ }
711
+ (line.trim_end(), None)
712
+ }
713
+
714
+ fn is_sequence_entry(content: &str) -> bool {
715
+ if !content.starts_with('-') {
716
+ return false;
717
+ }
718
+ matches!(content.chars().nth(1), None | Some(' ' | '\t' | '\r' | '#'))
719
+ }
720
+
721
+ fn is_compact_sequence_start(content: &str) -> bool {
722
+ let trimmed = content.trim();
723
+ if !is_sequence_entry(trimmed) {
724
+ return false;
725
+ }
726
+ let stripped = trimmed
727
+ .strip_prefix('-')
728
+ .expect("sequence entry starts with '-'");
729
+ is_sequence_entry(stripped.trim_start())
730
+ }
731
+
732
+ fn classify_mapping(content: &str) -> (bool, bool) {
733
+ let mut in_single = false;
734
+ let mut in_double = false;
735
+ let mut brace_depth = 0;
736
+ let mut bracket_depth = 0;
737
+ let mut escaped = false;
738
+ for (idx, ch) in content.char_indices() {
739
+ match ch {
740
+ '\\' => escaped = !escaped,
741
+ '\'' if !escaped && !in_double => in_single = !in_single,
742
+ '"' if !escaped && !in_single => in_double = !in_double,
743
+ '{' if !in_single && !in_double => brace_depth += 1,
744
+ '}' if !in_single && !in_double && brace_depth > 0 => brace_depth -= 1,
745
+ '[' if !in_single && !in_double => bracket_depth += 1,
746
+ ']' if !in_single && !in_double && bracket_depth > 0 => bracket_depth -= 1,
747
+ ':' if !in_single
748
+ && !in_double
749
+ && brace_depth == 0
750
+ && bracket_depth == 0 =>
751
+ {
752
+ let before = content[..idx].trim_end();
753
+ if before.is_empty() {
754
+ return (false, false);
755
+ }
756
+ let after = content[idx + 1..].trim();
757
+ let opens_block = after.is_empty();
758
+ return (true, opens_block);
759
+ }
760
+ _ => escaped = false,
761
+ }
762
+ }
763
+ (false, false)
764
+ }
765
+
766
+ fn detect_multiline_indicator(content: &str) -> bool {
767
+ let base = content.trim_end_matches(|ch: char| ch.is_whitespace());
768
+ base.ends_with("|-")
769
+ || base.ends_with("|+")
770
+ || base.ends_with('|')
771
+ || base.ends_with(">-")
772
+ || base.ends_with(">+")
773
+ || base.ends_with('>')
774
+ }
775
+
776
+ fn sequence_prefix_width(content: &str) -> usize {
777
+ if !content.starts_with('-') {
778
+ return 0;
779
+ }
780
+ 1 + content
781
+ .chars()
782
+ .skip(1)
783
+ .take_while(|ch| matches!(ch, ' ' | '\t'))
784
+ .count()
785
+ }
786
+
787
+ fn compact_flow_mapping_continuation_indent(
788
+ content: &str,
789
+ indent: usize,
790
+ ) -> Option<usize> {
791
+ let trimmed = content.trim();
792
+ if !is_sequence_entry(trimmed) {
793
+ return None;
794
+ }
795
+ let base_prefix = 1 + trimmed
796
+ .chars()
797
+ .skip(1)
798
+ .take_while(|ch| matches!(ch, ' ' | '\t'))
799
+ .count();
800
+ trimmed[base_prefix..]
801
+ .starts_with('{')
802
+ .then_some(indent.saturating_add(base_prefix + 1))
803
+ }