Compare commits

..

7 Commits

Author SHA1 Message Date
5143b03e1a Code formatting 2025-10-11 16:19:17 -04:00
f72fb8963f Added method to save coverart to the filesystem 2025-10-11 16:18:48 -04:00
440caca7c2 Moved to_data (#66)
Some checks failed
Rust Build / Check (pull_request) Successful in 33s
Release Tagging / release (pull_request) Successful in 36s
Rust Build / Test Suite (pull_request) Successful in 45s
Rust Build / Rustfmt (pull_request) Successful in 33s
Rust Build / Clippy (pull_request) Failing after 44s
Rust Build / build (pull_request) Successful in 1m7s
Reviewed-on: #66
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2025-10-11 20:11:39 +00:00
0c0b4ba7ca Merge pull request 'Song changes' (#60) from song_changes into main
Some checks failed
Rust Build / Check (push) Successful in 30s
Rust Build / Rustfmt (push) Successful in 33s
Rust Build / Test Suite (push) Successful in 39s
Rust Build / Clippy (push) Failing after 36s
Rust Build / build (push) Successful in 35s
Rust Build / Check (pull_request) Successful in 32s
Release Tagging / release (pull_request) Failing after 36s
Rust Build / Test Suite (pull_request) Successful in 34s
Rust Build / Rustfmt (pull_request) Successful in 32s
Rust Build / build (pull_request) Successful in 31s
Rust Build / Clippy (pull_request) Failing after 34s
Reviewed-on: #60
2025-10-11 19:49:52 +00:00
c263cedf28 Remove song (#64)
Some checks failed
Release Tagging / release (pull_request) Successful in 33s
Rust Build / Check (pull_request) Successful in 43s
Rust Build / Test Suite (pull_request) Successful in 44s
Rust Build / Rustfmt (pull_request) Successful in 32s
Rust Build / Clippy (pull_request) Failing after 32s
Rust Build / build (pull_request) Successful in 38s
Reviewed-on: #64
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2025-10-11 19:46:00 +00:00
0637a9432e Changes to how song filenames are generated (#63)
Some checks failed
Rust Build / Test Suite (pull_request) Successful in 35s
Rust Build / Check (pull_request) Successful in 38s
Release Tagging / release (pull_request) Successful in 48s
Rust Build / Rustfmt (pull_request) Failing after 33s
Rust Build / Clippy (pull_request) Successful in 34s
Rust Build / build (pull_request) Successful in 35s
Reviewed-on: #63
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2025-10-11 19:20:57 +00:00
d7c078d95a Song to_data change (#62)
All checks were successful
Rust Build / Check (pull_request) Successful in 35s
Release Tagging / release (pull_request) Successful in 43s
Rust Build / Test Suite (pull_request) Successful in 46s
Rust Build / Rustfmt (pull_request) Successful in 33s
Rust Build / build (pull_request) Successful in 33s
Rust Build / Clippy (pull_request) Successful in 50s
Reviewed-on: #62
Co-authored-by: phoenix <kundeng00@pm.me>
Co-committed-by: phoenix <kundeng00@pm.me>
2025-10-11 19:02:01 +00:00
5 changed files with 113 additions and 80 deletions

2
Cargo.lock generated
View File

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

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "icarus_models" name = "icarus_models"
version = "0.6.0" version = "0.6.4"
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,4 +1,4 @@
use std::io::Read; use std::io::Write;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -28,8 +28,26 @@ pub mod init {
} }
impl CoverArt { impl CoverArt {
pub fn to_data(&self) -> Result<Vec<u8>, std::io::Error> { /// Saves the coverart to the filesystem
let path: &String = &self.path; pub fn save_to_filesystem(&self) -> Result<(), std::io::Error> {
match std::fs::File::create(&self.path) {
Ok(mut file) => match file.write_all(&self.data) {
Ok(_) => Ok(()),
Err(err) => Err(err),
},
Err(err) => Err(err),
}
}
// TODO: Add method to remove from filesystem
}
pub mod io {
use std::io::Read;
/// Gets the raw data of the cover art
pub fn to_data(coverart: &super::CoverArt) -> Result<Vec<u8>, std::io::Error> {
let path: &String = &coverart.path;
let mut file = std::fs::File::open(path)?; let mut file = std::fs::File::open(path)?;
let mut buffer = Vec::new(); let mut buffer = Vec::new();
match file.read_to_end(&mut buffer) { match file.read_to_end(&mut buffer) {
@@ -37,9 +55,6 @@ impl CoverArt {
Err(err) => Err(err), Err(err) => Err(err),
} }
} }
// TODO: Add method to save to filesystem
// TODO: Add method to remove from filesystem
} }
#[cfg(test)] #[cfg(test)]

View File

@@ -1,4 +1,4 @@
use std::io::{Read, Write}; use std::io::Write;
use rand::Rng; use rand::Rng;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@@ -7,6 +7,9 @@ use crate::constants;
use crate::init; use crate::init;
use crate::types; 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)] #[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
pub struct Song { pub struct Song {
#[serde(skip_serializing_if = "init::is_uuid_nil")] #[serde(skip_serializing_if = "init::is_uuid_nil")]
@@ -90,31 +93,42 @@ impl Song {
} }
} }
// TODO: Make this available as a function /// Saves the song to the filesystem using the song's data
pub fn to_data(&self) -> Result<Vec<u8>, std::io::Error> { pub fn save_to_filesystem(&self) -> Result<(), std::io::Error> {
let path_result = self.song_path(); 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),
}
}
match path_result { /// Removes the song from the filesystem
Ok(path) => { pub fn remove_from_filesystem(&self) -> Result<(), std::io::Error> {
let mut file = std::fs::File::open(path)?; match self.song_path() {
let mut buffer: Vec<u8> = Vec::new(); Ok(song_path) => {
file.read_to_end(&mut buffer)?; let p = std::path::Path::new(&song_path);
if p.exists() {
if buffer.is_empty() { match std::fs::remove_file(&p) {
Err(std::io::Error::other("File is empty")) Ok(_) => Ok(()),
Err(err) => Err(err),
}
} else { } else {
Ok(buffer) Ok(())
} }
} }
Err(er) => Err(er), Err(err) => Err(err),
}
} }
} }
// TODO: Make this available as a function /// Generates a filename. In order to save a song to the filesystem, the song must have
pub fn generate_filename(&self, typ: types::MusicTypes, randomize: bool) -> String { /// a directory and filename
let mut filename: String = String::new(); pub fn generate_filename(typ: types::MusicTypes, randomize: bool) -> String {
let filename_len = 10;
let file_extension = match typ { let file_extension = match typ {
types::MusicTypes::DefaultMusicExtension => { types::MusicTypes::DefaultMusicExtension => {
String::from(constants::file_extensions::audio::DEFAULTMUSICEXTENSION) String::from(constants::file_extensions::audio::DEFAULTMUSICEXTENSION)
@@ -132,46 +146,28 @@ impl Song {
}; };
if randomize { if randomize {
let mut filename: String = String::new();
let some_chars: String = String::from("abcdefghij0123456789"); let some_chars: String = String::from("abcdefghij0123456789");
let mut rng = rand::rng(); let mut rng = rand::rng();
for _i in 0..filename_len { for _ in 0..FILENAME_LENGTH {
let random_number: i32 = rng.random_range(0..=19); let index = rng.random_range(0..=19);
let index = random_number as usize;
let rando_char = some_chars.chars().nth(index); let rando_char = some_chars.chars().nth(index);
if let Some(c) = rando_char { if let Some(c) = rando_char {
filename.push(c); filename.push(c);
} }
} }
filename + &file_extension
} else { } else {
filename += "track-output"; "track-output".to_string() + &file_extension
} }
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 /// I/O operations for songs
pub mod io { pub mod io {
use std::io::Read;
/// Copies a song using the source song's data /// Copies a song using the source song's data
pub fn copy_song( pub fn copy_song(
song_source: &super::Song, song_source: &super::Song,
@@ -201,4 +197,22 @@ 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

@@ -61,7 +61,7 @@ mod song_tests {
Ok(buffer) => { Ok(buffer) => {
assert_eq!(buffer.is_empty(), false); assert_eq!(buffer.is_empty(), false);
match song.to_data() { match song::io::to_data(&song) {
Ok(song_data) => { Ok(song_data) => {
println!("Both files match"); println!("Both files match");
assert_eq!(buffer, song_data); assert_eq!(buffer, song_data);
@@ -107,11 +107,11 @@ 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::copy_song(&song, &mut song_cpy) { match song::io::copy_song(&song, &mut song_cpy) {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
assert!(false, "Error copying song: Error: {err:?}") assert!(false, "Error copying song: Error: {err:?}")
@@ -120,7 +120,7 @@ mod song_tests {
} }
#[test] #[test]
fn test_save_song_to_filesystem() { fn test_save_song_to_filesystem_and_remove() {
let mut song = song::Song::default(); let mut song = song::Song::default();
song.directory = utils::get_tests_directory(); song.directory = utils::get_tests_directory();
song.filename = String::from("track02.flac"); song.filename = String::from("track02.flac");
@@ -131,18 +131,22 @@ mod song_tests {
..Default::default() ..Default::default()
}; };
match song::copy_song(&song, &mut copied_song) { match song::io::copy_song(&song, &mut copied_song) {
Ok(_) => match copied_song.remove_from_filesystem() {
Ok(_) => {} Ok(_) => {}
Err(err) => { Err(err) => {
assert!(false, "Error: {err:?}") assert!(false, "Error: {err:?}")
} }
},
Err(err) => {
assert!(false, "Error: {err:?}")
}
} }
} }
} }
#[cfg(test)] #[cfg(test)]
mod album_tests { mod album_tests {
use crate::utils; use crate::utils;
use icarus_models::album; use icarus_models::album;