diff options
author | James Campos <james.r.campos@gmail.com> | 2020-07-13 22:02:08 -0700 |
---|---|---|
committer | James Campos <james.r.campos@gmail.com> | 2020-07-13 22:02:08 -0700 |
commit | 46edc57b24558ee62c095482c49f0504860bac97 (patch) | |
tree | 3ed86944058734da7651b3d0a3db89d5d7671098 /src | |
parent | ce2e48d7a39fecf0e5c3ea0e16a4fa2de535518b (diff) | |
download | bk-46edc57b24558ee62c095482c49f0504860bac97.tar.gz |
use anyhow in epub
Diffstat (limited to 'src')
-rw-r--r-- | src/epub.rs | 19 | ||||
-rw-r--r-- | src/main.rs | 5 |
2 files changed, 14 insertions, 10 deletions
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<Self> { + pub fn new(path: &str, meta: bool) -> Result<Self> { 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<Vec<(String, String)>> { 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 |