Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/+easystack-unicode-line.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Point a duplicate easystack key at the repeated line when a Unicode line break separates it from the first.
65 changes: 64 additions & 1 deletion src/easystack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,36 @@ fn yaml_diagnostic(text: &str, error: serde_yaml::Error) -> Diagnostic {
Diagnostic { message, location }
}

/// Lines as libyaml counts them.
///
/// A break is LF, CRLF, a bare CR, U+0085, U+2028 or U+2029. `str::lines`
/// does not split on the last four, which would leave a repeated key
/// reported on the first key.
fn yaml_lines(text: &str) -> Vec<&str> {
let mut lines = Vec::new();
let mut start = 0;
let mut chars = text.char_indices().peekable();
while let Some((index, ch)) = chars.next() {
let break_len = match ch {
'\r' if chars.peek().is_some_and(|(_, next)| *next == '\n') => {
chars.next();
2
}
'\n' | '\r' | '\u{0085}' | '\u{2028}' | '\u{2029}' => ch.len_utf8(),
_ => 0,
};
if break_len == 0 {
continue;
}
lines.push(&text[start..index]);
start = index + break_len;
}
if start < text.len() {
lines.push(&text[start..]);
}
lines
}

fn duplicate_key_location(text: &str, message: &str) -> Option<(usize, usize)> {
let encoded = message
.rsplit(": ")
Expand All @@ -114,7 +144,7 @@ fn duplicate_key_location(text: &str, message: &str) -> Option<(usize, usize)> {
let key: String = serde_json::from_str(encoded).ok()?;
let mut scopes = Vec::<(usize, usize)>::new();
let mut seen = std::collections::BTreeSet::new();
for (index, line) in text.lines().enumerate() {
for (index, line) in yaml_lines(text).into_iter().enumerate() {
let line = line.trim_end_matches('\r');
let indent = line.len() - line.trim_start_matches(' ').len();
let content = &line[indent..];
Expand Down Expand Up @@ -683,4 +713,37 @@ easyconfigs:
assert_eq!(is_easystack(Path::new(p)), want, "{p}");
}
}

#[test]
#[allow(
clippy::disallowed_methods,
reason = "this seeds a temporary easystack, not a user-visible output path"
)]
fn a_duplicate_key_after_a_unicode_line_break_points_at_the_repeat() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("input.yml");
// An empty YAML line sits between the two keys when the gap is two breaks.
for (gap, line) in [
("\u{2028}", 5),
("\u{2029}", 5),
("\u{0085}", 5),
("\r", 5),
("\u{2028}\u{2028}", 6),
] {
let text = format!(
"easyconfigs:\n - x.eb:\n options:\n rebuild: true{gap} rebuild: false\n"
);
std::fs::write(&path, &text).unwrap();
let err = read(&path).unwrap_err();
assert_eq!(
err,
format!(
"easystack {}:{}:9: easyconfigs[0].x.eb.options: duplicate entry with key \"rebuild\"",
path.display(),
line
),
"{gap:?}"
);
}
}
}
Loading