@owenlamont/ryl 0.4.1 → 0.4.3

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/README.md +13 -0
  2. package/bin/ryl.js +96 -1
  3. package/npm-platforms.json +124 -0
  4. package/package.json +46 -12
  5. package/.github/CODEOWNERS +0 -1
  6. package/.github/dependabot.yml +0 -13
  7. package/.github/workflows/ci.yml +0 -107
  8. package/.github/workflows/release.yml +0 -613
  9. package/.github/workflows/update_dependencies.yml +0 -61
  10. package/.github/workflows/update_linters.yml +0 -56
  11. package/.pre-commit-config.yaml +0 -87
  12. package/.yamllint +0 -4
  13. package/AGENTS.md +0 -200
  14. package/Cargo.lock +0 -908
  15. package/Cargo.toml +0 -32
  16. package/clippy.toml +0 -1
  17. package/docs/config-presets.md +0 -100
  18. package/img/benchmark-5x5-5runs.svg +0 -2176
  19. package/pyproject.toml +0 -42
  20. package/ruff.toml +0 -107
  21. package/rumdl.toml +0 -20
  22. package/rust-toolchain.toml +0 -3
  23. package/rustfmt.toml +0 -3
  24. package/scripts/benchmark_perf_vs_yamllint.py +0 -400
  25. package/scripts/coverage-missing.ps1 +0 -80
  26. package/scripts/coverage-missing.sh +0 -60
  27. package/src/bin/discover_config_bin.rs +0 -24
  28. package/src/cli_support.rs +0 -33
  29. package/src/conf/mod.rs +0 -85
  30. package/src/config.rs +0 -2099
  31. package/src/decoder.rs +0 -326
  32. package/src/discover.rs +0 -31
  33. package/src/lib.rs +0 -19
  34. package/src/lint.rs +0 -558
  35. package/src/main.rs +0 -535
  36. package/src/migrate.rs +0 -233
  37. package/src/rules/anchors.rs +0 -517
  38. package/src/rules/braces.rs +0 -77
  39. package/src/rules/brackets.rs +0 -77
  40. package/src/rules/colons.rs +0 -475
  41. package/src/rules/commas.rs +0 -372
  42. package/src/rules/comments.rs +0 -299
  43. package/src/rules/comments_indentation.rs +0 -243
  44. package/src/rules/document_end.rs +0 -175
  45. package/src/rules/document_start.rs +0 -84
  46. package/src/rules/empty_lines.rs +0 -152
  47. package/src/rules/empty_values.rs +0 -255
  48. package/src/rules/float_values.rs +0 -259
  49. package/src/rules/flow_collection.rs +0 -562
  50. package/src/rules/hyphens.rs +0 -104
  51. package/src/rules/indentation.rs +0 -803
  52. package/src/rules/key_duplicates.rs +0 -218
  53. package/src/rules/key_ordering.rs +0 -303
  54. package/src/rules/line_length.rs +0 -326
  55. package/src/rules/mod.rs +0 -25
  56. package/src/rules/new_line_at_end_of_file.rs +0 -23
  57. package/src/rules/new_lines.rs +0 -95
  58. package/src/rules/octal_values.rs +0 -121
  59. package/src/rules/quoted_strings.rs +0 -577
  60. package/src/rules/span_utils.rs +0 -37
  61. package/src/rules/trailing_spaces.rs +0 -65
  62. package/src/rules/truthy.rs +0 -420
  63. package/tests/brackets_carriage_return.rs +0 -114
  64. package/tests/build_global_cfg_error.rs +0 -23
  65. package/tests/cli_anchors_rule.rs +0 -143
  66. package/tests/cli_braces_rule.rs +0 -104
  67. package/tests/cli_brackets_rule.rs +0 -104
  68. package/tests/cli_colons_rule.rs +0 -65
  69. package/tests/cli_commas_rule.rs +0 -104
  70. package/tests/cli_comments_indentation_rule.rs +0 -61
  71. package/tests/cli_comments_rule.rs +0 -67
  72. package/tests/cli_config_data_error.rs +0 -30
  73. package/tests/cli_config_flags.rs +0 -66
  74. package/tests/cli_config_migrate.rs +0 -229
  75. package/tests/cli_document_end_rule.rs +0 -92
  76. package/tests/cli_document_start_rule.rs +0 -92
  77. package/tests/cli_empty_lines_rule.rs +0 -87
  78. package/tests/cli_empty_values_rule.rs +0 -68
  79. package/tests/cli_env_config.rs +0 -34
  80. package/tests/cli_exit_and_errors.rs +0 -41
  81. package/tests/cli_file_encoding.rs +0 -203
  82. package/tests/cli_float_values_rule.rs +0 -64
  83. package/tests/cli_format_options.rs +0 -316
  84. package/tests/cli_global_cfg_relaxed.rs +0 -20
  85. package/tests/cli_hyphens_rule.rs +0 -104
  86. package/tests/cli_indentation_rule.rs +0 -65
  87. package/tests/cli_invalid_project_config.rs +0 -39
  88. package/tests/cli_key_duplicates_rule.rs +0 -104
  89. package/tests/cli_key_ordering_rule.rs +0 -59
  90. package/tests/cli_line_length_rule.rs +0 -85
  91. package/tests/cli_list_files.rs +0 -29
  92. package/tests/cli_new_line_rule.rs +0 -141
  93. package/tests/cli_new_lines_rule.rs +0 -119
  94. package/tests/cli_octal_values_rule.rs +0 -60
  95. package/tests/cli_quoted_strings_rule.rs +0 -47
  96. package/tests/cli_toml_config.rs +0 -119
  97. package/tests/cli_trailing_spaces_rule.rs +0 -77
  98. package/tests/cli_truthy_rule.rs +0 -83
  99. package/tests/cli_yaml_files_negation.rs +0 -45
  100. package/tests/colons_rule.rs +0 -303
  101. package/tests/common/compat.rs +0 -114
  102. package/tests/common/fake_env.rs +0 -93
  103. package/tests/common/mod.rs +0 -1
  104. package/tests/conf_builtin.rs +0 -9
  105. package/tests/config_anchors.rs +0 -84
  106. package/tests/config_braces.rs +0 -121
  107. package/tests/config_brackets.rs +0 -127
  108. package/tests/config_commas.rs +0 -79
  109. package/tests/config_comments.rs +0 -65
  110. package/tests/config_comments_indentation.rs +0 -20
  111. package/tests/config_deep_merge_nonstring_key.rs +0 -24
  112. package/tests/config_document_end.rs +0 -54
  113. package/tests/config_document_start.rs +0 -55
  114. package/tests/config_empty_lines.rs +0 -48
  115. package/tests/config_empty_values.rs +0 -35
  116. package/tests/config_env_errors.rs +0 -23
  117. package/tests/config_env_invalid_inline.rs +0 -15
  118. package/tests/config_env_missing.rs +0 -63
  119. package/tests/config_env_shim.rs +0 -301
  120. package/tests/config_explicit_file_parse_error.rs +0 -55
  121. package/tests/config_extended_features.rs +0 -225
  122. package/tests/config_extends_inline.rs +0 -185
  123. package/tests/config_extends_sequence.rs +0 -18
  124. package/tests/config_find_project_home_boundary.rs +0 -54
  125. package/tests/config_find_project_two_files_in_cwd.rs +0 -47
  126. package/tests/config_float_values.rs +0 -34
  127. package/tests/config_from_yaml_paths.rs +0 -32
  128. package/tests/config_hyphens.rs +0 -51
  129. package/tests/config_ignore_errors.rs +0 -243
  130. package/tests/config_ignore_overrides.rs +0 -83
  131. package/tests/config_indentation.rs +0 -65
  132. package/tests/config_invalid_globs.rs +0 -16
  133. package/tests/config_invalid_types.rs +0 -19
  134. package/tests/config_key_duplicates.rs +0 -34
  135. package/tests/config_key_ordering.rs +0 -70
  136. package/tests/config_line_length.rs +0 -65
  137. package/tests/config_locale.rs +0 -111
  138. package/tests/config_merge.rs +0 -26
  139. package/tests/config_new_lines.rs +0 -89
  140. package/tests/config_octal_values.rs +0 -33
  141. package/tests/config_quoted_strings.rs +0 -195
  142. package/tests/config_rule_level.rs +0 -147
  143. package/tests/config_rules_non_string_keys.rs +0 -23
  144. package/tests/config_scalar_overrides.rs +0 -27
  145. package/tests/config_to_toml.rs +0 -110
  146. package/tests/config_toml_coverage.rs +0 -80
  147. package/tests/config_toml_discovery.rs +0 -304
  148. package/tests/config_trailing_spaces.rs +0 -152
  149. package/tests/config_truthy.rs +0 -77
  150. package/tests/config_yaml_files.rs +0 -62
  151. package/tests/config_yaml_files_all_non_string.rs +0 -15
  152. package/tests/config_yaml_files_empty.rs +0 -30
  153. package/tests/coverage_commas.rs +0 -46
  154. package/tests/decoder_decode.rs +0 -338
  155. package/tests/discover_config_bin_all.rs +0 -66
  156. package/tests/discover_config_bin_env_invalid_yaml.rs +0 -26
  157. package/tests/discover_config_bin_project_config_parse_error.rs +0 -24
  158. package/tests/discover_config_bin_user_global_error.rs +0 -26
  159. package/tests/discover_module.rs +0 -30
  160. package/tests/discover_per_file_dir.rs +0 -10
  161. package/tests/discover_per_file_project_config_error.rs +0 -21
  162. package/tests/float_values.rs +0 -43
  163. package/tests/lint_multi_errors.rs +0 -32
  164. package/tests/main_yaml_ok_filtering.rs +0 -30
  165. package/tests/migrate_module.rs +0 -259
  166. package/tests/resolve_ctx_empty_parent.rs +0 -16
  167. package/tests/rule_anchors.rs +0 -442
  168. package/tests/rule_braces.rs +0 -258
  169. package/tests/rule_brackets.rs +0 -217
  170. package/tests/rule_commas.rs +0 -205
  171. package/tests/rule_comments.rs +0 -197
  172. package/tests/rule_comments_indentation.rs +0 -127
  173. package/tests/rule_document_end.rs +0 -118
  174. package/tests/rule_document_start.rs +0 -60
  175. package/tests/rule_empty_lines.rs +0 -96
  176. package/tests/rule_empty_values.rs +0 -102
  177. package/tests/rule_float_values.rs +0 -109
  178. package/tests/rule_hyphens.rs +0 -65
  179. package/tests/rule_indentation.rs +0 -455
  180. package/tests/rule_key_duplicates.rs +0 -76
  181. package/tests/rule_key_ordering.rs +0 -207
  182. package/tests/rule_line_length.rs +0 -200
  183. package/tests/rule_new_lines.rs +0 -51
  184. package/tests/rule_octal_values.rs +0 -53
  185. package/tests/rule_quoted_strings.rs +0 -290
  186. package/tests/rule_trailing_spaces.rs +0 -41
  187. package/tests/rule_truthy.rs +0 -236
  188. package/tests/user_global_invalid_yaml.rs +0 -32
  189. package/tests/yamllint_compat_anchors.rs +0 -280
  190. package/tests/yamllint_compat_braces.rs +0 -411
  191. package/tests/yamllint_compat_brackets.rs +0 -364
  192. package/tests/yamllint_compat_colons.rs +0 -298
  193. package/tests/yamllint_compat_colors.rs +0 -80
  194. package/tests/yamllint_compat_commas.rs +0 -375
  195. package/tests/yamllint_compat_comments.rs +0 -167
  196. package/tests/yamllint_compat_comments_indentation.rs +0 -281
  197. package/tests/yamllint_compat_config.rs +0 -170
  198. package/tests/yamllint_compat_document_end.rs +0 -243
  199. package/tests/yamllint_compat_document_start.rs +0 -136
  200. package/tests/yamllint_compat_empty_lines.rs +0 -117
  201. package/tests/yamllint_compat_empty_values.rs +0 -179
  202. package/tests/yamllint_compat_float_values.rs +0 -216
  203. package/tests/yamllint_compat_hyphens.rs +0 -223
  204. package/tests/yamllint_compat_indentation.rs +0 -398
  205. package/tests/yamllint_compat_key_duplicates.rs +0 -139
  206. package/tests/yamllint_compat_key_ordering.rs +0 -170
  207. package/tests/yamllint_compat_line_length.rs +0 -375
  208. package/tests/yamllint_compat_list.rs +0 -127
  209. package/tests/yamllint_compat_new_line.rs +0 -133
  210. package/tests/yamllint_compat_newline_types.rs +0 -185
  211. package/tests/yamllint_compat_octal_values.rs +0 -172
  212. package/tests/yamllint_compat_quoted_strings.rs +0 -154
  213. package/tests/yamllint_compat_syntax.rs +0 -200
  214. package/tests/yamllint_compat_trailing_spaces.rs +0 -162
  215. package/tests/yamllint_compat_truthy.rs +0 -130
  216. package/tests/yamllint_compat_yaml_files.rs +0 -81
  217. package/typos.toml +0 -2
@@ -1,326 +0,0 @@
1
- use std::convert::TryFrom;
2
-
3
- use saphyr::YamlOwned;
4
- use saphyr_parser::{Event, Parser, Span, SpannedEventReceiver};
5
-
6
- use crate::config::YamlLintConfig;
7
-
8
- pub const ID: &str = "line-length";
9
-
10
- #[derive(Debug, Clone)]
11
- pub struct Config {
12
- max: i64,
13
- allow_non_breakable_words: bool,
14
- allow_non_breakable_inline_mappings: bool,
15
- }
16
-
17
- impl Config {
18
- #[must_use]
19
- pub fn resolve(cfg: &YamlLintConfig) -> Self {
20
- let max = cfg
21
- .rule_option(ID, "max")
22
- .and_then(YamlOwned::as_integer)
23
- .unwrap_or(80);
24
-
25
- let allow_inline = cfg
26
- .rule_option(ID, "allow-non-breakable-inline-mappings")
27
- .and_then(YamlOwned::as_bool)
28
- .unwrap_or(false);
29
-
30
- let allow_words = cfg
31
- .rule_option(ID, "allow-non-breakable-words")
32
- .and_then(YamlOwned::as_bool)
33
- .unwrap_or(true)
34
- || allow_inline;
35
-
36
- Self {
37
- max,
38
- allow_non_breakable_words: allow_words,
39
- allow_non_breakable_inline_mappings: allow_inline,
40
- }
41
- }
42
-
43
- const fn max(&self) -> i64 {
44
- self.max
45
- }
46
-
47
- const fn allow_non_breakable_words(&self) -> bool {
48
- self.allow_non_breakable_words
49
- }
50
-
51
- const fn allow_non_breakable_inline_mappings(&self) -> bool {
52
- self.allow_non_breakable_inline_mappings
53
- }
54
-
55
- fn diagnostic_column(&self) -> usize {
56
- if self.max < 0 {
57
- 0
58
- } else {
59
- let value = self.max.saturating_add(1);
60
- usize::try_from(value).unwrap_or(usize::MAX)
61
- }
62
- }
63
- }
64
-
65
- #[derive(Debug, Clone, PartialEq, Eq)]
66
- pub struct Violation {
67
- pub line: usize,
68
- pub column: usize,
69
- pub message: String,
70
- }
71
-
72
- #[must_use]
73
- pub fn check(buffer: &str, cfg: &Config) -> Vec<Violation> {
74
- let mut violations = Vec::new();
75
- let bytes = buffer.as_bytes();
76
- let mut line_no = 1usize;
77
- let mut line_start = 0usize;
78
- let mut idx = 0usize;
79
- let mut directive_state = DirectiveState::new();
80
-
81
- while idx < bytes.len() {
82
- if bytes[idx] == b'\n' {
83
- let line_end = if idx > line_start && bytes[idx - 1] == b'\r' {
84
- idx - 1
85
- } else {
86
- idx
87
- };
88
- process_line(
89
- buffer,
90
- line_no,
91
- line_start,
92
- line_end,
93
- cfg,
94
- &mut violations,
95
- &mut directive_state,
96
- );
97
- idx += 1;
98
- line_start = idx;
99
- line_no += 1;
100
- } else {
101
- idx += 1;
102
- }
103
- }
104
-
105
- process_line(
106
- buffer,
107
- line_no,
108
- line_start,
109
- bytes.len(),
110
- cfg,
111
- &mut violations,
112
- &mut directive_state,
113
- );
114
- violations
115
- }
116
-
117
- fn process_line(
118
- buffer: &str,
119
- line_no: usize,
120
- start: usize,
121
- end: usize,
122
- cfg: &Config,
123
- out: &mut Vec<Violation>,
124
- directive_state: &mut DirectiveState,
125
- ) {
126
- let disable_line_applies = std::mem::take(&mut directive_state.disable_next_line);
127
- if start == end {
128
- return;
129
- }
130
-
131
- let line = &buffer[start..end];
132
- match line_length_directive(line) {
133
- Directive::Disable => {
134
- directive_state.disabled = true;
135
- return;
136
- }
137
- Directive::DisableLine => {
138
- directive_state.disable_next_line = true;
139
- return;
140
- }
141
- Directive::Enable => {
142
- directive_state.disabled = false;
143
- return;
144
- }
145
- Directive::None => {}
146
- }
147
- if directive_state.disabled || disable_line_applies {
148
- return;
149
- }
150
-
151
- let length = line.chars().count();
152
- let length_i64 = i64::try_from(length).unwrap_or(i64::MAX);
153
-
154
- if length_i64 <= cfg.max() {
155
- return;
156
- }
157
-
158
- if cfg.allow_non_breakable_words() && allows_overflow(line, cfg) {
159
- return;
160
- }
161
-
162
- out.push(Violation {
163
- line: line_no,
164
- column: cfg.diagnostic_column(),
165
- message: format!("line too long ({} > {} characters)", length, cfg.max()),
166
- });
167
- }
168
-
169
- fn allows_overflow(line: &str, cfg: &Config) -> bool {
170
- let mut idx = 0usize;
171
- let bytes = line.as_bytes();
172
-
173
- while idx < bytes.len() && bytes[idx] == b' ' {
174
- idx += 1;
175
- }
176
-
177
- if idx >= bytes.len() {
178
- return false;
179
- }
180
-
181
- if bytes[idx] == b'#' {
182
- while idx < bytes.len() && bytes[idx] == b'#' {
183
- idx += 1;
184
- }
185
- idx = (idx + 1).min(bytes.len());
186
- } else if bytes[idx] == b'-' {
187
- idx = (idx + 1).min(bytes.len());
188
- idx = (idx + 1).min(bytes.len());
189
- }
190
-
191
- if idx >= bytes.len() {
192
- return false;
193
- }
194
-
195
- let tail_bytes = &line.as_bytes()[idx..];
196
- if !tail_bytes.contains(&b' ') {
197
- return true;
198
- }
199
-
200
- cfg.allow_non_breakable_inline_mappings() && check_inline_mapping(line)
201
- }
202
-
203
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
204
- enum Directive {
205
- None,
206
- Disable,
207
- DisableLine,
208
- Enable,
209
- }
210
-
211
- #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
212
- struct DirectiveState {
213
- disabled: bool,
214
- disable_next_line: bool,
215
- }
216
-
217
- impl DirectiveState {
218
- const fn new() -> Self {
219
- Self {
220
- disabled: false,
221
- disable_next_line: false,
222
- }
223
- }
224
- }
225
-
226
- fn line_length_directive(line: &str) -> Directive {
227
- let trimmed = line.trim_start();
228
- if !trimmed.starts_with('#') {
229
- return Directive::None;
230
- }
231
- let payload = trimmed.trim_start_matches('#').trim_start();
232
- if payload == "yamllint disable rule:line-length" {
233
- return Directive::Disable;
234
- }
235
- if payload == "yamllint disable-line rule:line-length" {
236
- return Directive::DisableLine;
237
- }
238
- if payload == "yamllint enable rule:line-length" {
239
- return Directive::Enable;
240
- }
241
- Directive::None
242
- }
243
-
244
- fn check_inline_mapping(line: &str) -> bool {
245
- let mut parser = Parser::new_from_str(line);
246
- let mut detector = InlineMappingDetector::new(line);
247
- let _ = parser.load(&mut detector, true);
248
- detector.allowed()
249
- }
250
-
251
- struct InlineMappingDetector<'a> {
252
- line: &'a str,
253
- mappings: Vec<MappingState>,
254
- allowed: bool,
255
- }
256
-
257
- impl<'a> InlineMappingDetector<'a> {
258
- const fn new(line: &'a str) -> Self {
259
- Self {
260
- line,
261
- mappings: Vec::new(),
262
- allowed: false,
263
- }
264
- }
265
-
266
- const fn allowed(&self) -> bool {
267
- self.allowed
268
- }
269
-
270
- fn tail_from_column(&self, column: usize) -> &str {
271
- self.line
272
- .char_indices()
273
- .nth(column)
274
- .map_or("", |(idx, _)| &self.line[idx..])
275
- }
276
-
277
- fn handle_scalar_event(&mut self, span: Span) {
278
- let state = self
279
- .mappings
280
- .last_mut()
281
- .expect("scalar event handler requires active mapping");
282
-
283
- if state.expect_key {
284
- state.expect_key = false;
285
- return;
286
- }
287
-
288
- state.expect_key = true;
289
- let single_line = span.start.line() == 1 && span.end.line() == 1;
290
- if single_line && !self.tail_from_column(span.start.col()).contains(' ') {
291
- self.allowed = true;
292
- }
293
- }
294
- }
295
-
296
- #[derive(Debug, Clone)]
297
- struct MappingState {
298
- expect_key: bool,
299
- }
300
-
301
- impl SpannedEventReceiver<'_> for InlineMappingDetector<'_> {
302
- fn on_event(&mut self, event: Event<'_>, span: Span) {
303
- if self.allowed {
304
- return;
305
- }
306
-
307
- match event {
308
- Event::MappingStart(_, _) => {
309
- self.mappings.push(MappingState { expect_key: true });
310
- }
311
- Event::MappingEnd => {
312
- self.mappings.pop();
313
- }
314
- Event::Scalar(_, _, _, _) if self.mappings.is_empty() => {}
315
- Event::Scalar(_, _, _, _) => self.handle_scalar_event(span),
316
- Event::SequenceStart(_, _)
317
- | Event::SequenceEnd
318
- | Event::StreamStart
319
- | Event::StreamEnd
320
- | Event::DocumentStart(_)
321
- | Event::DocumentEnd
322
- | Event::Alias(_)
323
- | Event::Nothing => {}
324
- }
325
- }
326
- }
package/src/rules/mod.rs DELETED
@@ -1,25 +0,0 @@
1
- pub mod anchors;
2
- pub mod braces;
3
- pub mod brackets;
4
- pub mod colons;
5
- pub mod commas;
6
- pub mod comments;
7
- pub mod comments_indentation;
8
- pub mod document_end;
9
- pub mod document_start;
10
- pub mod empty_lines;
11
- pub mod empty_values;
12
- pub mod float_values;
13
- pub(crate) mod flow_collection;
14
- pub mod hyphens;
15
- pub mod indentation;
16
- pub mod key_duplicates;
17
- pub mod key_ordering;
18
- pub mod line_length;
19
- pub mod new_line_at_end_of_file;
20
- pub mod new_lines;
21
- pub mod octal_values;
22
- pub mod quoted_strings;
23
- pub(crate) mod span_utils;
24
- pub mod trailing_spaces;
25
- pub mod truthy;
@@ -1,23 +0,0 @@
1
- pub const ID: &str = "new-line-at-end-of-file";
2
- pub const MESSAGE: &str = "no new line character at the end of file";
3
-
4
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
5
- pub struct Violation {
6
- pub line: usize,
7
- pub column: usize,
8
- }
9
-
10
- #[must_use]
11
- pub fn check(buffer: &str) -> Option<Violation> {
12
- if buffer.is_empty() || buffer.ends_with('\n') {
13
- return None;
14
- }
15
-
16
- let line = buffer.lines().count();
17
- let tail = buffer
18
- .rsplit_once('\n')
19
- .map_or(buffer, |(_, trailing)| trailing);
20
- let column = tail.chars().count() + 1;
21
-
22
- Some(Violation { line, column })
23
- }
@@ -1,95 +0,0 @@
1
- use std::borrow::Cow;
2
-
3
- use crate::config::YamlLintConfig;
4
-
5
- pub const ID: &str = "new-lines";
6
-
7
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
8
- pub enum LineKind {
9
- Unix,
10
- Dos,
11
- Platform,
12
- }
13
-
14
- impl LineKind {
15
- fn expected(self, platform_newline: &str) -> Cow<'_, str> {
16
- match self {
17
- Self::Unix => Cow::Borrowed("\n"),
18
- Self::Dos => Cow::Borrowed("\r\n"),
19
- Self::Platform => Cow::Owned(platform_newline.to_string()),
20
- }
21
- }
22
- }
23
-
24
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
25
- pub struct Config {
26
- pub kind: LineKind,
27
- }
28
-
29
- impl Config {
30
- #[must_use]
31
- pub fn resolve(cfg: &YamlLintConfig) -> Self {
32
- let kind = match cfg.rule_option_str(ID, "type") {
33
- Some("dos") => LineKind::Dos,
34
- Some("platform") => LineKind::Platform,
35
- _ => LineKind::Unix,
36
- };
37
- Self { kind }
38
- }
39
- }
40
-
41
- #[derive(Debug, Clone, PartialEq, Eq)]
42
- pub struct Violation {
43
- pub line: usize,
44
- pub column: usize,
45
- pub message: String,
46
- }
47
-
48
- #[cfg(windows)]
49
- #[must_use]
50
- pub const fn platform_newline() -> &'static str {
51
- "\r\n"
52
- }
53
-
54
- #[cfg(not(windows))]
55
- #[must_use]
56
- pub const fn platform_newline() -> &'static str {
57
- "\n"
58
- }
59
-
60
- #[must_use]
61
- pub fn check(buffer: &str, cfg: Config, platform_newline: &str) -> Option<Violation> {
62
- let expected = cfg.kind.expected(platform_newline);
63
- let (index, actual) = first_line_ending(buffer)?;
64
- if actual == expected.as_ref() {
65
- return None;
66
- }
67
-
68
- let column = buffer[..index].chars().count() + 1;
69
- Some(Violation {
70
- line: 1,
71
- column,
72
- message: format!(
73
- "wrong new line character: expected {}",
74
- display_sequence(expected.as_ref())
75
- ),
76
- })
77
- }
78
-
79
- fn first_line_ending(buffer: &str) -> Option<(usize, &'static str)> {
80
- let bytes = buffer.as_bytes();
81
- let mut idx = 0;
82
- while idx < bytes.len() {
83
- match bytes[idx] {
84
- b'\n' => return Some((idx, "\n")),
85
- b'\r' if bytes.get(idx + 1) == Some(&b'\n') => return Some((idx, "\r\n")),
86
- _ => {}
87
- }
88
- idx += 1;
89
- }
90
- None
91
- }
92
-
93
- fn display_sequence(input: &str) -> &'static str {
94
- if input == "\r\n" { "\\r\\n" } else { "\\n" }
95
- }
@@ -1,121 +0,0 @@
1
- use saphyr::YamlOwned;
2
- use saphyr_parser::{Event, Parser, ScalarStyle, Span, SpannedEventReceiver};
3
-
4
- use crate::config::YamlLintConfig;
5
-
6
- pub const ID: &str = "octal-values";
7
-
8
- #[derive(Debug, Clone)]
9
- pub struct Config {
10
- forbid_implicit: bool,
11
- forbid_explicit: bool,
12
- }
13
-
14
- impl Config {
15
- #[must_use]
16
- pub fn resolve(cfg: &YamlLintConfig) -> Self {
17
- let forbid_implicit = cfg
18
- .rule_option(ID, "forbid-implicit-octal")
19
- .and_then(YamlOwned::as_bool)
20
- .unwrap_or(true);
21
-
22
- let forbid_explicit = cfg
23
- .rule_option(ID, "forbid-explicit-octal")
24
- .and_then(YamlOwned::as_bool)
25
- .unwrap_or(true);
26
-
27
- Self {
28
- forbid_implicit,
29
- forbid_explicit,
30
- }
31
- }
32
-
33
- const fn forbid_implicit(&self) -> bool {
34
- self.forbid_implicit
35
- }
36
-
37
- const fn forbid_explicit(&self) -> bool {
38
- self.forbid_explicit
39
- }
40
- }
41
-
42
- #[derive(Debug, Clone, PartialEq, Eq)]
43
- pub struct Violation {
44
- pub line: usize,
45
- pub column: usize,
46
- pub message: String,
47
- }
48
-
49
- #[must_use]
50
- pub fn check(buffer: &str, cfg: &Config) -> Vec<Violation> {
51
- let mut parser = Parser::new_from_str(buffer);
52
- let mut receiver = OctalValuesReceiver::new(cfg);
53
- let _ = parser.load(&mut receiver, true);
54
- receiver.diagnostics
55
- }
56
-
57
- struct OctalValuesReceiver<'cfg> {
58
- config: &'cfg Config,
59
- diagnostics: Vec<Violation>,
60
- }
61
-
62
- impl<'cfg> OctalValuesReceiver<'cfg> {
63
- const fn new(config: &'cfg Config) -> Self {
64
- Self {
65
- config,
66
- diagnostics: Vec::new(),
67
- }
68
- }
69
-
70
- fn handle_scalar(&mut self, span: Span, value: &str) {
71
- let line = span.end.line();
72
- let column = span.end.col() + 1;
73
-
74
- if self.config.forbid_implicit() && is_implicit_octal(value) {
75
- self.diagnostics.push(Violation {
76
- line,
77
- column,
78
- message: format!("forbidden implicit octal value \"{value}\""),
79
- });
80
- return;
81
- }
82
-
83
- if self.config.forbid_explicit() && is_explicit_octal(value) {
84
- self.diagnostics.push(Violation {
85
- line,
86
- column,
87
- message: format!("forbidden explicit octal value \"{value}\""),
88
- });
89
- }
90
- }
91
- }
92
-
93
- impl SpannedEventReceiver<'_> for OctalValuesReceiver<'_> {
94
- fn on_event(&mut self, event: Event<'_>, span: Span) {
95
- if let Event::Scalar(value, style, _, tag) = event {
96
- if tag.is_some() || !matches!(style, ScalarStyle::Plain) {
97
- return;
98
- }
99
- self.handle_scalar(span, value.as_ref());
100
- }
101
- }
102
- }
103
-
104
- fn is_implicit_octal(value: &str) -> bool {
105
- let bytes = value.as_bytes();
106
- if bytes.len() <= 1 || bytes[0] != b'0' {
107
- return false;
108
- }
109
- if !bytes.iter().all(u8::is_ascii_digit) {
110
- return false;
111
- }
112
- bytes[1..].iter().all(|b| (b'0'..=b'7').contains(b))
113
- }
114
-
115
- fn is_explicit_octal(value: &str) -> bool {
116
- let bytes = value.as_bytes();
117
- if bytes.len() <= 2 || bytes[0] != b'0' || bytes[1] != b'o' {
118
- return false;
119
- }
120
- bytes[2..].iter().all(|b| (b'0'..=b'7').contains(b))
121
- }