tsk-76: Improve getting path of Song or CoverArt #81
@@ -12,3 +12,9 @@ pub mod file_extensions {
|
|||||||
pub const PNGEXTENSION: &str = ".png";
|
pub const PNGEXTENSION: &str = ".png";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod error {
|
||||||
|
pub const DIRECTORY_NOT_INITIALIZED: &str = "Directory has not been initialized";
|
||||||
|
pub const FILENAME_NOT_INITIALIZED: &str = "Filename has not bee initialized";
|
||||||
|
pub const LAST_CHARACTER_IN_DIRECTORY: &str = "Could not access last character of directory";
|
||||||
|
}
|
||||||
|
|||||||
10
src/song.rs
10
src/song.rs
@@ -71,14 +71,15 @@ impl Song {
|
|||||||
|
|
||||||
pub fn song_path(&self) -> Result<String, std::io::Error> {
|
pub fn song_path(&self) -> Result<String, std::io::Error> {
|
||||||
if self.directory.is_empty() {
|
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() {
|
} 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 directory = &self.directory;
|
||||||
let last_index = directory.len() - 1;
|
let last_index = directory.len() - 1;
|
||||||
|
|
||||||
|
/*
|
||||||
if let Some(character) = directory.chars().nth(last_index) {
|
if let Some(character) = directory.chars().nth(last_index) {
|
||||||
let buffer: String = if character != '/' {
|
let buffer: String = if character != '/' {
|
||||||
directory.clone() + "/"
|
directory.clone() + "/"
|
||||||
@@ -92,6 +93,11 @@ impl Song {
|
|||||||
"Could not access last character of directory",
|
"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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Saves the song to the filesystem using the song's data
|
/// Saves the song to the filesystem using the song's data
|
||||||
|
|||||||
13
src/util/mod.rs
Normal file
13
src/util/mod.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
pub fn concatenate_path(directory: &str, filename: &str, last_index: usize) -> Result<String, std::io::Error> {
|
||||||
|
if let Some(character) = directory.chars().nth(last_index) {
|
||||||
|
let buffer: String = if character != '/' {
|
||||||
|
format!("{directory}/")
|
||||||
|
} else {
|
||||||
|
String::from(directory)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(format!("{buffer}{filename}"))
|
||||||
|
} else {
|
||||||
|
Err(std::io::Error::other(crate::constants::error::LAST_CHARACTER_IN_DIRECTORY))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user