Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
Fix #2
Browse files Browse the repository at this point in the history
If a book has a description less than MAX_DESC_LEN, which is rare, the program will panic
  • Loading branch information
thundergolfer-two committed Oct 19, 2019
1 parent d17b9b6 commit c785552
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,14 @@ pub struct Author {

impl Display for Book {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let shortened_desc = &self.description[..MAX_DESC_LEN];
write!(f, "{}: {}...", self.title, shortened_desc)
match self.description.len() {
0 => write!(f, "{}", self.title),
1..=MAX_DESC_LEN => write!(f, "{}: {}...", self.title, self.description),
_ => {
let shortened_desc = &self.description[..MAX_DESC_LEN];
write!(f, "{}: {}...", self.title, shortened_desc)
}
}
}
}

Expand Down

0 comments on commit c785552

Please sign in to comment.