Compare commits

..

5 Commits

Author SHA1 Message Date
dd4a2acce0 Version bump with breaking change 2025-10-11 14:50:23 -04:00
7c867695b5 Cleanup 2025-10-11 14:49:54 -04:00
cdcb64b742 Code cleanup 2025-10-11 14:47:49 -04:00
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 74 additions and 92 deletions

2
Cargo.lock generated
View File

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

View File

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

View File

@@ -1,4 +1,4 @@
use std::io::Write;
use std::io::{Read, Write};
use rand::Rng;
use serde::{Deserialize, Serialize};
@@ -7,9 +7,6 @@ use crate::constants;
use crate::init;
use crate::types;
/// Length of characters of a filename to be generated
const FILENAME_LENGTH: i32 = 16;
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
pub struct Song {
#[serde(skip_serializing_if = "init::is_uuid_nil")]
@@ -93,42 +90,31 @@ impl Song {
}
}
/// Saves the song to the filesystem using the song's data
pub fn save_to_filesystem(&self) -> Result<(), std::io::Error> {
match self.song_path() {
Ok(song_path) => match std::fs::File::create(&song_path) {
Ok(mut file) => match file.write_all(&self.data) {
Ok(_res) => Ok(()),
Err(err) => Err(err),
},
Err(err) => Err(err),
},
Err(err) => Err(err),
}
}
// TODO: Make this available as a function
pub fn to_data(&self) -> Result<Vec<u8>, std::io::Error> {
let path_result = self.song_path();
/// Removes the song from the filesystem
pub fn remove_from_filesystem(&self) -> Result<(), std::io::Error> {
match self.song_path() {
Ok(song_path) => {
let p = std::path::Path::new(&song_path);
if p.exists() {
match std::fs::remove_file(&p) {
Ok(_) => Ok(()),
Err(err) => Err(err),
}
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(())
Ok(buffer)
}
}
Err(err) => Err(err),
}
Err(er) => Err(er),
}
}
/// Generates a filename. In order to save a song to the filesystem, the song must have
/// a directory and filename
pub fn generate_filename(typ: types::MusicTypes, randomize: bool) -> String {
// 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)
@@ -146,28 +132,46 @@ pub fn generate_filename(typ: types::MusicTypes, randomize: bool) -> String {
};
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);
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);
}
}
filename + &file_extension
} else {
"track-output".to_string() + &file_extension
filename += "track-output";
}
filename += &file_extension;
filename
}
/// Saves the song to the filesystem using the song's data
pub fn save_to_filesystem(&self) -> Result<(), std::io::Error> {
match self.song_path() {
Ok(song_path) => match std::fs::File::create(&song_path) {
Ok(mut file) => match file.write_all(&self.data) {
Ok(_res) => Ok(()),
Err(err) => Err(err),
},
Err(err) => Err(err),
},
Err(err) => Err(err),
}
}
// TODO: Add function to remove file from the filesystem
}
/// 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,
@@ -197,22 +201,4 @@ 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::io::to_data(&song) {
match song.to_data() {
Ok(song_data) => {
println!("Both files match");
assert_eq!(buffer, song_data);
@@ -107,11 +107,11 @@ mod song_tests {
};
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!("File to be created: {:?}", song_cpy.filename);
match song::io::copy_song(&song, &mut song_cpy) {
match song::copy_song(&song, &mut song_cpy) {
Ok(_) => {}
Err(err) => {
assert!(false, "Error copying song: Error: {err:?}")
@@ -120,7 +120,7 @@ mod song_tests {
}
#[test]
fn test_save_song_to_filesystem_and_remove() {
fn test_save_song_to_filesystem() {
let mut song = song::Song::default();
song.directory = utils::get_tests_directory();
song.filename = String::from("track02.flac");
@@ -131,22 +131,18 @@ mod song_tests {
..Default::default()
};
match song::io::copy_song(&song, &mut copied_song) {
Ok(_) => match copied_song.remove_from_filesystem() {
match song::copy_song(&song, &mut copied_song) {
Ok(_) => {}
Err(err) => {
assert!(false, "Error: {err:?}")
}
},
Err(err) => {
assert!(false, "Error: {err:?}")
}
}
}
}
#[cfg(test)]
mod album_tests {
use crate::utils;
use icarus_models::album;