diff options
| author | Nathan Jaremko | 2019-03-02 21:57:16 -0500 |
|---|---|---|
| committer | Nathan Jaremko | 2019-03-02 22:01:33 -0500 |
| commit | 28fc42b1a8a0c45b1d0ef22d60a9b693c4294474 (patch) | |
| tree | da62cd54862c61faea60ef38686d5a3f003a2da5 | |
| parent | d3cec33fdd5589cfa11b81e2c8b2f82138d13fad (diff) | |
| download | podcast-28fc42b1a8a0c45b1d0ef22d60a9b693c4294474.tar.bz2 | |
Improve error message when file already exists when using range download
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/download.rs | 16 | ||||
| -rw-r--r-- | src/main.rs | 2 |
3 files changed, 13 insertions, 7 deletions
@@ -1,7 +1,7 @@ [package] name = "podcast" edition = "2018" -version = "0.10.1" +version = "0.10.2" authors = ["Nathan Jaremko <njaremko@gmail.com>"] description = "A command line podcast manager" license = "GPL-3.0" diff --git a/src/download.rs b/src/download.rs index 69dc853..be4598d 100644 --- a/src/download.rs +++ b/src/download.rs @@ -4,6 +4,7 @@ use crate::utils::*; use std::collections::HashSet; use std::fs::File; use std::io::{self, BufReader, BufWriter, Write}; +use failure::Fail; use failure::Error; use rayon::prelude::*; @@ -16,18 +17,15 @@ pub fn download_range(state: &State, p_search: &str, e_search: &str) -> Result<( for subscription in &state.subscriptions { if re_pod.is_match(&subscription.title) { let podcast = Podcast::from_title(&subscription.title)?; - let downloaded = already_downloaded(podcast.title())?; let episodes = podcast.episodes(); let episodes_to_download = parse_download_episodes(e_search)?; episodes_to_download .par_iter() .map(|ep_num| &episodes[episodes.len() - ep_num]) - .filter(|e| e.title().is_some()) - .filter(|e| !downloaded.contains(&e.title().unwrap())) .map(|ep| download(podcast.title(), ep)) .flat_map(|e| e.err()) - .for_each(|err| eprintln!("Error: {}", err)); + .for_each(|err| println!("Error: {}", err)); } } Ok(()) @@ -52,6 +50,14 @@ pub fn download_episode_by_num(state: &State, p_search: &str, e_search: &str) -> Ok(()) } +#[derive(Debug, Fail)] +enum DownloadError { + #[fail(display = "File already exists: {}", path)] + AlreadyExists { + path: String, + } +} + pub fn download(podcast_name: &str, episode: &Episode) -> Result<(), Error> { let mut path = get_podcast_dir()?; path.push(podcast_name); @@ -68,7 +74,7 @@ pub fn download(podcast_name: &str, episode: &Episode) -> Result<(), Error> { let mut writer = BufWriter::new(file); io::copy(&mut reader, &mut writer)?; } else { - eprintln!("File already exists: {:?}", &path); + return Err(DownloadError::AlreadyExists{path: path.to_str().unwrap().to_string()}.into()); } } Ok(()) diff --git a/src/main.rs b/src/main.rs index 640e480..e83f527 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,7 +38,7 @@ mod errors { use self::structs::*; use errors::Result; -const VERSION: &str = "0.10.1"; +const VERSION: &str = "0.10.2"; fn main() -> Result<()> { utils::create_directories()?; |
