From c785552e71d4f6bb23f6be36442d9590a504876a Mon Sep 17 00:00:00 2001 From: Jonathon Belotti Date: Sat, 19 Oct 2019 18:32:09 +1100 Subject: [PATCH] Fix https://github.com/thundergolfer/goodreads-sh/issues/2 If a book has a description less than MAX_DESC_LEN, which is rare, the program will panic --- src/models.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/models.rs b/src/models.rs index 5835cd2..223f8e1 100644 --- a/src/models.rs +++ b/src/models.rs @@ -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) + } + } } }