aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan2017-07-27 13:40:41 -0400
committerNathan2017-07-27 13:40:41 -0400
commite8ee8223a87cb8b730905808162435e5b716b59b (patch)
tree519a82772fe0c1841c2f5c22e739c9dff5675c4d
parenta03435811860addf4cdb498caf5a15887be1db05 (diff)
downloadpodcast-e8ee8223a87cb8b730905808162435e5b716b59b.tar.bz2
Better error handling
-rw-r--r--src/actions.rs4
-rw-r--r--src/main.rs15
-rw-r--r--src/structs.rs53
-rw-r--r--src/utils.rs19
4 files changed, 51 insertions, 40 deletions
diff --git a/src/actions.rs b/src/actions.rs
index 553ac37..6f3cfc2 100644
--- a/src/actions.rs
+++ b/src/actions.rs
@@ -1,6 +1,6 @@
+use rayon::prelude::*;
use regex::Regex;
use reqwest;
-use rayon::prelude::*;
use rss::Channel;
use std::collections::HashSet;
use std::fs::{self, DirBuilder, File};
@@ -22,7 +22,7 @@ pub fn list_episodes(search: &str) {
let podcast = Podcast::from(channel);
let episodes = podcast.episodes();
for (num, ep) in episodes.iter().enumerate() {
- println!("({}) {}", episodes.len()-num, ep.title().unwrap());
+ println!("({}) {}", episodes.len() - num, ep.title().unwrap());
}
return;
}
diff --git a/src/main.rs b/src/main.rs
index 64f03ac..37da2eb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -17,11 +17,20 @@ mod utils;
use actions::*;
use clap::{Arg, App, SubCommand};
use structs::*;
+use utils::*;
fn main() {
- let mut state = State::new().expect(
- ".subscription file couldn't be parsed...I probably changed the format...sorry",
- );
+ if let Err(err) = create_directories() {
+ eprintln!("{}", err);
+ return;
+ }
+ let mut state = match State::new() {
+ Ok(val) => val,
+ Err(err) => {
+ eprintln!("{}", err);
+ return;
+ }
+ };
let config = Config::new();
let matches = App::new("podcast")
.version("1.0")
diff --git a/src/structs.rs b/src/structs.rs
index 2ea385e..730aab4 100644
--- a/src/structs.rs
+++ b/src/structs.rs
@@ -61,32 +61,37 @@ pub struct State {
impl State {
pub fn new() -> Result<State, String> {
- if let Err(err) = create_directories() {
- return Err(format!("{}", err))
- }
let mut path = get_podcast_dir();
path.push(".subscriptions");
if path.exists() {
let mut s = String::new();
- File::open(&path).unwrap().read_to_string(&mut s).unwrap();
- match serde_json::from_str(&s) {
- Ok(val) => {
- let mut state: State = val;
- // Check if a day has passed (86400 seconds)
- if state
- .last_run_time
- .signed_duration_since(Utc::now())
- .num_seconds() < -86400
- {
- update_rss(&mut state);
- }
- state.last_run_time = Utc::now();
- Ok(state)
+ let mut file = match File::open(&path) {
+ Ok(val) => val,
+ Err(err) => return Err(format!("{}", err)),
+ };
+ if let Err(err) = file.read_to_string(&mut s) {
+ return Err(format!("{}", err));
+ };
+ let mut state: State = match serde_json::from_str(&s) {
+ Ok(val) => val,
+ Err(err) => {
+ return Err(format!(
+ "Could not parse: {}\nReason: {}",
+ &path.to_str().unwrap(),
+ err
+ ))
}
- Err(_) => Err(format!(
- "Failed to parse .subscriptions ... I probably changed the schema ... sorry"
- )),
+ };
+ // Check if a day has passed (86400 seconds)
+ if state
+ .last_run_time
+ .signed_duration_since(Utc::now())
+ .num_seconds() < -86400
+ {
+ update_rss(&mut state);
}
+ state.last_run_time = Utc::now();
+ Ok(state)
} else {
Ok(State {
last_run_time: Utc::now(),
@@ -182,7 +187,7 @@ impl Podcast {
self.episodes().par_iter().for_each(
|ref i| if let Some(ep_title) =
- i.title()
+ i.title()
{
if !downloaded.contains(ep_title) {
if let Err(err) = i.download(self.title()) {
@@ -190,7 +195,7 @@ impl Podcast {
}
}
},
- );
+ );
}
pub fn download_specific(&self, episode_numbers: Vec<usize>) {
@@ -202,7 +207,7 @@ impl Podcast {
episode_numbers.par_iter().for_each(
|ep_num| if let Some(ep_title) =
- episodes[episodes.len() - ep_num].title()
+ episodes[episodes.len() - ep_num].title()
{
if !downloaded.contains(ep_title) {
if let Err(err) = episodes[episodes.len() - ep_num].download(self.title()) {
@@ -210,7 +215,7 @@ impl Podcast {
}
}
},
- );
+ );
}
}
diff --git a/src/utils.rs b/src/utils.rs
index 5444cb1..113517e 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,11 +1,9 @@
use std::collections::HashSet;
use std::env;
+use std::fs::DirBuilder;
use std::fs;
use std::num::ParseIntError;
use std::path::PathBuf;
-use std::fs::DirBuilder;
-use std::io;
-
pub fn trim_extension(filename: &str) -> String {
let name = String::from(filename);
@@ -28,16 +26,15 @@ pub fn find_extension(input: &str) -> Option<&str> {
}
}
-pub fn create_directories() -> io::Result<()> {
+pub fn create_directories() -> Result<(), String> {
let mut path = get_podcast_dir();
path.push(".rss");
if let Err(err) = DirBuilder::new().recursive(true).create(&path) {
- eprintln!(
- "Couldn't create directory: {}\nReason: {}",
- path.to_str().unwrap(),
- err
- );
- return Err(err)
+ return Err(format!(
+ "Couldn't create directory: {}\nReason: {}",
+ path.to_str().unwrap(),
+ err
+ ));
}
Ok(())
}
@@ -61,7 +58,7 @@ pub fn already_downloaded(dir: &str) -> HashSet<String> {
println!(
"OsString: {:?} couldn't be converted to String",
entry.file_name()
- );
+ );
}
}
}