diff options
| author | Nathan Jaremko | 2018-03-21 23:19:20 -0400 |
|---|---|---|
| committer | Nathan Jaremko | 2018-03-21 23:19:20 -0400 |
| commit | eb11c15038c358fc8c0794148d3b99d255c33206 (patch) | |
| tree | fd9b4774efd6fdd7171f8cfac1a184d21439732e | |
| parent | 36ec8131c0e71bad454aeb5e66c63f04cfb93e2c (diff) | |
| download | podcast-eb11c15038c358fc8c0794148d3b99d255c33206.tar.bz2 | |
Improve code
| -rw-r--r-- | src/actions.rs | 14 | ||||
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/structs.rs | 24 | ||||
| -rw-r--r-- | src/utils.rs | 5 |
4 files changed, 21 insertions, 24 deletions
diff --git a/src/actions.rs b/src/actions.rs index 3d89002..e9cf27b 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -47,7 +47,7 @@ pub fn download_rss(config: &Config, url: &str) { if download_limit > 0 { let podcast = Podcast::from(channel); let episodes = podcast.episodes(); - &episodes[..download_limit].par_iter().for_each(|ref ep| { + episodes[..download_limit].par_iter().for_each(|ep| { if let Err(err) = ep.download(podcast.title()) { eprintln!("Error downloading {}: {}", podcast.title(), err); } @@ -60,7 +60,7 @@ pub fn download_rss(config: &Config, url: &str) { pub fn update_rss(state: &mut State) { println!("Checking for new episodes..."); - &state.subscriptions.par_iter_mut().for_each(|sub| { + state.subscriptions.par_iter_mut().for_each(|sub| { let mut path = get_podcast_dir(); path.push(&sub.title); DirBuilder::new().recursive(true).create(&path).unwrap(); @@ -85,9 +85,9 @@ pub fn update_rss(state: &mut State) { file.write_all(&content).unwrap(); if podcast.episodes().len() > sub.num_episodes { - &podcast.episodes()[..podcast.episodes().len() - sub.num_episodes] + podcast.episodes()[..podcast.episodes().len() - sub.num_episodes] .par_iter() - .for_each(|ref ep| { + .for_each(|ep| { if let Err(err) = ep.download(podcast.title()) { eprintln!("Error downloading {}: {}", podcast.title(), err); } @@ -111,7 +111,7 @@ pub fn download_range(state: &State, p_search: &str, e_search: &str) { match Podcast::from_title(&subscription.title) { Ok(podcast) => match parse_download_episodes(e_search) { Ok(episodes_to_download) => { - if let Err(err) = podcast.download_specific(episodes_to_download) { + if let Err(err) = podcast.download_specific(&episodes_to_download) { eprintln!("Error: {}", err); } } @@ -259,8 +259,8 @@ pub fn check_for_update(version: &str) { } fn launch_player(url: &str) { - if let Err(_) = launch_mpv(&url) { - launch_vlc(&url) + if launch_mpv(url).is_err() { + launch_vlc(url) } } diff --git a/src/main.rs b/src/main.rs index e7f4e75..599ec72 100644 --- a/src/main.rs +++ b/src/main.rs @@ -161,7 +161,7 @@ fn main() { let rm_matches = matches.subcommand_matches("rm").unwrap(); match rm_matches.value_of("PODCAST") { Some(regex) => remove_podcast(&mut state, regex), - None => println!(""), + None => println!(), } } Some("completion") => { diff --git a/src/structs.rs b/src/structs.rs index b9db14d..ac6832e 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -25,7 +25,7 @@ impl Config { let mut s = String::new(); File::open(&path).unwrap().read_to_string(&mut s).unwrap(); let config = YamlLoader::load_from_str(&s).unwrap(); - if config.len() > 0 { + if !config.is_empty() { let doc = &config[0]; if let Some(val) = doc["auto_download_limit"].as_i64() { download_limit = val; @@ -183,10 +183,10 @@ impl Podcast { match File::open(&path) { Ok(file) => match Channel::read_from(BufReader::new(file)) { - Ok(podcast) => return Ok(Podcast::from(podcast)), - Err(err) => return Err(format!("Error: {}", err)), + Ok(podcast) => Ok(Podcast::from(podcast)), + Err(err) => Err(format!("Error: {}", err)), }, - Err(err) => return Err(format!("Error: {}", err)), + Err(err) => Err(format!("Error: {}", err)), } } @@ -196,12 +196,12 @@ impl Podcast { filename.push_str(".xml"); path.push(filename); - return remove_file(path); + remove_file(path) } pub fn delete_all() -> io::Result<()> { let path = get_xml_dir(); - return remove_dir_all(path); + remove_dir_all(path) } pub fn episodes(&self) -> Vec<Episode> { @@ -218,10 +218,8 @@ impl Podcast { print!("You are about to download all episodes (y/n): "); io::stdout().flush().ok(); let mut input = String::new(); - if let Ok(_) = io::stdin().read_line(&mut input) { - if input.to_lowercase().trim() != "y" { - return Ok(()); - } + if io::stdin().read_line(&mut input).is_ok() && input.to_lowercase().trim() != "y" { + return Ok(()); } let mut path = get_podcast_dir(); @@ -229,7 +227,7 @@ impl Podcast { match already_downloaded(self.title()) { Ok(downloaded) => { - self.episodes().par_iter().for_each(|ref i| { + self.episodes().par_iter().for_each(|i| { if let Some(ep_title) = i.title() { if !downloaded.contains(ep_title) { if let Err(err) = i.download(self.title()) { @@ -240,7 +238,7 @@ impl Podcast { }); } Err(_) => { - self.episodes().par_iter().for_each(|ref i| { + self.episodes().par_iter().for_each(|i| { if let Err(err) = i.download(self.title()) { println!("{}", err); } @@ -251,7 +249,7 @@ impl Podcast { Ok(()) } - pub fn download_specific(&self, episode_numbers: Vec<usize>) -> Result<(), io::Error> { + pub fn download_specific(&self, episode_numbers: &[usize]) -> Result<(), io::Error> { let mut path = get_podcast_dir(); path.push(self.title()); diff --git a/src/utils.rs b/src/utils.rs index 29bd15a..597d9b6 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -56,8 +56,7 @@ pub fn already_downloaded(dir: &str) -> Result<HashSet<String>, io::Error> { for entry in entries { let entry = entry?; match entry.file_name().into_string() { - Ok(val) => { - let name = String::from(val); + Ok(name) => { let index = name.find('.').unwrap(); result.insert(String::from(&name[0..index])); } @@ -118,7 +117,7 @@ pub fn parse_download_episodes(e_search: &str) -> Result<Vec<usize>, ParseIntErr let comma_separated: Vec<&str> = input.split(',').collect(); for elem in comma_separated { let temp = String::from(elem); - if temp.contains("-") { + if temp.contains('-') { let range: Vec<usize> = elem.split('-') .map(|i| i.parse::<usize>().unwrap()) .collect(); |
