tsk-76: Improve getting path of Song or CoverArt (#81)
All checks were successful
Rust Build / Test Suite (push) Successful in 28s
Release Tagging / release (push) Successful in 36s
Rust Build / Check (push) Successful in 43s
Rust Build / Rustfmt (push) Successful in 24s
Rust Build / build (push) Successful in 26s
Rust Build / Clippy (push) Successful in 35s

Closes #76

Reviewed-on: #81
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
This commit was merged in pull request #81.
This commit is contained in:
2025-10-24 16:51:08 +00:00
committed by phoenix
parent 02b6157e0d
commit afc4ca21a2
8 changed files with 50 additions and 32 deletions

2
Cargo.lock generated
View File

@@ -149,7 +149,7 @@ checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d"
[[package]]
name = "icarus_models"
version = "0.8.1"
version = "0.8.2"
dependencies = [
"josekit",
"rand",

View File

@@ -1,6 +1,6 @@
[package]
name = "icarus_models"
version = "0.8.1"
version = "0.8.2"
edition = "2024"
rust-version = "1.90"
description = "models used for the icarus project"

View File

@@ -1,2 +1,3 @@
A library containing commonly used models and functions that is used throughout
icarus projects. This reduces the amount of duplicated code without a benefit.
A library containing commonly used structs, functions, enums, constants and other code
that is used throughout the icarus projects. Code from this library serves as the model
for other projects in the icarus project.

View File

@@ -12,3 +12,9 @@ pub mod file_extensions {
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";
}

View File

@@ -68,26 +68,21 @@ impl CoverArt {
/// Gets the path of the CoverArt
pub fn get_path(&self) -> Result<String, std::io::Error> {
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),
}
}
}

View File

@@ -7,6 +7,7 @@ pub mod song;
pub mod token;
pub mod types;
pub mod user;
pub mod util;
pub mod init {
pub fn is_id_valid(num: &i32) -> bool {

View File

@@ -69,28 +69,24 @@ impl Song {
}
}
/// Gets the path of a Song
pub fn song_path(&self) -> Result<String, std::io::Error> {
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: 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),
}
}

19
src/util/mod.rs Normal file
View File

@@ -0,0 +1,19 @@
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,
))
}
}