Compare commits
5 Commits
v0.6.0-60-
...
v0.6.5-65-
Author | SHA1 | Date | |
---|---|---|---|
e3ca2c5781 | |||
440caca7c2 | |||
0c0b4ba7ca | |||
c263cedf28 | |||
0637a9432e |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -142,7 +142,7 @@ checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "icarus_models"
|
name = "icarus_models"
|
||||||
version = "0.6.0"
|
version = "0.6.5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"josekit",
|
"josekit",
|
||||||
"rand",
|
"rand",
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "icarus_models"
|
name = "icarus_models"
|
||||||
version = "0.6.0"
|
version = "0.6.5"
|
||||||
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"
|
||||||
|
@@ -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)]
|
||||||
|
102
src/song.rs
102
src/song.rs
@@ -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,49 +93,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();
|
|
||||||
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> {
|
||||||
match self.song_path() {
|
match self.song_path() {
|
||||||
@@ -147,7 +107,61 @@ impl Song {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Add function to remove file from the filesystem
|
/// 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),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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 {
|
||||||
|
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
|
/// I/O operations for songs
|
||||||
|
@@ -107,7 +107,7 @@ 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);
|
||||||
|
|
||||||
@@ -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");
|
||||||
@@ -132,7 +132,12 @@ mod song_tests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
match song::io::copy_song(&song, &mut copied_song) {
|
match song::io::copy_song(&song, &mut copied_song) {
|
||||||
Ok(_) => {}
|
Ok(_) => match copied_song.remove_from_filesystem() {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error: {err:?}")
|
||||||
|
}
|
||||||
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
assert!(false, "Error: {err:?}")
|
assert!(false, "Error: {err:?}")
|
||||||
}
|
}
|
||||||
@@ -142,7 +147,6 @@ mod song_tests {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod album_tests {
|
mod album_tests {
|
||||||
|
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
use icarus_models::album;
|
use icarus_models::album;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user