Song changes #60

Merged
phoenix merged 10 commits from song_changes into main 2025-10-11 19:49:53 +00:00
2 changed files with 24 additions and 24 deletions
Showing only changes of commit d7c078d95a - Show all commits

View File

@@ -1,4 +1,4 @@
use std::io::{Read, Write};
use std::io::Write;
use rand::Rng;
use serde::{Deserialize, Serialize};
@@ -90,26 +90,6 @@ impl Song {
}
}
// TODO: Make this available as a function
pub fn to_data(&self) -> Result<Vec<u8>, 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<u8> = 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 {
let mut filename: String = String::new();
@@ -172,6 +152,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 +183,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<Vec<u8>, std::io::Error> {
match song.song_path() {
Ok(path) => {
let mut file = std::fs::File::open(path)?;
let mut buffer: Vec<u8> = 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),
}
}
}

View File

@@ -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:?}")