From 69423d8d3960de6fe1de9c51eb16f459cea03701 Mon Sep 17 00:00:00 2001 From: phoenix Date: Fri, 24 Oct 2025 12:40:34 -0400 Subject: [PATCH] tsk-76: Cleanup --- src/coverart.rs | 23 +++++++++-------------- src/song.rs | 26 ++++++++------------------ src/util/mod.rs | 10 ++++++++-- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/coverart.rs b/src/coverart.rs index 0d50feb..23c3740 100644 --- a/src/coverart.rs +++ b/src/coverart.rs @@ -68,26 +68,21 @@ impl CoverArt { /// Gets the path of the CoverArt pub fn get_path(&self) -> Result { if self.directory.is_empty() { - return Err(std::io::Error::other("Directory has not been initialized")); + return Err(std::io::Error::other( + crate::constants::error::DIRECTORY_NOT_INITIALIZED, + )); } else if self.filename.is_empty() { - return Err(std::io::Error::other("Filename has not bee initialized")); + return Err(std::io::Error::other( + crate::constants::error::FILENAME_NOT_INITIALIZED, + )); } let directory = &self.directory; let last_index = directory.len() - 1; - if let Some(character) = directory.chars().nth(last_index) { - let buffer = if character != '/' { - directory.clone() + "/" - } else { - directory.clone() - }; - - Ok(buffer + &self.filename.clone()) - } else { - Err(std::io::Error::other( - "Could not access last character of directory", - )) + match crate::util::concatenate_path(&directory, &self.filename, last_index) { + Ok(path) => Ok(path), + Err(err) => Err(err), } } } diff --git a/src/song.rs b/src/song.rs index 5254c83..cf22272 100644 --- a/src/song.rs +++ b/src/song.rs @@ -69,34 +69,24 @@ impl Song { } } + /// Gets the path of a Song pub fn song_path(&self) -> Result { if self.directory.is_empty() { - return Err(std::io::Error::other(crate::constants::error::DIRECTORY_NOT_INITIALIZED)); + return Err(std::io::Error::other( + crate::constants::error::DIRECTORY_NOT_INITIALIZED, + )); } else if self.filename.is_empty() { - return Err(std::io::Error::other(crate::constants::error::FILENAME_NOT_INITIALIZED)); + return Err(std::io::Error::other( + crate::constants::error::FILENAME_NOT_INITIALIZED, + )); } let directory = &self.directory; let last_index = directory.len() - 1; - /* - if let Some(character) = directory.chars().nth(last_index) { - let buffer: String = if character != '/' { - directory.clone() + "/" - } else { - directory.clone() - }; - - Ok(buffer + &self.filename.clone()) - } else { - Err(std::io::Error::other( - "Could not access last character of directory", - )) - } - */ match crate::util::concatenate_path(&directory, &self.filename, last_index) { Ok(path) => Ok(path), - Err(err) => Err(err) + Err(err) => Err(err), } } diff --git a/src/util/mod.rs b/src/util/mod.rs index 8543f65..983edbd 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,4 +1,8 @@ -pub fn concatenate_path(directory: &str, filename: &str, last_index: usize) -> Result { +pub fn concatenate_path( + directory: &str, + filename: &str, + last_index: usize, +) -> Result { if let Some(character) = directory.chars().nth(last_index) { let buffer: String = if character != '/' { format!("{directory}/") @@ -8,6 +12,8 @@ pub fn concatenate_path(directory: &str, filename: &str, last_index: usize) -> R Ok(format!("{buffer}{filename}")) } else { - Err(std::io::Error::other(crate::constants::error::LAST_CHARACTER_IN_DIRECTORY)) + Err(std::io::Error::other( + crate::constants::error::LAST_CHARACTER_IN_DIRECTORY, + )) } }