aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJames Campos <james.r.campos@gmail.com>2021-03-28 01:35:32 -0700
committerJames Campos <james.r.campos@gmail.com>2021-03-28 01:35:32 -0700
commit9809dc99acd2ff65358b7bbe3c1be6c303f558b7 (patch)
tree0f84c42cf6d7607ffca344d83109957166b5a9de /src
parentb79c05312df99115d475b69eabd5961e8a4e485f (diff)
downloadbk-9809dc99acd2ff65358b7bbe3c1be6c303f558b7.tar.gz
remove anyhow
doesn't work on None
Diffstat (limited to 'src')
-rw-r--r--src/epub.rs17
-rw-r--r--src/main.rs53
2 files changed, 32 insertions, 38 deletions
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,