Compare commits

..

2 Commits

Author SHA1 Message Date
a867f65a26 Modified test 2025-10-11 14:41:05 -04:00
43b9c512a1 Added code to copy song 2025-10-11 14:40:46 -04:00
4 changed files with 134 additions and 107 deletions

2
Cargo.lock generated
View File

@@ -142,7 +142,7 @@ checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
[[package]] [[package]]
name = "icarus_models" name = "icarus_models"
version = "0.6.2" version = "0.5.6"
dependencies = [ dependencies = [
"josekit", "josekit",
"rand", "rand",

View File

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

View File

@@ -1,13 +1,11 @@
use std::io::Write; use std::io::{Read, Write};
use rand::Rng;
use serde::{Deserialize, Serialize};
use crate::constants; use crate::constants;
use crate::init; use crate::init;
use crate::types; use crate::types;
const FILENAME_LENGTH: i32 = 16; use rand::Rng;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)] #[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
pub struct Song { pub struct Song {
@@ -92,6 +90,68 @@ 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();
let filename_len = 10;
let file_extension = match typ {
types::MusicTypes::DefaultMusicExtension => {
String::from(constants::file_extensions::audio::DEFAULTMUSICEXTENSION)
}
types::MusicTypes::WavExtension => {
String::from(constants::file_extensions::audio::WAVEXTENSION)
}
types::MusicTypes::FlacExtension => {
String::from(constants::file_extensions::audio::FLACEXTENSION)
}
types::MusicTypes::MPThreeExtension => {
String::from(constants::file_extensions::audio::MPTHREEEXTENSION)
}
};
if randomize {
let some_chars: String = String::from("abcdefghij0123456789");
let mut rng = rand::rng();
for _i in 0..filename_len {
let random_number: i32 = rng.random_range(0..=19);
let index = random_number as usize;
let rando_char = some_chars.chars().nth(index);
if let Some(c) = rando_char {
filename.push(c);
}
}
} else {
filename += "track-output";
}
filename += &file_extension;
filename
}
/// Saves the song to the filesystem using the song's data /// Saves the song to the filesystem using the song's data
pub fn save_to_filesystem(&self) -> Result<(), std::io::Error> { pub fn save_to_filesystem(&self) -> Result<(), std::io::Error> {
@@ -110,54 +170,8 @@ impl Song {
// TODO: Add function to remove file from the filesystem // TODO: Add function to remove file from the filesystem
} }
/// Copies a song using the source song's data
/// Generates a filename. In order to save a song to the filesystem, the song must have pub fn copy_song(song_source: &Song, song_target: &mut Song) -> Result<(), std::io::Error> {
/// a directory and filename
pub fn generate_filename(typ: types::MusicTypes, randomize: bool) -> String {
let file_extension = match typ {
types::MusicTypes::DefaultMusicExtension => {
String::from(constants::file_extensions::audio::DEFAULTMUSICEXTENSION)
}
types::MusicTypes::WavExtension => {
String::from(constants::file_extensions::audio::WAVEXTENSION)
}
types::MusicTypes::FlacExtension => {
String::from(constants::file_extensions::audio::FLACEXTENSION)
}
types::MusicTypes::MPThreeExtension => {
String::from(constants::file_extensions::audio::MPTHREEEXTENSION)
}
};
if randomize {
let mut filename: String = String::new();
let some_chars: String = String::from("abcdefghij0123456789");
let mut rng = rand::rng();
for _ in 0..FILENAME_LENGTH {
let index = rng.random_range(0..=19);
let rando_char = some_chars.chars().nth(index);
if let Some(c) = rando_char {
filename.push(c);
}
}
filename + &file_extension
} else {
"track-output".to_string() + &file_extension
}
}
/// 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,
song_target: &mut super::Song,
) -> Result<(), std::io::Error> {
match song_target.song_path() { match song_target.song_path() {
Ok(songpath) => { Ok(songpath) => {
let p = std::path::Path::new(&songpath); let p = std::path::Path::new(&songpath);
@@ -181,23 +195,4 @@ pub mod io {
} }
Err(err) => Err(err), 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

@@ -30,12 +30,16 @@ mod utils {
#[cfg(test)] #[cfg(test)]
mod song_tests { mod song_tests {
use std::fs::File;
use std::io::Write;
use tempfile::tempdir; use tempfile::tempdir;
use crate::utils;
use icarus_models::song; use icarus_models::song;
use icarus_models::types; use icarus_models::types;
use crate::utils;
#[test] #[test]
fn test_song_to_data() { fn test_song_to_data() {
println!("Test"); println!("Test");
@@ -61,7 +65,7 @@ mod song_tests {
Ok(buffer) => { Ok(buffer) => {
assert_eq!(buffer.is_empty(), false); assert_eq!(buffer.is_empty(), false);
match song::io::to_data(&song) { match song.to_data() {
Ok(song_data) => { Ok(song_data) => {
println!("Both files match"); println!("Both files match");
assert_eq!(buffer, song_data); assert_eq!(buffer, song_data);
@@ -99,6 +103,9 @@ mod song_tests {
song.directory = utils::get_tests_directory(); song.directory = utils::get_tests_directory();
song.filename = String::from("track01.flac"); song.filename = String::from("track01.flac");
match song.song_path() {
Ok(songpath) => match utils::extract_data_from_file(&songpath) {
Ok(buffer) => {
let mut song_cpy = song.clone(); let mut song_cpy = song.clone();
let temp_dir = tempdir().expect("Failed to create temp dir"); let temp_dir = tempdir().expect("Failed to create temp dir");
song_cpy.directory = match temp_dir.path().to_str() { song_cpy.directory = match temp_dir.path().to_str() {
@@ -107,14 +114,39 @@ mod song_tests {
}; };
assert_eq!(song.directory.is_empty(), false); assert_eq!(song.directory.is_empty(), false);
song_cpy.filename = song::generate_filename(types::MusicTypes::FlacExtension, true); song_cpy.filename =
song.generate_filename(types::MusicTypes::FlacExtension, true);
println!("Directory: {:?}", song_cpy.directory); println!("Directory: {:?}", song_cpy.directory);
println!("File to be created: {:?}", song_cpy.filename); println!("File to be created: {:?}", song_cpy.filename);
match song::io::copy_song(&song, &mut song_cpy) { let path = match song_cpy.song_path() {
Ok(_) => {} Ok(s_path) => s_path,
Err(err) => { Err(err) => {
assert!(false, "Error copying song: Error: {err:?}") assert!(false, "Error: {:?}", err);
String::new()
}
};
match File::create(path) {
Ok(mut file_cpy) => match file_cpy.write_all(&buffer) {
Ok(success) => {
println!("Success: {:?}", success);
}
Err(err) => {
assert!(false, "Error saving file: {:?}", err);
}
},
Err(err) => {
assert!(false, "Error: {:?}", err);
}
};
}
Err(err) => {
assert!(false, "Error: {:?}", err);
}
},
Err(err) => {
assert!(false, "Error extracting song data: {:?}", err);
} }
} }
} }
@@ -131,7 +163,7 @@ mod song_tests {
..Default::default() ..Default::default()
}; };
match song::io::copy_song(&song, &mut copied_song) { match song::copy_song(&song, &mut copied_song) {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
assert!(false, "Error: {err:?}") assert!(false, "Error: {err:?}")