From 1f333d647582c7dd800285f09f6d15c64ef11f35 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 1 Feb 2023 20:34:23 +0100 Subject: [PATCH] check if path exists (without extension) --- src/file_sink.rs | 15 ++++++--------- src/main.rs | 26 +++++++++++++++++--------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/file_sink.rs b/src/file_sink.rs index 8cfd812..966694b 100644 --- a/src/file_sink.rs +++ b/src/file_sink.rs @@ -22,16 +22,13 @@ impl FileSink { } impl Open for FileSink { + // use the unwrap_or_else method instead of the if let statement to handle the absence of a path value fn open(path: Option, _audio_format: AudioFormat) -> Self { - if let Some(path) = path { - let file = path; - FileSink { - sink: file, - content: Vec::new(), - metadata: None - } - } else { - panic!(); + let file = path.unwrap_or_else(|| panic!()); + FileSink { + sink: file, + content: Vec::new(), + metadata: None } } } diff --git a/src/main.rs b/src/main.rs index c04a4b1..7d154d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod file_sink; extern crate rpassword; use std::{path::PathBuf}; +use std::path::Path; use librespot::{core::authentication::Credentials, metadata::Playlist}; use librespot::core::config::SessionConfig; @@ -82,15 +83,22 @@ async fn download_tracks(session: &Session, destination: PathBuf, tracks: Vec