Skip to content

Commit

Permalink
use anyhow in epub
Browse files Browse the repository at this point in the history
  • Loading branch information
aeosynth committed Jul 14, 2020
1 parent ce2e48d commit 46edc57
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
19 changes: 10 additions & 9 deletions src/epub.rs
Original file line number Diff line number Diff line change
@@ -1,3 1,4 @@
use anyhow::Result;
use crossterm::style::Attribute;
use roxmltree::{Document, Node};
use std::{collections::HashMap, fs::File, io::Read};
Expand All @@ -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);
}
Expand Down Expand Up @@ -53,17 54,17 @@ 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"))
.unwrap()
.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('/') {
Expand Down Expand Up @@ -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()
Expand All @@ -121,7 122,7 @@ impl Epub {
let path = format!("{}{}", rootdir, path);
(label, path)
})
.collect()
.collect())
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 46edc57

Please sign in to comment.