From 64402a0626a2277a24caf0fe446382b5c0340e8e Mon Sep 17 00:00:00 2001 From: KD Date: Sat, 15 Mar 2025 21:26:54 +0000 Subject: [PATCH] Added functions --- src/song.rs | 122 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 108 insertions(+), 14 deletions(-) diff --git a/src/song.rs b/src/song.rs index 522bcb0..1a0fdd6 100644 --- a/src/song.rs +++ b/src/song.rs @@ -98,32 +98,126 @@ impl Song { } } - // TODO: Implement - pub fn song_path(&self) -> String { - return String::new(); + pub fn song_path(&self) -> Result { + if self.directory.is_empty() { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Directory does not exist", + )); + } + + let directory = &self.directory; + let mut buffer: String = String::from(directory.clone()); + let lastIndex = directory.len() - 1; + + if let Some(character) = directory.chars().nth(lastIndex) { + if character != '/' { + buffer += "/"; + } + + buffer += &self.filename.clone(); + + return Ok(buffer); + } else { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Could not access last character of directory", + )); + } } pub fn to_data(&self) -> Result, std::io::Error> { - let path = self.song_path(); + let pathResult = self.song_path(); - let mut file = std::fs::File::open(path)?; - let mut buffer: Vec = Vec::new(); - file.read_to_end(&mut buffer)?; + match pathResult { + Ok(path) => { + let mut file = std::fs::File::open(path)?; + let mut buffer: Vec = Vec::new(); + file.read_to_end(&mut buffer)?; - if buffer.len() == 0 { - Err(std::io::Error::new( - std::io::ErrorKind::Other, - "File is empty", - )) - } else { - Ok(buffer) + if buffer.len() == 0 { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "File is empty", + )); + } else { + return Ok(buffer); + } + } + Err(er) => { + return Err(er); + } } } } mod embedded { + use std::io::Read; + use serde::{Deserialize, Serialize}; + impl Song { + pub fn to_metadata_json(&self, pretty: bool) -> Result { + if pretty { + return serde_json::to_string_pretty(&self); + } else { + return serde_json::to_string(&self); + } + } + + pub fn song_path(&self) -> Result { + if self.directory.is_empty() { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Directory does not exist", + )); + } + + let directory = &self.directory; + let mut buffer: String = String::from(directory.clone()); + let lastIndex = directory.len() - 1; + + if let Some(character) = directory.chars().nth(lastIndex) { + if character != '/' { + buffer += "/"; + } + + buffer += &self.filename.clone(); + + return Ok(buffer); + } else { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Could not access last character of directory", + )); + } + } + + pub fn to_data(&self) -> Result, std::io::Error> { + let pathResult = self.song_path(); + + match pathResult { + Ok(path) => { + let mut file = std::fs::File::open(path)?; + let mut buffer: Vec = Vec::new(); + file.read_to_end(&mut buffer)?; + + if buffer.len() == 0 { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "File is empty", + )); + } else { + return Ok(buffer); + } + } + Err(er) => { + return Err(er); + } + } + } + } + // The song's duration is a floating point in seconds #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Song {