Skip to content

Commit 38442da

Browse files
authored
Remove a couple of possible panics when opening a post. (#1132)
I'm not actually so bothered about the panics per se, but this way a naïve user like myself who accidentally created a zero length file in `posts` gets a useful error message.
1 parent 7505a5b commit 38442da

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/posts.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,15 @@ impl Post {
5050
let filename = split.next().unwrap().to_string();
5151

5252
let contents = std::fs::read_to_string(path)?;
53+
if contents.len() < 5 {
54+
return Err(
55+
format!("{path:?} is empty, or too short to have valid front matter").into(),
56+
);
57+
}
5358

5459
// yaml headers.... we know the first four bytes of each file are "---\n"
5560
// so we need to find the end. we need the fours to adjust for those first bytes
56-
let end_of_yaml = contents[4..].find("---").unwrap() + 4;
61+
let end_of_yaml = contents[4..].find("---\n").unwrap() + 4;
5762
let yaml = &contents[..end_of_yaml];
5863
let YamlHeader {
5964
author,

0 commit comments

Comments
 (0)