diff options
author | James Campos <james.r.campos@gmail.com> | 2021-03-28 01:35:32 -0700 |
---|---|---|
committer | James Campos <james.r.campos@gmail.com> | 2021-03-28 01:35:32 -0700 |
commit | 9809dc99acd2ff65358b7bbe3c1be6c303f558b7 (patch) | |
tree | 0f84c42cf6d7607ffca344d83109957166b5a9de | |
parent | b79c05312df99115d475b69eabd5961e8a4e485f (diff) | |
download | bk-9809dc99acd2ff65358b7bbe3c1be6c303f558b7.tar.gz |
remove anyhow
doesn't work on None
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/epub.rs | 17 | ||||
-rw-r--r-- | src/main.rs | 53 |
4 files changed, 32 insertions, 46 deletions
@@ -7,12 +7,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aae1277d39aeec15cb388266ecc24b11c80469deae6067e17a1a7aa9e5c1f234" [[package]] -name = "anyhow" -version = "1.0.39" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81cddc5f91628367664cc7c69714ff08deee8a3efc54623011c772544d7b2767" - -[[package]] name = "argh" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -57,7 +51,6 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" name = "bk" version = "0.5.3" dependencies = [ - "anyhow", "argh", "crossterm", "ron", @@ -11,7 +11,6 @@ readme = "README.md" repository = "https://github.com/aeosynth/bk" [dependencies] -anyhow = "^1.0" argh = "^0.1" crossterm = "^0.19" ron = "^0.6" diff --git a/src/epub.rs b/src/epub.rs index 66d1077..a52c9d0 100644 --- a/src/epub.rs +++ b/src/epub.rs @@ -1,7 +1,10 @@ -use anyhow::Result; use crossterm::style::{Attribute, Attributes}; use roxmltree::{Document, Node, ParsingOptions}; -use std::{collections::HashMap, fs::File, io::Read}; +use std::{ + collections::HashMap, + fs::File, + io::{self, Read}, +}; pub struct Chapter { pub title: String, @@ -24,7 +27,7 @@ pub struct Epub { } impl Epub { - pub fn new(path: &str, meta: bool) -> Result<Self> { + pub fn new(path: &str, meta: bool) -> io::Result<Self> { let file = File::open(path)?; let mut epub = Epub { container: zip::ZipArchive::new(file)?, @@ -33,7 +36,7 @@ impl Epub { links: HashMap::new(), meta: String::new(), }; - let chapters = epub.get_spine()?; + let chapters = epub.get_spine(); if !meta { epub.get_chapters(chapters); } @@ -85,7 +88,7 @@ impl Epub { self.chapters.push(c); } } - fn get_spine(&mut self) -> Result<Vec<(String, String)>> { + fn get_spine(&mut self) -> Vec<(String, String)> { let xml = self.get_text("META-INF/container.xml"); let doc = Document::parse(&xml).unwrap(); let path = doc @@ -141,7 +144,7 @@ impl Epub { let doc = Document::parse(&xml).unwrap(); epub2(doc, &mut nav); } - Ok(spine_node + spine_node .children() .filter(Node::is_element) .enumerate() @@ -151,7 +154,7 @@ impl Epub { let label = nav.remove(path).unwrap_or_else(|| i.to_string()); (label, path.to_string()) }) - .collect()) + .collect() } } diff --git a/src/main.rs b/src/main.rs index f2ba392..fefce50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use anyhow::Result; use crossterm::{ cursor, event::{DisableMouseCapture, EnableMouseCapture, Event}, @@ -11,7 +10,7 @@ use std::{ cmp::min, collections::HashMap, env, fs, - io::{stdout, Write}, + io::{self, Write}, process::exit, }; use unicode_width::UnicodeWidthChar; @@ -160,7 +159,7 @@ impl Bk<'_> { bk } fn run(&mut self) -> crossterm::Result<()> { - let mut stdout = stdout(); + let mut stdout = io::stdout(); queue!( stdout, terminal::EnterAlternateScreen, @@ -282,7 +281,7 @@ struct State { bk: Props, } -fn init() -> Result<State> { +fn init() -> Result<State, Box<dyn std::error::Error>> { let save_path = if cfg!(windows) { format!("{}\\bk", env::var("APPDATA")?) } else { @@ -290,46 +289,38 @@ fn init() -> Result<State> { }; // XXX will silently create a new default save if ron errors but path arg works. // revisit if/when stabilizing. ez file format upgrades - let save = fs::read_to_string(&save_path) - .map_err(anyhow::Error::new) - .and_then(|s| { - let save: Save = ron::from_str(&s)?; - Ok(save) - }); + let save: io::Result<Save> = fs::read_to_string(&save_path).and_then(|s| { + ron::from_str(&s) + .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "invalid save file")) + }); let args: Args = argh::from_env(); let mut path = args.path; - // abort on path error - if path.is_some() { - path = Some( - fs::canonicalize(path.unwrap())? - .to_str() - .unwrap() - .to_string(), - ); + if let Some(p) = path { + path = Some(fs::canonicalize(p)?.to_str().unwrap().to_string()); } - let (path, chapter, byte) = match (&save, &path) { - (Err(_), None) => return Err(anyhow::anyhow!("no path arg and no valid save file")), - (Err(_), Some(p)) => (p, 0, 0), - (Ok(save), None) => { - let &(chapter, byte) = save.files.get(&save.last).unwrap(); - (&save.last, chapter, byte) + let (path, save, chapter, byte) = match (save, path) { + (Err(e), None) => return Err(Box::new(e)), + (Err(_), Some(p)) => (p, Save::default(), 0, 0), + (Ok(s), None) => { + let &(chapter, byte) = s.files.get(&s.last).unwrap(); + (s.last.clone(), s, chapter, byte) } - (Ok(save), Some(p)) => { - if save.files.contains_key(p) { - let &(chapter, byte) = save.files.get(p).unwrap(); - (p, chapter, byte) + (Ok(s), Some(p)) => { + if s.files.contains_key(&p) { + let &(chapter, byte) = s.files.get(&p).unwrap(); + (p, s, chapter, byte) } else { - (p, 0, 0) + (p, s, 0, 0) } } }; Ok(State { + path, + save, save_path, - path: path.clone(), - save: save.unwrap_or_default(), meta: args.meta, bk: Props { chapter, |