Skip to content

Commit

Permalink
Remove a couple of possible panics when opening a post. (#1132)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LawnGnome authored Oct 24, 2023
1 parent 7505a5b commit 38442da
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,15 @@ impl Post {
let filename = split.next().unwrap().to_string();

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

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

0 comments on commit 38442da

Please sign in to comment.