From 46edc57b24558ee62c095482c49f0504860bac97 Mon Sep 17 00:00:00 2001 From: James Campos Date: Mon, 13 Jul 2020 22:02:08 -0700 Subject: use anyhow in epub --- src/epub.rs | 19 ++++++++++--------- src/main.rs | 5 ++++- 2 files changed, 14 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/epub.rs b/src/epub.rs index fb0445a..8915214 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -1,3 +1,4 @@ +use anyhow::Result; use crossterm::style::Attribute; use roxmltree::{Document, Node}; use std::{collections::HashMap, fs::File, io::Read}; @@ -11,14 +12,14 @@ pub struct Epub { } impl Epub { - pub fn new(path: &str, meta: bool) -> std::io::Result { + pub fn new(path: &str, meta: bool) -> Result { let file = File::open(path)?; let mut epub = Epub { container: zip::ZipArchive::new(file)?, chapters: Vec::new(), meta: String::new(), }; - let chapters = epub.get_rootfile(); + let chapters = epub.get_rootfile()?; if !meta { epub.get_chapters(chapters); } @@ -53,9 +54,9 @@ impl Epub { }) .collect(); } - fn get_rootfile(&mut self) -> Vec<(String, String)> { + fn get_rootfile(&mut self) -> Result> { let xml = self.get_text("META-INF/container.xml"); - let doc = Document::parse(&xml).unwrap(); + let doc = Document::parse(&xml)?; let path = doc .descendants() .find(|n| n.has_tag_name("rootfile")) @@ -63,7 +64,7 @@ impl Epub { .attribute("full-path") .unwrap(); let xml = self.get_text(path); - let doc = Document::parse(&xml).unwrap(); + let doc = Document::parse(&xml)?; // zip expects unix path even on windows let rootdir = match path.rfind('/') { @@ -101,16 +102,16 @@ impl Epub { .attribute("href") .unwrap(); let xml = self.get_text(&format!("{}{}", rootdir, path)); - let doc = Document::parse(&xml).unwrap(); + let doc = Document::parse(&xml)?; epub3(doc, &mut nav); } else { let toc = spine_node.attribute("toc").unwrap_or("ncx"); let path = manifest.get(toc).unwrap(); let xml = self.get_text(&format!("{}{}", rootdir, path)); - let doc = Document::parse(&xml).unwrap(); + let doc = Document::parse(&xml)?; epub2(doc, &mut nav); } - spine_node + Ok(spine_node .children() .filter(Node::is_element) .enumerate() @@ -121,7 +122,7 @@ impl Epub { let path = format!("{}{}", rootdir, path); (label, path) }) - .collect() + .collect()) } } diff --git a/src/main.rs b/src/main.rs index d2c965f..30abdec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -758,7 +758,10 @@ fn main() { exit(0); } let mut bk = Bk::new(epub, state.bk); - bk.run().unwrap(); + bk.run().unwrap_or_else(|e| { + println!("run error: {}", e); + exit(1); + }); let byte = bk.chap().lines[bk.line].0; state -- cgit v1.2.3