From 6b2572e78d14fdeebe06812f671016d8aa5fe95c Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Sat, 31 Dec 2016 18:41:59 -0800 Subject: [PATCH] Simplify some as_str error handling code --- src/renderer/html_handlebars/hbs_renderer.rs | 30 ++++++-------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index bcb55522..84be4f51 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -82,15 +82,9 @@ impl Renderer for HtmlHandlebars { print_content.push_str(&content); // Update the context with data for this file - match ch.path.to_str() { - Some(p) => { - data.insert("path".to_owned(), p.to_json()); - }, - None => { - return Err(Box::new(io::Error::new(io::ErrorKind::Other, - "Could not convert path to str"))) - }, - } + let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other, + "Could not convert path to str"))?; + data.insert("path".to_owned(), path.to_json()); data.insert("content".to_owned(), content.to_json()); data.insert("chapter_title".to_owned(), ch.name.to_json()); data.insert("path_to_root".to_owned(), utils::fs::path_to_root(&ch.path).to_json()); @@ -285,22 +279,16 @@ fn make_data(book: &MDBook) -> Result match *item { BookItem::Affix(ref ch) => { chapter.insert("name".to_owned(), ch.name.to_json()); - match ch.path.to_str() { - Some(p) => { - chapter.insert("path".to_owned(), p.to_json()); - }, - None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))), - } + let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other, + "Could not convert path to str"))?; + chapter.insert("path".to_owned(), path.to_json()); }, BookItem::Chapter(ref s, ref ch) => { chapter.insert("section".to_owned(), s.to_json()); chapter.insert("name".to_owned(), ch.name.to_json()); - match ch.path.to_str() { - Some(p) => { - chapter.insert("path".to_owned(), p.to_json()); - }, - None => return Err(Box::new(io::Error::new(io::ErrorKind::Other, "Could not convert path to str"))), - } + let path = ch.path.to_str().ok_or(io::Error::new(io::ErrorKind::Other, + "Could not convert path to str"))?; + chapter.insert("path".to_owned(), path.to_json()); }, BookItem::Spacer => { chapter.insert("spacer".to_owned(), "_spacer_".to_json());