Compare commits
4 Commits
v0.8.0
...
v0.8.4-mai
| Author | SHA1 | Date | |
|---|---|---|---|
| 432078e3c2 | |||
| 0a27b8ccb1 | |||
| afc4ca21a2 | |||
| 02b6157e0d |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -149,7 +149,7 @@ checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "icarus_models"
|
name = "icarus_models"
|
||||||
version = "0.8.0"
|
version = "0.8.4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"josekit",
|
"josekit",
|
||||||
"rand",
|
"rand",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "icarus_models"
|
name = "icarus_models"
|
||||||
version = "0.8.0"
|
version = "0.8.4"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
rust-version = "1.90"
|
rust-version = "1.90"
|
||||||
description = "models used for the icarus project"
|
description = "models used for the icarus project"
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
A library containing commonly used models and functions that is used throughout
|
A library containing commonly used structs, functions, enums, constants and other code
|
||||||
icarus projects. This reduces the amount of duplicated code without a benefit.
|
that is used throughout the icarus projects. Code from this library serves as the model
|
||||||
|
for other projects in the icarus project.
|
||||||
|
|||||||
@@ -12,3 +12,9 @@ pub mod file_extensions {
|
|||||||
pub const PNGEXTENSION: &str = ".png";
|
pub const PNGEXTENSION: &str = ".png";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub mod error {
|
||||||
|
pub const DIRECTORY_NOT_INITIALIZED: &str = "Directory has not been initialized";
|
||||||
|
pub const FILENAME_NOT_INITIALIZED: &str = "Filename has not bee initialized";
|
||||||
|
pub const LAST_CHARACTER_IN_DIRECTORY: &str = "Could not access last character of directory";
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use rand::Rng;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize, Serialize, utoipa::ToSchema)]
|
const FILENAME_LENGTH: i32 = 16;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, utoipa::ToSchema)]
|
||||||
pub struct CoverArt {
|
pub struct CoverArt {
|
||||||
pub id: uuid::Uuid,
|
pub id: uuid::Uuid,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
@@ -15,11 +17,9 @@ pub struct CoverArt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod init {
|
pub mod init {
|
||||||
use super::CoverArt;
|
|
||||||
|
|
||||||
/// Initializes the CoverArt with just the directory and filename
|
/// Initializes the CoverArt with just the directory and filename
|
||||||
pub fn init_coverart_dir_and_filename(directory: &str, filename: &str) -> CoverArt {
|
pub fn init_coverart_dir_and_filename(directory: &str, filename: &str) -> super::CoverArt {
|
||||||
CoverArt {
|
super::CoverArt {
|
||||||
directory: String::from(directory),
|
directory: String::from(directory),
|
||||||
filename: String::from(filename),
|
filename: String::from(filename),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@@ -65,28 +65,65 @@ impl CoverArt {
|
|||||||
/// Gets the path of the CoverArt
|
/// Gets the path of the CoverArt
|
||||||
pub fn get_path(&self) -> Result<String, std::io::Error> {
|
pub fn get_path(&self) -> Result<String, std::io::Error> {
|
||||||
if self.directory.is_empty() {
|
if self.directory.is_empty() {
|
||||||
return Err(std::io::Error::other("Directory has not been initialized"));
|
return Err(std::io::Error::other(
|
||||||
|
crate::constants::error::DIRECTORY_NOT_INITIALIZED,
|
||||||
|
));
|
||||||
} else if self.filename.is_empty() {
|
} else if self.filename.is_empty() {
|
||||||
return Err(std::io::Error::other("Filename has not bee initialized"));
|
return Err(std::io::Error::other(
|
||||||
|
crate::constants::error::FILENAME_NOT_INITIALIZED,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let directory = &self.directory;
|
let directory = &self.directory;
|
||||||
let last_index = directory.len() - 1;
|
let last_index = directory.len() - 1;
|
||||||
|
|
||||||
if let Some(character) = directory.chars().nth(last_index) {
|
match crate::util::concatenate_path(directory, &self.filename, last_index) {
|
||||||
let buffer = if character != '/' {
|
Ok(path) => Ok(path),
|
||||||
directory.clone() + "/"
|
Err(err) => Err(err),
|
||||||
} else {
|
}
|
||||||
directory.clone()
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates filename for a CoverArt
|
||||||
|
pub fn generate_filename(
|
||||||
|
typ: crate::types::CoverArtTypes,
|
||||||
|
randomize: bool,
|
||||||
|
) -> Result<String, std::io::Error> {
|
||||||
|
let file_extension = match typ {
|
||||||
|
crate::types::CoverArtTypes::PngExtension => {
|
||||||
|
String::from(crate::constants::file_extensions::image::PNGEXTENSION)
|
||||||
|
}
|
||||||
|
crate::types::CoverArtTypes::JpegExtension => {
|
||||||
|
String::from(crate::constants::file_extensions::image::JPEGEXTENSION)
|
||||||
|
}
|
||||||
|
crate::types::CoverArtTypes::JpgExtension => {
|
||||||
|
String::from(crate::constants::file_extensions::image::JPGEXTENSION)
|
||||||
|
}
|
||||||
|
crate::types::CoverArtTypes::None => {
|
||||||
|
return Err(std::io::Error::other("Unsupported CoverArtTypes"));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(buffer + &self.filename.clone())
|
let filename: String = if randomize {
|
||||||
|
let mut filename: String = String::from("coverart-");
|
||||||
|
let some_chars: String = String::from("abcdefghij0123456789");
|
||||||
|
let some_chars_length = some_chars.len();
|
||||||
|
let mut rng = rand::rng();
|
||||||
|
|
||||||
|
for _ in 0..FILENAME_LENGTH {
|
||||||
|
let index = rng.random_range(0..=some_chars_length);
|
||||||
|
let rando_char = some_chars.chars().nth(index);
|
||||||
|
|
||||||
|
if let Some(c) = rando_char {
|
||||||
|
filename.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
format!("{filename}{file_extension}")
|
||||||
} else {
|
} else {
|
||||||
Err(std::io::Error::other(
|
format!("coverart-output{file_extension}")
|
||||||
"Could not access last character of directory",
|
};
|
||||||
))
|
|
||||||
}
|
Ok(filename)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod io {
|
pub mod io {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ pub mod song;
|
|||||||
pub mod token;
|
pub mod token;
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
pub mod util;
|
||||||
|
|
||||||
pub mod init {
|
pub mod init {
|
||||||
pub fn is_id_valid(num: &i32) -> bool {
|
pub fn is_id_valid(num: &i32) -> bool {
|
||||||
|
|||||||
49
src/song.rs
49
src/song.rs
@@ -69,28 +69,24 @@ impl Song {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the path of a Song
|
||||||
pub fn song_path(&self) -> Result<String, std::io::Error> {
|
pub fn song_path(&self) -> Result<String, std::io::Error> {
|
||||||
if self.directory.is_empty() {
|
if self.directory.is_empty() {
|
||||||
return Err(std::io::Error::other("Directory has not been initialized"));
|
return Err(std::io::Error::other(
|
||||||
|
crate::constants::error::DIRECTORY_NOT_INITIALIZED,
|
||||||
|
));
|
||||||
} else if self.filename.is_empty() {
|
} else if self.filename.is_empty() {
|
||||||
return Err(std::io::Error::other("Filename has not bee initialized"));
|
return Err(std::io::Error::other(
|
||||||
|
crate::constants::error::FILENAME_NOT_INITIALIZED,
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let directory = &self.directory;
|
let directory = &self.directory;
|
||||||
let last_index = directory.len() - 1;
|
let last_index = directory.len() - 1;
|
||||||
|
|
||||||
if let Some(character) = directory.chars().nth(last_index) {
|
match crate::util::concatenate_path(directory, &self.filename, last_index) {
|
||||||
let buffer: String = if character != '/' {
|
Ok(path) => Ok(path),
|
||||||
directory.clone() + "/"
|
Err(err) => Err(err),
|
||||||
} else {
|
|
||||||
directory.clone()
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(buffer + &self.filename.clone())
|
|
||||||
} else {
|
|
||||||
Err(std::io::Error::other(
|
|
||||||
"Could not access last character of directory",
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,14 +125,15 @@ impl Song {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a filename. In order to save a song to the filesystem, the song must have
|
/// Generates a filename. In order to save a song to the filesystem
|
||||||
/// a directory and filename
|
pub fn generate_filename(
|
||||||
pub fn generate_filename(typ: types::MusicTypes, randomize: bool) -> String {
|
typ: types::MusicTypes,
|
||||||
|
randomize: bool,
|
||||||
|
) -> Result<String, std::io::Error> {
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
types::MusicTypes::WavExtension => {
|
types::MusicTypes::WavExtension => {
|
||||||
String::from(constants::file_extensions::audio::WAVEXTENSION)
|
String::from(constants::file_extensions::audio::WAVEXTENSION)
|
||||||
}
|
}
|
||||||
@@ -146,25 +143,29 @@ pub fn generate_filename(typ: types::MusicTypes, randomize: bool) -> String {
|
|||||||
types::MusicTypes::MPThreeExtension => {
|
types::MusicTypes::MPThreeExtension => {
|
||||||
String::from(constants::file_extensions::audio::MPTHREEEXTENSION)
|
String::from(constants::file_extensions::audio::MPTHREEEXTENSION)
|
||||||
}
|
}
|
||||||
|
types::MusicTypes::None => return Err(std::io::Error::other("Unsupported MusicTypes")),
|
||||||
};
|
};
|
||||||
|
|
||||||
if randomize {
|
let filename: String = if randomize {
|
||||||
let mut filename: String = String::new();
|
let mut filename: String = String::from("track-");
|
||||||
let some_chars: String = String::from("abcdefghij0123456789");
|
let some_chars: String = String::from("abcdefghij0123456789");
|
||||||
|
let some_chars_length = some_chars.len();
|
||||||
let mut rng = rand::rng();
|
let mut rng = rand::rng();
|
||||||
|
|
||||||
for _ in 0..FILENAME_LENGTH {
|
for _ in 0..FILENAME_LENGTH {
|
||||||
let index = rng.random_range(0..=19);
|
let index = rng.random_range(0..=some_chars_length);
|
||||||
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
|
format!("{filename}{file_extension}")
|
||||||
} else {
|
} else {
|
||||||
"track-output".to_string() + &file_extension
|
format!("track-output{file_extension}")
|
||||||
}
|
};
|
||||||
|
|
||||||
|
Ok(filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// I/O operations for songs
|
/// I/O operations for songs
|
||||||
|
|||||||
10
src/types.rs
10
src/types.rs
@@ -1,6 +1,16 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
pub enum MusicTypes {
|
pub enum MusicTypes {
|
||||||
DefaultMusicExtension,
|
DefaultMusicExtension,
|
||||||
WavExtension,
|
WavExtension,
|
||||||
FlacExtension,
|
FlacExtension,
|
||||||
MPThreeExtension,
|
MPThreeExtension,
|
||||||
|
None,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum CoverArtTypes {
|
||||||
|
PngExtension,
|
||||||
|
JpegExtension,
|
||||||
|
JpgExtension,
|
||||||
|
None,
|
||||||
}
|
}
|
||||||
|
|||||||
19
src/util/mod.rs
Normal file
19
src/util/mod.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
pub fn concatenate_path(
|
||||||
|
directory: &str,
|
||||||
|
filename: &str,
|
||||||
|
last_index: usize,
|
||||||
|
) -> Result<String, std::io::Error> {
|
||||||
|
if let Some(character) = directory.chars().nth(last_index) {
|
||||||
|
let buffer: String = if character != '/' {
|
||||||
|
format!("{directory}/")
|
||||||
|
} else {
|
||||||
|
String::from(directory)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(format!("{buffer}{filename}"))
|
||||||
|
} else {
|
||||||
|
Err(std::io::Error::other(
|
||||||
|
crate::constants::error::LAST_CHARACTER_IN_DIRECTORY,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -107,12 +107,21 @@ 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);
|
match song::generate_filename(types::MusicTypes::FlacExtension, true) {
|
||||||
|
Ok(filename) => {
|
||||||
|
song_cpy.filename = filename;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
assert!(false, "Error generatig filename: {err:?}");
|
||||||
|
}
|
||||||
|
};
|
||||||
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) {
|
match song::io::copy_song(&song, &mut song_cpy) {
|
||||||
Ok(_) => {}
|
Ok(_) => {
|
||||||
|
println!("Song copied");
|
||||||
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
assert!(false, "Error copying song: Error: {err:?}")
|
assert!(false, "Error copying song: Error: {err:?}")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user