From 9809dc99acd2ff65358b7bbe3c1be6c303f558b7 Mon Sep 17 00:00:00 2001 From: James Campos Date: Sun, 28 Mar 2021 01:35:32 -0700 Subject: remove anyhow doesn't work on None --- src/epub.rs | 17 ++++++++++------- src/main.rs | 53 ++++++++++++++++++++++------------------------------- 2 files changed, 32 insertions(+), 38 deletions(-) (limited to 'src') 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 { + pub fn new(path: &str, meta: bool) -> io::Result { 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> { + 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 { +fn init() -> Result> { let save_path = if cfg!(windows) { format!("{}\\bk", env::var("APPDATA")?) } else { @@ -290,46 +289,38 @@ fn init() -> Result { }; // 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 = 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, -- cgit v1.2.3