@owenlamont/ryl 0.4.1 → 0.4.2
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.
- package/README.md +13 -0
- package/bin/ryl.js +195 -1
- package/package.json +35 -13
- package/.github/CODEOWNERS +0 -1
- package/.github/dependabot.yml +0 -13
- package/.github/workflows/ci.yml +0 -107
- package/.github/workflows/release.yml +0 -613
- package/.github/workflows/update_dependencies.yml +0 -61
- package/.github/workflows/update_linters.yml +0 -56
- package/.pre-commit-config.yaml +0 -87
- package/.yamllint +0 -4
- package/AGENTS.md +0 -200
- package/Cargo.lock +0 -908
- package/Cargo.toml +0 -32
- package/clippy.toml +0 -1
- package/docs/config-presets.md +0 -100
- package/img/benchmark-5x5-5runs.svg +0 -2176
- package/pyproject.toml +0 -42
- package/ruff.toml +0 -107
- package/rumdl.toml +0 -20
- package/rust-toolchain.toml +0 -3
- package/rustfmt.toml +0 -3
- package/scripts/benchmark_perf_vs_yamllint.py +0 -400
- package/scripts/coverage-missing.ps1 +0 -80
- package/scripts/coverage-missing.sh +0 -60
- package/src/bin/discover_config_bin.rs +0 -24
- package/src/cli_support.rs +0 -33
- package/src/conf/mod.rs +0 -85
- package/src/config.rs +0 -2099
- package/src/decoder.rs +0 -326
- package/src/discover.rs +0 -31
- package/src/lib.rs +0 -19
- package/src/lint.rs +0 -558
- package/src/main.rs +0 -535
- package/src/migrate.rs +0 -233
- package/src/rules/anchors.rs +0 -517
- package/src/rules/braces.rs +0 -77
- package/src/rules/brackets.rs +0 -77
- package/src/rules/colons.rs +0 -475
- package/src/rules/commas.rs +0 -372
- package/src/rules/comments.rs +0 -299
- package/src/rules/comments_indentation.rs +0 -243
- package/src/rules/document_end.rs +0 -175
- package/src/rules/document_start.rs +0 -84
- package/src/rules/empty_lines.rs +0 -152
- package/src/rules/empty_values.rs +0 -255
- package/src/rules/float_values.rs +0 -259
- package/src/rules/flow_collection.rs +0 -562
- package/src/rules/hyphens.rs +0 -104
- package/src/rules/indentation.rs +0 -803
- package/src/rules/key_duplicates.rs +0 -218
- package/src/rules/key_ordering.rs +0 -303
- package/src/rules/line_length.rs +0 -326
- package/src/rules/mod.rs +0 -25
- package/src/rules/new_line_at_end_of_file.rs +0 -23
- package/src/rules/new_lines.rs +0 -95
- package/src/rules/octal_values.rs +0 -121
- package/src/rules/quoted_strings.rs +0 -577
- package/src/rules/span_utils.rs +0 -37
- package/src/rules/trailing_spaces.rs +0 -65
- package/src/rules/truthy.rs +0 -420
- package/tests/brackets_carriage_return.rs +0 -114
- package/tests/build_global_cfg_error.rs +0 -23
- package/tests/cli_anchors_rule.rs +0 -143
- package/tests/cli_braces_rule.rs +0 -104
- package/tests/cli_brackets_rule.rs +0 -104
- package/tests/cli_colons_rule.rs +0 -65
- package/tests/cli_commas_rule.rs +0 -104
- package/tests/cli_comments_indentation_rule.rs +0 -61
- package/tests/cli_comments_rule.rs +0 -67
- package/tests/cli_config_data_error.rs +0 -30
- package/tests/cli_config_flags.rs +0 -66
- package/tests/cli_config_migrate.rs +0 -229
- package/tests/cli_document_end_rule.rs +0 -92
- package/tests/cli_document_start_rule.rs +0 -92
- package/tests/cli_empty_lines_rule.rs +0 -87
- package/tests/cli_empty_values_rule.rs +0 -68
- package/tests/cli_env_config.rs +0 -34
- package/tests/cli_exit_and_errors.rs +0 -41
- package/tests/cli_file_encoding.rs +0 -203
- package/tests/cli_float_values_rule.rs +0 -64
- package/tests/cli_format_options.rs +0 -316
- package/tests/cli_global_cfg_relaxed.rs +0 -20
- package/tests/cli_hyphens_rule.rs +0 -104
- package/tests/cli_indentation_rule.rs +0 -65
- package/tests/cli_invalid_project_config.rs +0 -39
- package/tests/cli_key_duplicates_rule.rs +0 -104
- package/tests/cli_key_ordering_rule.rs +0 -59
- package/tests/cli_line_length_rule.rs +0 -85
- package/tests/cli_list_files.rs +0 -29
- package/tests/cli_new_line_rule.rs +0 -141
- package/tests/cli_new_lines_rule.rs +0 -119
- package/tests/cli_octal_values_rule.rs +0 -60
- package/tests/cli_quoted_strings_rule.rs +0 -47
- package/tests/cli_toml_config.rs +0 -119
- package/tests/cli_trailing_spaces_rule.rs +0 -77
- package/tests/cli_truthy_rule.rs +0 -83
- package/tests/cli_yaml_files_negation.rs +0 -45
- package/tests/colons_rule.rs +0 -303
- package/tests/common/compat.rs +0 -114
- package/tests/common/fake_env.rs +0 -93
- package/tests/common/mod.rs +0 -1
- package/tests/conf_builtin.rs +0 -9
- package/tests/config_anchors.rs +0 -84
- package/tests/config_braces.rs +0 -121
- package/tests/config_brackets.rs +0 -127
- package/tests/config_commas.rs +0 -79
- package/tests/config_comments.rs +0 -65
- package/tests/config_comments_indentation.rs +0 -20
- package/tests/config_deep_merge_nonstring_key.rs +0 -24
- package/tests/config_document_end.rs +0 -54
- package/tests/config_document_start.rs +0 -55
- package/tests/config_empty_lines.rs +0 -48
- package/tests/config_empty_values.rs +0 -35
- package/tests/config_env_errors.rs +0 -23
- package/tests/config_env_invalid_inline.rs +0 -15
- package/tests/config_env_missing.rs +0 -63
- package/tests/config_env_shim.rs +0 -301
- package/tests/config_explicit_file_parse_error.rs +0 -55
- package/tests/config_extended_features.rs +0 -225
- package/tests/config_extends_inline.rs +0 -185
- package/tests/config_extends_sequence.rs +0 -18
- package/tests/config_find_project_home_boundary.rs +0 -54
- package/tests/config_find_project_two_files_in_cwd.rs +0 -47
- package/tests/config_float_values.rs +0 -34
- package/tests/config_from_yaml_paths.rs +0 -32
- package/tests/config_hyphens.rs +0 -51
- package/tests/config_ignore_errors.rs +0 -243
- package/tests/config_ignore_overrides.rs +0 -83
- package/tests/config_indentation.rs +0 -65
- package/tests/config_invalid_globs.rs +0 -16
- package/tests/config_invalid_types.rs +0 -19
- package/tests/config_key_duplicates.rs +0 -34
- package/tests/config_key_ordering.rs +0 -70
- package/tests/config_line_length.rs +0 -65
- package/tests/config_locale.rs +0 -111
- package/tests/config_merge.rs +0 -26
- package/tests/config_new_lines.rs +0 -89
- package/tests/config_octal_values.rs +0 -33
- package/tests/config_quoted_strings.rs +0 -195
- package/tests/config_rule_level.rs +0 -147
- package/tests/config_rules_non_string_keys.rs +0 -23
- package/tests/config_scalar_overrides.rs +0 -27
- package/tests/config_to_toml.rs +0 -110
- package/tests/config_toml_coverage.rs +0 -80
- package/tests/config_toml_discovery.rs +0 -304
- package/tests/config_trailing_spaces.rs +0 -152
- package/tests/config_truthy.rs +0 -77
- package/tests/config_yaml_files.rs +0 -62
- package/tests/config_yaml_files_all_non_string.rs +0 -15
- package/tests/config_yaml_files_empty.rs +0 -30
- package/tests/coverage_commas.rs +0 -46
- package/tests/decoder_decode.rs +0 -338
- package/tests/discover_config_bin_all.rs +0 -66
- package/tests/discover_config_bin_env_invalid_yaml.rs +0 -26
- package/tests/discover_config_bin_project_config_parse_error.rs +0 -24
- package/tests/discover_config_bin_user_global_error.rs +0 -26
- package/tests/discover_module.rs +0 -30
- package/tests/discover_per_file_dir.rs +0 -10
- package/tests/discover_per_file_project_config_error.rs +0 -21
- package/tests/float_values.rs +0 -43
- package/tests/lint_multi_errors.rs +0 -32
- package/tests/main_yaml_ok_filtering.rs +0 -30
- package/tests/migrate_module.rs +0 -259
- package/tests/resolve_ctx_empty_parent.rs +0 -16
- package/tests/rule_anchors.rs +0 -442
- package/tests/rule_braces.rs +0 -258
- package/tests/rule_brackets.rs +0 -217
- package/tests/rule_commas.rs +0 -205
- package/tests/rule_comments.rs +0 -197
- package/tests/rule_comments_indentation.rs +0 -127
- package/tests/rule_document_end.rs +0 -118
- package/tests/rule_document_start.rs +0 -60
- package/tests/rule_empty_lines.rs +0 -96
- package/tests/rule_empty_values.rs +0 -102
- package/tests/rule_float_values.rs +0 -109
- package/tests/rule_hyphens.rs +0 -65
- package/tests/rule_indentation.rs +0 -455
- package/tests/rule_key_duplicates.rs +0 -76
- package/tests/rule_key_ordering.rs +0 -207
- package/tests/rule_line_length.rs +0 -200
- package/tests/rule_new_lines.rs +0 -51
- package/tests/rule_octal_values.rs +0 -53
- package/tests/rule_quoted_strings.rs +0 -290
- package/tests/rule_trailing_spaces.rs +0 -41
- package/tests/rule_truthy.rs +0 -236
- package/tests/user_global_invalid_yaml.rs +0 -32
- package/tests/yamllint_compat_anchors.rs +0 -280
- package/tests/yamllint_compat_braces.rs +0 -411
- package/tests/yamllint_compat_brackets.rs +0 -364
- package/tests/yamllint_compat_colons.rs +0 -298
- package/tests/yamllint_compat_colors.rs +0 -80
- package/tests/yamllint_compat_commas.rs +0 -375
- package/tests/yamllint_compat_comments.rs +0 -167
- package/tests/yamllint_compat_comments_indentation.rs +0 -281
- package/tests/yamllint_compat_config.rs +0 -170
- package/tests/yamllint_compat_document_end.rs +0 -243
- package/tests/yamllint_compat_document_start.rs +0 -136
- package/tests/yamllint_compat_empty_lines.rs +0 -117
- package/tests/yamllint_compat_empty_values.rs +0 -179
- package/tests/yamllint_compat_float_values.rs +0 -216
- package/tests/yamllint_compat_hyphens.rs +0 -223
- package/tests/yamllint_compat_indentation.rs +0 -398
- package/tests/yamllint_compat_key_duplicates.rs +0 -139
- package/tests/yamllint_compat_key_ordering.rs +0 -170
- package/tests/yamllint_compat_line_length.rs +0 -375
- package/tests/yamllint_compat_list.rs +0 -127
- package/tests/yamllint_compat_new_line.rs +0 -133
- package/tests/yamllint_compat_newline_types.rs +0 -185
- package/tests/yamllint_compat_octal_values.rs +0 -172
- package/tests/yamllint_compat_quoted_strings.rs +0 -154
- package/tests/yamllint_compat_syntax.rs +0 -200
- package/tests/yamllint_compat_trailing_spaces.rs +0 -162
- package/tests/yamllint_compat_truthy.rs +0 -130
- package/tests/yamllint_compat_yaml_files.rs +0 -81
- package/typos.toml +0 -2
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
use saphyr_parser::{Event, Parser, Span, SpannedEventReceiver};
|
|
2
|
-
|
|
3
|
-
use crate::config::YamlLintConfig;
|
|
4
|
-
|
|
5
|
-
pub const ID: &str = "key-duplicates";
|
|
6
|
-
|
|
7
|
-
#[derive(Debug, Clone, Copy)]
|
|
8
|
-
pub struct Config {
|
|
9
|
-
forbid_duplicated_merge_keys: bool,
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
impl Config {
|
|
13
|
-
#[must_use]
|
|
14
|
-
pub fn resolve(cfg: &YamlLintConfig) -> Self {
|
|
15
|
-
let forbid_duplicated_merge_keys = cfg
|
|
16
|
-
.rule_option(ID, "forbid-duplicated-merge-keys")
|
|
17
|
-
.and_then(saphyr::YamlOwned::as_bool)
|
|
18
|
-
.unwrap_or(false);
|
|
19
|
-
|
|
20
|
-
Self {
|
|
21
|
-
forbid_duplicated_merge_keys,
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
27
|
-
pub struct Violation {
|
|
28
|
-
pub line: usize,
|
|
29
|
-
pub column: usize,
|
|
30
|
-
pub message: String,
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
#[must_use]
|
|
34
|
-
pub fn check(buffer: &str, cfg: &Config) -> Vec<Violation> {
|
|
35
|
-
let mut parser = Parser::new_from_str(buffer);
|
|
36
|
-
let mut receiver = KeyDuplicatesReceiver::new(cfg);
|
|
37
|
-
let _ = parser.load(&mut receiver, true);
|
|
38
|
-
receiver.violations
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
struct KeyDuplicatesReceiver<'cfg> {
|
|
42
|
-
state: KeyDuplicatesState<'cfg>,
|
|
43
|
-
violations: Vec<Violation>,
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
impl<'cfg> KeyDuplicatesReceiver<'cfg> {
|
|
47
|
-
const fn new(config: &'cfg Config) -> Self {
|
|
48
|
-
Self {
|
|
49
|
-
state: KeyDuplicatesState::new(config),
|
|
50
|
-
violations: Vec::new(),
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
impl SpannedEventReceiver<'_> for KeyDuplicatesReceiver<'_> {
|
|
56
|
-
fn on_event(&mut self, event: Event<'_>, span: Span) {
|
|
57
|
-
match event {
|
|
58
|
-
Event::StreamStart => self.state.reset_stream(),
|
|
59
|
-
Event::DocumentStart(_) => self.state.document_start(),
|
|
60
|
-
Event::DocumentEnd => self.state.document_end(),
|
|
61
|
-
Event::SequenceStart(_, _) => self.state.enter_sequence(),
|
|
62
|
-
Event::SequenceEnd | Event::MappingEnd => self.state.exit_container(),
|
|
63
|
-
Event::MappingStart(_, _) => self.state.enter_mapping(),
|
|
64
|
-
Event::Scalar(value, _, _, _) => {
|
|
65
|
-
self.state
|
|
66
|
-
.handle_scalar(value.as_ref(), span, &mut self.violations);
|
|
67
|
-
}
|
|
68
|
-
Event::Alias(_) => self.state.handle_alias(),
|
|
69
|
-
Event::StreamEnd | Event::Nothing => {}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
struct KeyDuplicatesState<'cfg> {
|
|
75
|
-
config: &'cfg Config,
|
|
76
|
-
containers: Vec<ContainerState>,
|
|
77
|
-
key_depth: usize,
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
impl<'cfg> KeyDuplicatesState<'cfg> {
|
|
81
|
-
const fn new(config: &'cfg Config) -> Self {
|
|
82
|
-
Self {
|
|
83
|
-
config,
|
|
84
|
-
containers: Vec::new(),
|
|
85
|
-
key_depth: 0,
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
fn reset_stream(&mut self) {
|
|
90
|
-
self.containers.clear();
|
|
91
|
-
self.key_depth = 0;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
fn document_start(&mut self) {
|
|
95
|
-
self.containers.clear();
|
|
96
|
-
self.key_depth = 0;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
fn document_end(&mut self) {
|
|
100
|
-
self.containers.clear();
|
|
101
|
-
self.key_depth = 0;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
fn enter_mapping(&mut self) {
|
|
105
|
-
let context = self.begin_node();
|
|
106
|
-
self.containers.push(ContainerState {
|
|
107
|
-
key_context: context.active,
|
|
108
|
-
mapping: Some(MappingState::new()),
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
fn enter_sequence(&mut self) {
|
|
113
|
-
let context = self.begin_node();
|
|
114
|
-
self.containers.push(ContainerState {
|
|
115
|
-
key_context: context.active,
|
|
116
|
-
mapping: None,
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
fn exit_container(&mut self) {
|
|
121
|
-
if let Some(container) = self.containers.pop()
|
|
122
|
-
&& container.key_context
|
|
123
|
-
&& self.key_depth > 0
|
|
124
|
-
{
|
|
125
|
-
self.key_depth -= 1;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
fn handle_scalar(
|
|
130
|
-
&mut self,
|
|
131
|
-
value: &str,
|
|
132
|
-
span: Span,
|
|
133
|
-
diagnostics: &mut Vec<Violation>,
|
|
134
|
-
) {
|
|
135
|
-
let context = self.begin_node();
|
|
136
|
-
if !context.key_root {
|
|
137
|
-
self.finish_node(context);
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
let state = self
|
|
142
|
-
.containers
|
|
143
|
-
.last_mut()
|
|
144
|
-
.and_then(|container| container.mapping.as_mut())
|
|
145
|
-
.expect("mapping state should exist when key_root is active");
|
|
146
|
-
|
|
147
|
-
let is_duplicate = state.seen_keys.iter().any(|key| key == value);
|
|
148
|
-
let is_merge_key = value == "<<";
|
|
149
|
-
if is_duplicate && (!is_merge_key || self.config.forbid_duplicated_merge_keys) {
|
|
150
|
-
diagnostics.push(Violation {
|
|
151
|
-
line: span.start.line(),
|
|
152
|
-
column: span.start.col() + 1,
|
|
153
|
-
message: format!("duplication of key \"{value}\" in mapping"),
|
|
154
|
-
});
|
|
155
|
-
} else {
|
|
156
|
-
state.seen_keys.push(value.to_owned());
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
self.finish_node(context);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
fn begin_node(&mut self) -> NodeContext {
|
|
163
|
-
let mut key_root = false;
|
|
164
|
-
if let Some(ContainerState {
|
|
165
|
-
mapping: Some(mapping),
|
|
166
|
-
..
|
|
167
|
-
}) = self.containers.last_mut()
|
|
168
|
-
{
|
|
169
|
-
if mapping.expect_key {
|
|
170
|
-
key_root = true;
|
|
171
|
-
mapping.expect_key = false;
|
|
172
|
-
} else {
|
|
173
|
-
mapping.expect_key = true;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
let active = key_root || self.key_depth > 0;
|
|
177
|
-
if active {
|
|
178
|
-
self.key_depth += 1;
|
|
179
|
-
}
|
|
180
|
-
NodeContext { active, key_root }
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const fn finish_node(&mut self, context: NodeContext) {
|
|
184
|
-
if context.active && self.key_depth > 0 {
|
|
185
|
-
self.key_depth -= 1;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
fn handle_alias(&mut self) {
|
|
190
|
-
let context = self.begin_node();
|
|
191
|
-
self.finish_node(context);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
struct ContainerState {
|
|
196
|
-
key_context: bool,
|
|
197
|
-
mapping: Option<MappingState>,
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
struct MappingState {
|
|
201
|
-
expect_key: bool,
|
|
202
|
-
seen_keys: Vec<String>,
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
impl MappingState {
|
|
206
|
-
const fn new() -> Self {
|
|
207
|
-
Self {
|
|
208
|
-
expect_key: true,
|
|
209
|
-
seen_keys: Vec::new(),
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
#[derive(Copy, Clone)]
|
|
215
|
-
struct NodeContext {
|
|
216
|
-
active: bool,
|
|
217
|
-
key_root: bool,
|
|
218
|
-
}
|
|
@@ -1,303 +0,0 @@
|
|
|
1
|
-
use regex::Regex;
|
|
2
|
-
use saphyr_parser::{Event, Parser, Span, SpannedEventReceiver};
|
|
3
|
-
use unicode_normalization::{UnicodeNormalization, char::is_combining_mark};
|
|
4
|
-
|
|
5
|
-
use crate::config::YamlLintConfig;
|
|
6
|
-
|
|
7
|
-
pub const ID: &str = "key-ordering";
|
|
8
|
-
|
|
9
|
-
#[derive(Debug, Clone)]
|
|
10
|
-
pub struct Config {
|
|
11
|
-
ignored: Vec<Regex>,
|
|
12
|
-
comparator: Comparator,
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
impl Config {
|
|
16
|
-
#[must_use]
|
|
17
|
-
/// Resolve the rule configuration from the parsed yamllint config.
|
|
18
|
-
///
|
|
19
|
-
/// # Panics
|
|
20
|
-
///
|
|
21
|
-
/// Panics when `ignored-keys` entries are not strings. Configuration parsing
|
|
22
|
-
/// validates types before resolution, so this only occurs with manual construction in tests.
|
|
23
|
-
pub fn resolve(cfg: &YamlLintConfig) -> Self {
|
|
24
|
-
let mut ignored: Vec<Regex> = Vec::new();
|
|
25
|
-
if let Some(node) = cfg.rule_option(ID, "ignored-keys") {
|
|
26
|
-
if let saphyr::YamlOwned::Sequence(seq) = node {
|
|
27
|
-
for entry in seq {
|
|
28
|
-
let pattern = entry
|
|
29
|
-
.as_str()
|
|
30
|
-
.expect("key-ordering ignored-keys should be strings");
|
|
31
|
-
ignored.push(
|
|
32
|
-
Regex::new(pattern).expect("key-ordering ignored-keys regex"),
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if let saphyr::YamlOwned::Value(value) = node
|
|
37
|
-
&& let Some(text) = value.as_str()
|
|
38
|
-
{
|
|
39
|
-
ignored
|
|
40
|
-
.push(Regex::new(text).expect("key-ordering ignored-keys regex"));
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
let comparator = cfg
|
|
45
|
-
.locale()
|
|
46
|
-
.map_or_else(Comparator::codepoint, Comparator::with_locale);
|
|
47
|
-
|
|
48
|
-
Self {
|
|
49
|
-
ignored,
|
|
50
|
-
comparator,
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
fn is_ignored(&self, key: &str) -> bool {
|
|
55
|
-
self.ignored.iter().any(|re| re.is_match(key))
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
fn in_order(&self, previous: Option<&str>, current: &str) -> bool {
|
|
59
|
-
let Some(prev) = previous else {
|
|
60
|
-
return true;
|
|
61
|
-
};
|
|
62
|
-
!matches!(
|
|
63
|
-
self.comparator.compare(prev, current),
|
|
64
|
-
std::cmp::Ordering::Greater
|
|
65
|
-
)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
#[derive(Debug, Clone)]
|
|
70
|
-
enum Comparator {
|
|
71
|
-
Codepoint,
|
|
72
|
-
Locale(LocaleComparator),
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
impl Comparator {
|
|
76
|
-
const fn codepoint() -> Self {
|
|
77
|
-
Self::Codepoint
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
fn with_locale(locale: &str) -> Self {
|
|
81
|
-
let base = if let Some((head, _)) = locale.split_once(['.', '@']) {
|
|
82
|
-
head
|
|
83
|
-
} else {
|
|
84
|
-
locale
|
|
85
|
-
};
|
|
86
|
-
if base.eq_ignore_ascii_case("C") || base.eq_ignore_ascii_case("POSIX") {
|
|
87
|
-
Self::Codepoint
|
|
88
|
-
} else {
|
|
89
|
-
Self::Locale(LocaleComparator::new(locale))
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
fn compare(&self, left: &str, right: &str) -> std::cmp::Ordering {
|
|
94
|
-
match self {
|
|
95
|
-
Self::Codepoint => left.cmp(right),
|
|
96
|
-
Self::Locale(_locale) => LocaleComparator::compare(left, right),
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
#[derive(Debug, Clone)]
|
|
102
|
-
struct LocaleComparator;
|
|
103
|
-
|
|
104
|
-
impl LocaleComparator {
|
|
105
|
-
const fn new(_locale: &str) -> Self {
|
|
106
|
-
Self
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
fn compare(left: &str, right: &str) -> std::cmp::Ordering {
|
|
110
|
-
let lhs = normalize_for_locale(left);
|
|
111
|
-
let rhs = normalize_for_locale(right);
|
|
112
|
-
lhs.cmp(&rhs)
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
fn normalize_for_locale(value: &str) -> String {
|
|
117
|
-
let decomposed: String = value.nfkd().filter(|c| !is_combining_mark(*c)).collect();
|
|
118
|
-
decomposed.to_lowercase()
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
122
|
-
pub struct Violation {
|
|
123
|
-
pub line: usize,
|
|
124
|
-
pub column: usize,
|
|
125
|
-
pub message: String,
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
#[must_use]
|
|
129
|
-
pub fn check(buffer: &str, cfg: &Config) -> Vec<Violation> {
|
|
130
|
-
let mut parser = Parser::new_from_str(buffer);
|
|
131
|
-
let mut receiver = KeyOrderingReceiver::new(cfg);
|
|
132
|
-
let _ = parser.load(&mut receiver, true);
|
|
133
|
-
receiver.violations
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
struct KeyOrderingReceiver<'cfg> {
|
|
137
|
-
state: KeyOrderingState<'cfg>,
|
|
138
|
-
violations: Vec<Violation>,
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
impl<'cfg> KeyOrderingReceiver<'cfg> {
|
|
142
|
-
#[allow(clippy::missing_const_for_fn)]
|
|
143
|
-
fn new(cfg: &'cfg Config) -> Self {
|
|
144
|
-
Self {
|
|
145
|
-
state: KeyOrderingState::new(cfg),
|
|
146
|
-
violations: Vec::new(),
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
impl SpannedEventReceiver<'_> for KeyOrderingReceiver<'_> {
|
|
152
|
-
fn on_event(&mut self, event: Event<'_>, span: Span) {
|
|
153
|
-
match event {
|
|
154
|
-
Event::StreamStart => self.state.reset_stream(),
|
|
155
|
-
Event::DocumentStart(_) => self.state.document_start(),
|
|
156
|
-
Event::DocumentEnd => self.state.document_end(),
|
|
157
|
-
Event::SequenceStart(_, _) => self.state.enter_sequence(),
|
|
158
|
-
Event::SequenceEnd | Event::MappingEnd => self.state.exit_container(),
|
|
159
|
-
Event::MappingStart(_, _) => self.state.enter_mapping(),
|
|
160
|
-
Event::Scalar(value, _, _, _) => {
|
|
161
|
-
self.state
|
|
162
|
-
.handle_scalar(value.as_ref(), span, &mut self.violations);
|
|
163
|
-
}
|
|
164
|
-
Event::Alias(_) | Event::StreamEnd | Event::Nothing => {}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
struct KeyOrderingState<'cfg> {
|
|
170
|
-
config: &'cfg Config,
|
|
171
|
-
containers: Vec<ContainerState>,
|
|
172
|
-
key_depth: usize,
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
impl<'cfg> KeyOrderingState<'cfg> {
|
|
176
|
-
const fn new(config: &'cfg Config) -> Self {
|
|
177
|
-
Self {
|
|
178
|
-
config,
|
|
179
|
-
containers: Vec::new(),
|
|
180
|
-
key_depth: 0,
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
fn reset_stream(&mut self) {
|
|
185
|
-
self.containers.clear();
|
|
186
|
-
self.key_depth = 0;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
fn document_start(&mut self) {
|
|
190
|
-
self.containers.clear();
|
|
191
|
-
self.key_depth = 0;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
fn document_end(&mut self) {
|
|
195
|
-
self.containers.clear();
|
|
196
|
-
self.key_depth = 0;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
fn enter_mapping(&mut self) {
|
|
200
|
-
let ctx = self.begin_node();
|
|
201
|
-
self.containers.push(ContainerState {
|
|
202
|
-
key_context: ctx.active,
|
|
203
|
-
mapping: Some(MappingState {
|
|
204
|
-
expect_key: true,
|
|
205
|
-
keys: Vec::new(),
|
|
206
|
-
}),
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
fn enter_sequence(&mut self) {
|
|
211
|
-
let ctx = self.begin_node();
|
|
212
|
-
self.containers.push(ContainerState {
|
|
213
|
-
key_context: ctx.active,
|
|
214
|
-
mapping: None,
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
fn exit_container(&mut self) {
|
|
219
|
-
let container = self
|
|
220
|
-
.containers
|
|
221
|
-
.pop()
|
|
222
|
-
.expect("container stack should not underflow");
|
|
223
|
-
if container.key_context && self.key_depth > 0 {
|
|
224
|
-
self.key_depth -= 1;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
fn handle_scalar(
|
|
229
|
-
&mut self,
|
|
230
|
-
value: &str,
|
|
231
|
-
span: Span,
|
|
232
|
-
diagnostics: &mut Vec<Violation>,
|
|
233
|
-
) {
|
|
234
|
-
let context = self.begin_node();
|
|
235
|
-
if !context.key_root || self.config.is_ignored(value) {
|
|
236
|
-
self.finish_node(context);
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
let state = self
|
|
241
|
-
.containers
|
|
242
|
-
.last_mut()
|
|
243
|
-
.expect("stack should contain mapping when key root is active");
|
|
244
|
-
let mapping = state
|
|
245
|
-
.mapping
|
|
246
|
-
.as_mut()
|
|
247
|
-
.expect("key root should only be active for mappings");
|
|
248
|
-
let keys = &mut mapping.keys;
|
|
249
|
-
if self.config.in_order(keys.last().map(String::as_str), value) {
|
|
250
|
-
keys.push(value.to_owned());
|
|
251
|
-
} else {
|
|
252
|
-
diagnostics.push(Violation {
|
|
253
|
-
line: span.start.line(),
|
|
254
|
-
column: span.start.col() + 1,
|
|
255
|
-
message: format!("wrong ordering of key \"{value}\" in mapping"),
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
self.finish_node(context);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
fn begin_node(&mut self) -> NodeContext {
|
|
262
|
-
let mut key_root = false;
|
|
263
|
-
if let Some(ContainerState {
|
|
264
|
-
mapping: Some(mapping),
|
|
265
|
-
..
|
|
266
|
-
}) = self.containers.last_mut()
|
|
267
|
-
{
|
|
268
|
-
if mapping.expect_key {
|
|
269
|
-
key_root = true;
|
|
270
|
-
mapping.expect_key = false;
|
|
271
|
-
} else {
|
|
272
|
-
mapping.expect_key = true;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
let active = key_root || self.key_depth > 0;
|
|
276
|
-
if active {
|
|
277
|
-
self.key_depth += 1;
|
|
278
|
-
}
|
|
279
|
-
NodeContext { active, key_root }
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
const fn finish_node(&mut self, context: NodeContext) {
|
|
283
|
-
if context.active && self.key_depth > 0 {
|
|
284
|
-
self.key_depth -= 1;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
struct ContainerState {
|
|
290
|
-
key_context: bool,
|
|
291
|
-
mapping: Option<MappingState>,
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
struct MappingState {
|
|
295
|
-
expect_key: bool,
|
|
296
|
-
keys: Vec<String>,
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
#[derive(Copy, Clone)]
|
|
300
|
-
struct NodeContext {
|
|
301
|
-
active: bool,
|
|
302
|
-
key_root: bool,
|
|
303
|
-
}
|