From 5ac6493999019542aabad154c9fac1298cebac6e Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 11 Oct 2025 14:58:06 -0400 Subject: [PATCH 1/2] Moved to data as a function --- src/song.rs | 41 +++++++++++++++++++++-------------------- tests/tests.rs | 6 +++--- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/song.rs b/src/song.rs index a389f2b..47b995c 100644 --- a/src/song.rs +++ b/src/song.rs @@ -1,4 +1,4 @@ -use std::io::{Read, Write}; +use std::io::{Write}; use rand::Rng; use serde::{Deserialize, Serialize}; @@ -90,25 +90,6 @@ impl Song { } } - // TODO: Make this available as a function - pub fn to_data(&self) -> Result, std::io::Error> { - let path_result = self.song_path(); - - match path_result { - Ok(path) => { - let mut file = std::fs::File::open(path)?; - let mut buffer: Vec = Vec::new(); - file.read_to_end(&mut buffer)?; - - if buffer.is_empty() { - Err(std::io::Error::other("File is empty")) - } else { - Ok(buffer) - } - } - Err(er) => Err(er), - } - } // TODO: Make this available as a function pub fn generate_filename(&self, typ: types::MusicTypes, randomize: bool) -> String { @@ -172,6 +153,8 @@ impl Song { /// I/O operations for songs pub mod io { + use std::io::Read; + /// Copies a song using the source song's data pub fn copy_song( song_source: &super::Song, @@ -201,4 +184,22 @@ pub mod io { Err(err) => Err(err), } } + + /// Gets the raw file data of a song from the filesystem + pub fn to_data(song: &super::Song) -> Result, std::io::Error> { + match song.song_path() { + Ok(path) => { + let mut file = std::fs::File::open(path)?; + let mut buffer: Vec = Vec::new(); + file.read_to_end(&mut buffer)?; + + if buffer.is_empty() { + Err(std::io::Error::other("File is empty")) + } else { + Ok(buffer) + } + } + Err(er) => Err(er), + } + } } diff --git a/tests/tests.rs b/tests/tests.rs index 7692afa..d0a04df 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -61,7 +61,7 @@ mod song_tests { Ok(buffer) => { assert_eq!(buffer.is_empty(), false); - match song.to_data() { + match song::io::to_data(&song) { Ok(song_data) => { println!("Both files match"); assert_eq!(buffer, song_data); @@ -111,7 +111,7 @@ mod song_tests { println!("Directory: {:?}", song_cpy.directory); println!("File to be created: {:?}", song_cpy.filename); - match song::copy_song(&song, &mut song_cpy) { + match song::io::copy_song(&song, &mut song_cpy) { Ok(_) => {} Err(err) => { assert!(false, "Error copying song: Error: {err:?}") @@ -131,7 +131,7 @@ mod song_tests { ..Default::default() }; - match song::copy_song(&song, &mut copied_song) { + match song::io::copy_song(&song, &mut copied_song) { Ok(_) => {} Err(err) => { assert!(false, "Error: {err:?}") -- 2.43.0 From a70055958a929f22ccd43b864abf48c8128af0ef Mon Sep 17 00:00:00 2001 From: phoenix Date: Sat, 11 Oct 2025 14:58:24 -0400 Subject: [PATCH 2/2] Code cleanup --- src/song.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/song.rs b/src/song.rs index 47b995c..f5488fe 100644 --- a/src/song.rs +++ b/src/song.rs @@ -1,4 +1,4 @@ -use std::io::{Write}; +use std::io::Write; use rand::Rng; use serde::{Deserialize, Serialize}; @@ -90,7 +90,6 @@ impl Song { } } - // TODO: Make this available as a function pub fn generate_filename(&self, typ: types::MusicTypes, randomize: bool) -> String { let mut filename: String = String::new(); -- 2.43.0