I think we can make this more idiomatic Rust by:
- using regex to match the name instead of string manipulations, repeating parts of the path and the file's structure (extension + dot);
- match a slice of the files to return a
Result;
- extract intermediary results for clarity;
- we could like use fs::read_dir("doc/release-notes") to iterate the folder, but I see that delegating to
git is common here:
fn lint_doc_release_note_snippets() -> LintResult {
let notes = check_output(git().args(["ls-files", "--", "doc/release-notes"]))?;
let snippet = Regex::new(r"release-notes-[^.]+\.md$").unwrap();
let snippet_notes = notes.lines().filter(|f| snippet.is_match(f)).collect::<Vec<_>>();
match snippet_notes.as_slice() {
[] => Ok(()),
_ => Err(format!(r#"
{}
^^^
Release note snippets must be put into the doc/ folder directly.
The doc/release-notes/ folder is for archived release notes of previous releases only.
"#, snippet_notes.join("\n")))
}
}
Note that this requires adding:
[dependencies]
regex = "1.10.6"
to the Cargo.toml file.
Also note that the snippet regex could be pulled out of the method.