@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.
- package/.github/CODEOWNERS +1 -0
- package/.github/dependabot.yml +13 -0
- package/.github/workflows/ci.yml +107 -0
- package/.github/workflows/release.yml +613 -0
- package/.github/workflows/update_dependencies.yml +61 -0
- package/.github/workflows/update_linters.yml +56 -0
- package/.pre-commit-config.yaml +87 -0
- package/.yamllint +4 -0
- package/AGENTS.md +200 -0
- package/Cargo.lock +908 -0
- package/Cargo.toml +32 -0
- package/LICENSE +21 -0
- package/README.md +230 -0
- package/bin/ryl.js +1 -0
- package/clippy.toml +1 -0
- package/docs/config-presets.md +100 -0
- package/img/benchmark-5x5-5runs.svg +2176 -0
- package/package.json +28 -0
- package/pyproject.toml +42 -0
- package/ruff.toml +107 -0
- package/rumdl.toml +20 -0
- package/rust-toolchain.toml +3 -0
- package/rustfmt.toml +3 -0
- package/scripts/benchmark_perf_vs_yamllint.py +400 -0
- package/scripts/coverage-missing.ps1 +80 -0
- package/scripts/coverage-missing.sh +60 -0
- package/src/bin/discover_config_bin.rs +24 -0
- package/src/cli_support.rs +33 -0
- package/src/conf/mod.rs +85 -0
- package/src/config.rs +2099 -0
- package/src/decoder.rs +326 -0
- package/src/discover.rs +31 -0
- package/src/lib.rs +19 -0
- package/src/lint.rs +558 -0
- package/src/main.rs +535 -0
- package/src/migrate.rs +233 -0
- package/src/rules/anchors.rs +517 -0
- package/src/rules/braces.rs +77 -0
- package/src/rules/brackets.rs +77 -0
- package/src/rules/colons.rs +475 -0
- package/src/rules/commas.rs +372 -0
- package/src/rules/comments.rs +299 -0
- package/src/rules/comments_indentation.rs +243 -0
- package/src/rules/document_end.rs +175 -0
- package/src/rules/document_start.rs +84 -0
- package/src/rules/empty_lines.rs +152 -0
- package/src/rules/empty_values.rs +255 -0
- package/src/rules/float_values.rs +259 -0
- package/src/rules/flow_collection.rs +562 -0
- package/src/rules/hyphens.rs +104 -0
- package/src/rules/indentation.rs +803 -0
- package/src/rules/key_duplicates.rs +218 -0
- package/src/rules/key_ordering.rs +303 -0
- package/src/rules/line_length.rs +326 -0
- package/src/rules/mod.rs +25 -0
- package/src/rules/new_line_at_end_of_file.rs +23 -0
- package/src/rules/new_lines.rs +95 -0
- package/src/rules/octal_values.rs +121 -0
- package/src/rules/quoted_strings.rs +577 -0
- package/src/rules/span_utils.rs +37 -0
- package/src/rules/trailing_spaces.rs +65 -0
- package/src/rules/truthy.rs +420 -0
- package/tests/brackets_carriage_return.rs +114 -0
- package/tests/build_global_cfg_error.rs +23 -0
- package/tests/cli_anchors_rule.rs +143 -0
- package/tests/cli_braces_rule.rs +104 -0
- package/tests/cli_brackets_rule.rs +104 -0
- package/tests/cli_colons_rule.rs +65 -0
- package/tests/cli_commas_rule.rs +104 -0
- package/tests/cli_comments_indentation_rule.rs +61 -0
- package/tests/cli_comments_rule.rs +67 -0
- package/tests/cli_config_data_error.rs +30 -0
- package/tests/cli_config_flags.rs +66 -0
- package/tests/cli_config_migrate.rs +229 -0
- package/tests/cli_document_end_rule.rs +92 -0
- package/tests/cli_document_start_rule.rs +92 -0
- package/tests/cli_empty_lines_rule.rs +87 -0
- package/tests/cli_empty_values_rule.rs +68 -0
- package/tests/cli_env_config.rs +34 -0
- package/tests/cli_exit_and_errors.rs +41 -0
- package/tests/cli_file_encoding.rs +203 -0
- package/tests/cli_float_values_rule.rs +64 -0
- package/tests/cli_format_options.rs +316 -0
- package/tests/cli_global_cfg_relaxed.rs +20 -0
- package/tests/cli_hyphens_rule.rs +104 -0
- package/tests/cli_indentation_rule.rs +65 -0
- package/tests/cli_invalid_project_config.rs +39 -0
- package/tests/cli_key_duplicates_rule.rs +104 -0
- package/tests/cli_key_ordering_rule.rs +59 -0
- package/tests/cli_line_length_rule.rs +85 -0
- package/tests/cli_list_files.rs +29 -0
- package/tests/cli_new_line_rule.rs +141 -0
- package/tests/cli_new_lines_rule.rs +119 -0
- package/tests/cli_octal_values_rule.rs +60 -0
- package/tests/cli_quoted_strings_rule.rs +47 -0
- package/tests/cli_toml_config.rs +119 -0
- package/tests/cli_trailing_spaces_rule.rs +77 -0
- package/tests/cli_truthy_rule.rs +83 -0
- package/tests/cli_yaml_files_negation.rs +45 -0
- package/tests/colons_rule.rs +303 -0
- package/tests/common/compat.rs +114 -0
- package/tests/common/fake_env.rs +93 -0
- package/tests/common/mod.rs +1 -0
- package/tests/conf_builtin.rs +9 -0
- package/tests/config_anchors.rs +84 -0
- package/tests/config_braces.rs +121 -0
- package/tests/config_brackets.rs +127 -0
- package/tests/config_commas.rs +79 -0
- package/tests/config_comments.rs +65 -0
- package/tests/config_comments_indentation.rs +20 -0
- package/tests/config_deep_merge_nonstring_key.rs +24 -0
- package/tests/config_document_end.rs +54 -0
- package/tests/config_document_start.rs +55 -0
- package/tests/config_empty_lines.rs +48 -0
- package/tests/config_empty_values.rs +35 -0
- package/tests/config_env_errors.rs +23 -0
- package/tests/config_env_invalid_inline.rs +15 -0
- package/tests/config_env_missing.rs +63 -0
- package/tests/config_env_shim.rs +301 -0
- package/tests/config_explicit_file_parse_error.rs +55 -0
- package/tests/config_extended_features.rs +225 -0
- package/tests/config_extends_inline.rs +185 -0
- package/tests/config_extends_sequence.rs +18 -0
- package/tests/config_find_project_home_boundary.rs +54 -0
- package/tests/config_find_project_two_files_in_cwd.rs +47 -0
- package/tests/config_float_values.rs +34 -0
- package/tests/config_from_yaml_paths.rs +32 -0
- package/tests/config_hyphens.rs +51 -0
- package/tests/config_ignore_errors.rs +243 -0
- package/tests/config_ignore_overrides.rs +83 -0
- package/tests/config_indentation.rs +65 -0
- package/tests/config_invalid_globs.rs +16 -0
- package/tests/config_invalid_types.rs +19 -0
- package/tests/config_key_duplicates.rs +34 -0
- package/tests/config_key_ordering.rs +70 -0
- package/tests/config_line_length.rs +65 -0
- package/tests/config_locale.rs +111 -0
- package/tests/config_merge.rs +26 -0
- package/tests/config_new_lines.rs +89 -0
- package/tests/config_octal_values.rs +33 -0
- package/tests/config_quoted_strings.rs +195 -0
- package/tests/config_rule_level.rs +147 -0
- package/tests/config_rules_non_string_keys.rs +23 -0
- package/tests/config_scalar_overrides.rs +27 -0
- package/tests/config_to_toml.rs +110 -0
- package/tests/config_toml_coverage.rs +80 -0
- package/tests/config_toml_discovery.rs +304 -0
- package/tests/config_trailing_spaces.rs +152 -0
- package/tests/config_truthy.rs +77 -0
- package/tests/config_yaml_files.rs +62 -0
- package/tests/config_yaml_files_all_non_string.rs +15 -0
- package/tests/config_yaml_files_empty.rs +30 -0
- package/tests/coverage_commas.rs +46 -0
- package/tests/decoder_decode.rs +338 -0
- package/tests/discover_config_bin_all.rs +66 -0
- package/tests/discover_config_bin_env_invalid_yaml.rs +26 -0
- package/tests/discover_config_bin_project_config_parse_error.rs +24 -0
- package/tests/discover_config_bin_user_global_error.rs +26 -0
- package/tests/discover_module.rs +30 -0
- package/tests/discover_per_file_dir.rs +10 -0
- package/tests/discover_per_file_project_config_error.rs +21 -0
- package/tests/float_values.rs +43 -0
- package/tests/lint_multi_errors.rs +32 -0
- package/tests/main_yaml_ok_filtering.rs +30 -0
- package/tests/migrate_module.rs +259 -0
- package/tests/resolve_ctx_empty_parent.rs +16 -0
- package/tests/rule_anchors.rs +442 -0
- package/tests/rule_braces.rs +258 -0
- package/tests/rule_brackets.rs +217 -0
- package/tests/rule_commas.rs +205 -0
- package/tests/rule_comments.rs +197 -0
- package/tests/rule_comments_indentation.rs +127 -0
- package/tests/rule_document_end.rs +118 -0
- package/tests/rule_document_start.rs +60 -0
- package/tests/rule_empty_lines.rs +96 -0
- package/tests/rule_empty_values.rs +102 -0
- package/tests/rule_float_values.rs +109 -0
- package/tests/rule_hyphens.rs +65 -0
- package/tests/rule_indentation.rs +455 -0
- package/tests/rule_key_duplicates.rs +76 -0
- package/tests/rule_key_ordering.rs +207 -0
- package/tests/rule_line_length.rs +200 -0
- package/tests/rule_new_lines.rs +51 -0
- package/tests/rule_octal_values.rs +53 -0
- package/tests/rule_quoted_strings.rs +290 -0
- package/tests/rule_trailing_spaces.rs +41 -0
- package/tests/rule_truthy.rs +236 -0
- package/tests/user_global_invalid_yaml.rs +32 -0
- package/tests/yamllint_compat_anchors.rs +280 -0
- package/tests/yamllint_compat_braces.rs +411 -0
- package/tests/yamllint_compat_brackets.rs +364 -0
- package/tests/yamllint_compat_colons.rs +298 -0
- package/tests/yamllint_compat_colors.rs +80 -0
- package/tests/yamllint_compat_commas.rs +375 -0
- package/tests/yamllint_compat_comments.rs +167 -0
- package/tests/yamllint_compat_comments_indentation.rs +281 -0
- package/tests/yamllint_compat_config.rs +170 -0
- package/tests/yamllint_compat_document_end.rs +243 -0
- package/tests/yamllint_compat_document_start.rs +136 -0
- package/tests/yamllint_compat_empty_lines.rs +117 -0
- package/tests/yamllint_compat_empty_values.rs +179 -0
- package/tests/yamllint_compat_float_values.rs +216 -0
- package/tests/yamllint_compat_hyphens.rs +223 -0
- package/tests/yamllint_compat_indentation.rs +398 -0
- package/tests/yamllint_compat_key_duplicates.rs +139 -0
- package/tests/yamllint_compat_key_ordering.rs +170 -0
- package/tests/yamllint_compat_line_length.rs +375 -0
- package/tests/yamllint_compat_list.rs +127 -0
- package/tests/yamllint_compat_new_line.rs +133 -0
- package/tests/yamllint_compat_newline_types.rs +185 -0
- package/tests/yamllint_compat_octal_values.rs +172 -0
- package/tests/yamllint_compat_quoted_strings.rs +154 -0
- package/tests/yamllint_compat_syntax.rs +200 -0
- package/tests/yamllint_compat_trailing_spaces.rs +162 -0
- package/tests/yamllint_compat_truthy.rs +130 -0
- package/tests/yamllint_compat_yaml_files.rs +81 -0
- package/typos.toml +2 -0
package/src/decoder.rs
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
use std::char;
|
|
2
|
+
use std::env;
|
|
3
|
+
use std::path::Path;
|
|
4
|
+
|
|
5
|
+
use encoding_rs::Encoding;
|
|
6
|
+
|
|
7
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
8
|
+
enum Endian {
|
|
9
|
+
Big,
|
|
10
|
+
Little,
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
#[derive(Clone, Copy)]
|
|
14
|
+
enum EncodingKind {
|
|
15
|
+
Utf8,
|
|
16
|
+
Utf8WithBom,
|
|
17
|
+
Utf16 { endian: Endian, skip_bom: bool },
|
|
18
|
+
Utf32 { endian: Endian, skip_bom: bool },
|
|
19
|
+
Latin1,
|
|
20
|
+
Custom(&'static Encoding),
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
fn normalize_label(label: &str) -> String {
|
|
24
|
+
label.trim().to_ascii_lowercase().replace('_', "-")
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fn decode_error(kind: &str, detail: impl Into<String>) -> String {
|
|
28
|
+
let detail = detail.into();
|
|
29
|
+
format!("invalid {kind}: {detail}")
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fn parse_override(bytes: &[u8], label: &str) -> Result<EncodingKind, String> {
|
|
33
|
+
let normalized = normalize_label(label);
|
|
34
|
+
if normalized.is_empty() {
|
|
35
|
+
return Err(decode_error(
|
|
36
|
+
"encoding",
|
|
37
|
+
"YAMLLINT_FILE_ENCODING cannot be empty",
|
|
38
|
+
));
|
|
39
|
+
}
|
|
40
|
+
match normalized.as_str() {
|
|
41
|
+
"utf-8" => Ok(EncodingKind::Utf8),
|
|
42
|
+
"utf-8-sig" | "utf8-sig" => Ok(EncodingKind::Utf8WithBom),
|
|
43
|
+
"utf-16" => Ok(EncodingKind::Utf16 {
|
|
44
|
+
endian: detect_utf16_endian(bytes).unwrap_or(Endian::Little),
|
|
45
|
+
skip_bom: bytes.starts_with(&[0xFE, 0xFF])
|
|
46
|
+
|| bytes.starts_with(&[0xFF, 0xFE]),
|
|
47
|
+
}),
|
|
48
|
+
"utf-16le" | "utf-16-le" | "utf16le" => Ok(EncodingKind::Utf16 {
|
|
49
|
+
endian: Endian::Little,
|
|
50
|
+
skip_bom: false,
|
|
51
|
+
}),
|
|
52
|
+
"utf-16be" | "utf-16-be" | "utf16be" => Ok(EncodingKind::Utf16 {
|
|
53
|
+
endian: Endian::Big,
|
|
54
|
+
skip_bom: false,
|
|
55
|
+
}),
|
|
56
|
+
"utf-32" => Ok(EncodingKind::Utf32 {
|
|
57
|
+
endian: detect_utf32_endian(bytes).unwrap_or(Endian::Little),
|
|
58
|
+
skip_bom: bytes.starts_with(&[0x00, 0x00, 0xFE, 0xFF])
|
|
59
|
+
|| bytes.starts_with(&[0xFF, 0xFE, 0x00, 0x00]),
|
|
60
|
+
}),
|
|
61
|
+
"utf-32le" | "utf-32-le" | "utf32le" => Ok(EncodingKind::Utf32 {
|
|
62
|
+
endian: Endian::Little,
|
|
63
|
+
skip_bom: false,
|
|
64
|
+
}),
|
|
65
|
+
"utf-32be" | "utf-32-be" | "utf32be" => Ok(EncodingKind::Utf32 {
|
|
66
|
+
endian: Endian::Big,
|
|
67
|
+
skip_bom: false,
|
|
68
|
+
}),
|
|
69
|
+
"latin-1" | "latin1" | "iso-8859-1" | "iso8859-1" => Ok(EncodingKind::Latin1),
|
|
70
|
+
other => Encoding::for_label(other.as_bytes())
|
|
71
|
+
.map(EncodingKind::Custom)
|
|
72
|
+
.ok_or_else(|| {
|
|
73
|
+
decode_error("encoding", format!("unsupported label '{label}'"))
|
|
74
|
+
}),
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
fn detect_utf16_endian(bytes: &[u8]) -> Option<Endian> {
|
|
79
|
+
if bytes.starts_with(&[0xFE, 0xFF]) {
|
|
80
|
+
Some(Endian::Big)
|
|
81
|
+
} else if bytes.starts_with(&[0xFF, 0xFE]) {
|
|
82
|
+
Some(Endian::Little)
|
|
83
|
+
} else if bytes.len() >= 2 && bytes[0] == 0x00 {
|
|
84
|
+
Some(Endian::Big)
|
|
85
|
+
} else if bytes.len() >= 2 && bytes[1] == 0x00 {
|
|
86
|
+
Some(Endian::Little)
|
|
87
|
+
} else {
|
|
88
|
+
None
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
fn detect_utf32_endian(bytes: &[u8]) -> Option<Endian> {
|
|
93
|
+
if bytes.starts_with(&[0x00, 0x00, 0xFE, 0xFF]) {
|
|
94
|
+
Some(Endian::Big)
|
|
95
|
+
} else if bytes.starts_with(&[0xFF, 0xFE, 0x00, 0x00]) {
|
|
96
|
+
Some(Endian::Little)
|
|
97
|
+
} else if bytes.len() >= 4 && bytes[0..3] == [0x00, 0x00, 0x00] {
|
|
98
|
+
Some(Endian::Big)
|
|
99
|
+
} else if bytes.len() >= 4 && bytes[1..4] == [0x00, 0x00, 0x00] {
|
|
100
|
+
Some(Endian::Little)
|
|
101
|
+
} else {
|
|
102
|
+
None
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
fn detect_encoding(bytes: &[u8]) -> Result<EncodingKind, String> {
|
|
107
|
+
let override_label = env::var("YAMLLINT_FILE_ENCODING").map_or(None, |value| {
|
|
108
|
+
eprintln!(
|
|
109
|
+
"YAMLLINT_FILE_ENCODING is meant for temporary workarounds. It may be removed in a future version of yamllint."
|
|
110
|
+
);
|
|
111
|
+
Some(value)
|
|
112
|
+
});
|
|
113
|
+
detect_encoding_with_override(bytes, override_label.as_deref())
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
fn detect_encoding_with_override(
|
|
117
|
+
bytes: &[u8],
|
|
118
|
+
override_label: Option<&str>,
|
|
119
|
+
) -> Result<EncodingKind, String> {
|
|
120
|
+
if let Some(label) = override_label {
|
|
121
|
+
return parse_override(bytes, label);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if bytes.starts_with(&[0x00, 0x00, 0xFE, 0xFF]) {
|
|
125
|
+
return Ok(EncodingKind::Utf32 {
|
|
126
|
+
endian: Endian::Big,
|
|
127
|
+
skip_bom: true,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if bytes.len() >= 4 && bytes[0..3] == [0x00, 0x00, 0x00] {
|
|
131
|
+
return Ok(EncodingKind::Utf32 {
|
|
132
|
+
endian: Endian::Big,
|
|
133
|
+
skip_bom: false,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
if bytes.starts_with(&[0xFF, 0xFE, 0x00, 0x00]) {
|
|
137
|
+
return Ok(EncodingKind::Utf32 {
|
|
138
|
+
endian: Endian::Little,
|
|
139
|
+
skip_bom: true,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
if bytes.len() >= 4 && bytes[1..4] == [0x00, 0x00, 0x00] {
|
|
143
|
+
return Ok(EncodingKind::Utf32 {
|
|
144
|
+
endian: Endian::Little,
|
|
145
|
+
skip_bom: false,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
if bytes.starts_with(&[0xFE, 0xFF]) {
|
|
149
|
+
return Ok(EncodingKind::Utf16 {
|
|
150
|
+
endian: Endian::Big,
|
|
151
|
+
skip_bom: true,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
if bytes.len() >= 2 && bytes[0] == 0x00 {
|
|
155
|
+
return Ok(EncodingKind::Utf16 {
|
|
156
|
+
endian: Endian::Big,
|
|
157
|
+
skip_bom: false,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
if bytes.starts_with(&[0xFF, 0xFE]) {
|
|
161
|
+
return Ok(EncodingKind::Utf16 {
|
|
162
|
+
endian: Endian::Little,
|
|
163
|
+
skip_bom: true,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
if bytes.len() >= 2 && bytes[1] == 0x00 {
|
|
167
|
+
return Ok(EncodingKind::Utf16 {
|
|
168
|
+
endian: Endian::Little,
|
|
169
|
+
skip_bom: false,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
if bytes.starts_with(&[0xEF, 0xBB, 0xBF]) {
|
|
173
|
+
return Ok(EncodingKind::Utf8WithBom);
|
|
174
|
+
}
|
|
175
|
+
Ok(EncodingKind::Utf8)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
fn decode_utf8(bytes: &[u8]) -> Result<String, String> {
|
|
179
|
+
std::str::from_utf8(bytes)
|
|
180
|
+
.map(str::to_owned)
|
|
181
|
+
.map_err(|err| decode_error("utf-8 data", err.to_string()))
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
fn decode_utf8_bom(bytes: &[u8]) -> Result<String, String> {
|
|
185
|
+
let sliced = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes);
|
|
186
|
+
decode_utf8(sliced)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
fn decode_utf16(
|
|
190
|
+
bytes: &[u8],
|
|
191
|
+
endian: Endian,
|
|
192
|
+
skip_bom: bool,
|
|
193
|
+
) -> Result<String, String> {
|
|
194
|
+
if bytes.is_empty() {
|
|
195
|
+
return Ok(String::new());
|
|
196
|
+
}
|
|
197
|
+
let data = if skip_bom {
|
|
198
|
+
bytes.get(2..).unwrap_or(&[])
|
|
199
|
+
} else {
|
|
200
|
+
bytes
|
|
201
|
+
};
|
|
202
|
+
if data.len() % 2 != 0 {
|
|
203
|
+
return Err(decode_error(
|
|
204
|
+
"utf-16 data",
|
|
205
|
+
format!("length {} is not even", data.len()),
|
|
206
|
+
));
|
|
207
|
+
}
|
|
208
|
+
let mut units: Vec<u16> = Vec::with_capacity(data.len() / 2);
|
|
209
|
+
for chunk in data.chunks_exact(2) {
|
|
210
|
+
let value = match endian {
|
|
211
|
+
Endian::Big => u16::from_be_bytes([chunk[0], chunk[1]]),
|
|
212
|
+
Endian::Little => u16::from_le_bytes([chunk[0], chunk[1]]),
|
|
213
|
+
};
|
|
214
|
+
units.push(value);
|
|
215
|
+
}
|
|
216
|
+
String::from_utf16(&units)
|
|
217
|
+
.map_err(|err| decode_error("utf-16 data", err.to_string()))
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
fn decode_utf32(
|
|
221
|
+
bytes: &[u8],
|
|
222
|
+
endian: Endian,
|
|
223
|
+
skip_bom: bool,
|
|
224
|
+
) -> Result<String, String> {
|
|
225
|
+
if bytes.is_empty() {
|
|
226
|
+
return Ok(String::new());
|
|
227
|
+
}
|
|
228
|
+
let data = if skip_bom {
|
|
229
|
+
bytes.get(4..).unwrap_or(&[])
|
|
230
|
+
} else {
|
|
231
|
+
bytes
|
|
232
|
+
};
|
|
233
|
+
if data.len() % 4 != 0 {
|
|
234
|
+
return Err(decode_error(
|
|
235
|
+
"utf-32 data",
|
|
236
|
+
format!("length {} is not divisible by 4", data.len()),
|
|
237
|
+
));
|
|
238
|
+
}
|
|
239
|
+
let mut out = String::with_capacity(data.len() / 4);
|
|
240
|
+
for chunk in data.chunks_exact(4) {
|
|
241
|
+
let raw = match endian {
|
|
242
|
+
Endian::Big => u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]),
|
|
243
|
+
Endian::Little => {
|
|
244
|
+
u32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]])
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
match char::from_u32(raw) {
|
|
248
|
+
Some(ch) => out.push(ch),
|
|
249
|
+
None => {
|
|
250
|
+
return Err(decode_error(
|
|
251
|
+
"utf-32 data",
|
|
252
|
+
format!("invalid scalar value 0x{raw:08X}"),
|
|
253
|
+
));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
Ok(out)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
fn decode_latin1(bytes: &[u8]) -> String {
|
|
261
|
+
bytes
|
|
262
|
+
.iter()
|
|
263
|
+
.map(|&b| char::from_u32(u32::from(b)).unwrap())
|
|
264
|
+
.collect()
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
fn decode_with_custom(
|
|
268
|
+
bytes: &[u8],
|
|
269
|
+
encoding: &'static Encoding,
|
|
270
|
+
) -> Result<String, String> {
|
|
271
|
+
let (text, _encoding_used, had_errors) = encoding.decode(bytes);
|
|
272
|
+
if had_errors {
|
|
273
|
+
return Err(decode_error(
|
|
274
|
+
encoding.name(),
|
|
275
|
+
"decode error with replacement required",
|
|
276
|
+
));
|
|
277
|
+
}
|
|
278
|
+
Ok(text.into_owned())
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
fn decode_with_kind(bytes: &[u8], encoding: EncodingKind) -> Result<String, String> {
|
|
282
|
+
match encoding {
|
|
283
|
+
EncodingKind::Utf8 => decode_utf8(bytes),
|
|
284
|
+
EncodingKind::Utf8WithBom => decode_utf8_bom(bytes),
|
|
285
|
+
EncodingKind::Utf16 { endian, skip_bom } => {
|
|
286
|
+
decode_utf16(bytes, endian, skip_bom)
|
|
287
|
+
}
|
|
288
|
+
EncodingKind::Utf32 { endian, skip_bom } => {
|
|
289
|
+
decode_utf32(bytes, endian, skip_bom)
|
|
290
|
+
}
|
|
291
|
+
EncodingKind::Latin1 => Ok(decode_latin1(bytes)),
|
|
292
|
+
EncodingKind::Custom(enc) => decode_with_custom(bytes, enc),
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/// Decode raw bytes using yamllint-compatible encoding detection.
|
|
297
|
+
///
|
|
298
|
+
/// # Errors
|
|
299
|
+
/// Returns an error string describing why decoding failed.
|
|
300
|
+
pub fn decode_bytes(bytes: &[u8]) -> Result<String, String> {
|
|
301
|
+
let encoding = detect_encoding(bytes)?;
|
|
302
|
+
decode_with_kind(bytes, encoding)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/// Decode bytes using an explicit encoding override, bypassing environment lookups.
|
|
306
|
+
///
|
|
307
|
+
/// # Errors
|
|
308
|
+
/// Returns an error string when the override label is unsupported or decoding fails.
|
|
309
|
+
pub fn decode_bytes_with_override(
|
|
310
|
+
bytes: &[u8],
|
|
311
|
+
override_label: Option<&str>,
|
|
312
|
+
) -> Result<String, String> {
|
|
313
|
+
let encoding = detect_encoding_with_override(bytes, override_label)?;
|
|
314
|
+
decode_with_kind(bytes, encoding)
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/// Read a file from disk and decode it using yamllint-compatible detection.
|
|
318
|
+
///
|
|
319
|
+
/// # Errors
|
|
320
|
+
/// Returns an error string when the file cannot be read or decoded.
|
|
321
|
+
pub fn read_file(path: &Path) -> Result<String, String> {
|
|
322
|
+
let data = std::fs::read(path)
|
|
323
|
+
.map_err(|err| format!("failed to read {}: {err}", path.display()))?;
|
|
324
|
+
decode_bytes(&data)
|
|
325
|
+
.map_err(|err| format!("failed to read {}: {err}", path.display()))
|
|
326
|
+
}
|
package/src/discover.rs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
use std::ffi::OsStr;
|
|
2
|
+
use std::path::{Path, PathBuf};
|
|
3
|
+
|
|
4
|
+
use ignore::WalkBuilder;
|
|
5
|
+
|
|
6
|
+
pub fn is_yaml_path(path: &Path) -> bool {
|
|
7
|
+
path.extension().and_then(OsStr::to_str).is_some_and(|ext| {
|
|
8
|
+
ext.eq_ignore_ascii_case("yml") || ext.eq_ignore_ascii_case("yaml")
|
|
9
|
+
})
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
#[must_use]
|
|
13
|
+
pub fn gather_yaml_from_dir(dir: &Path) -> Vec<PathBuf> {
|
|
14
|
+
let mut files = Vec::new();
|
|
15
|
+
let walker = WalkBuilder::new(dir)
|
|
16
|
+
.hidden(false)
|
|
17
|
+
.ignore(true)
|
|
18
|
+
.git_ignore(true)
|
|
19
|
+
.git_global(true)
|
|
20
|
+
.git_exclude(true)
|
|
21
|
+
.follow_links(false)
|
|
22
|
+
.build();
|
|
23
|
+
|
|
24
|
+
for entry in walker.flatten() {
|
|
25
|
+
let p = entry.path();
|
|
26
|
+
if p.is_file() && is_yaml_path(p) {
|
|
27
|
+
files.push(p.to_path_buf());
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
files
|
|
31
|
+
}
|
package/src/lib.rs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#![forbid(unsafe_code)]
|
|
2
|
+
#![deny(
|
|
3
|
+
clippy::all,
|
|
4
|
+
clippy::pedantic,
|
|
5
|
+
clippy::cargo,
|
|
6
|
+
clippy::cognitive_complexity
|
|
7
|
+
)]
|
|
8
|
+
|
|
9
|
+
pub mod cli_support;
|
|
10
|
+
pub mod conf;
|
|
11
|
+
pub mod config;
|
|
12
|
+
pub mod decoder;
|
|
13
|
+
pub mod discover;
|
|
14
|
+
pub mod lint;
|
|
15
|
+
pub mod migrate;
|
|
16
|
+
pub mod rules;
|
|
17
|
+
|
|
18
|
+
pub use discover::{gather_yaml_from_dir, is_yaml_path};
|
|
19
|
+
pub use lint::{LintProblem, Severity, lint_file};
|