diff --git a/Cargo.toml b/Cargo.toml index 17a7f14..b03a9ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ license = "MIT" [dependencies] serde = { version = "1.0.218", features = ["derive"] } -serde_json = "1.0.139" +serde_json = { version = "1.0.139" } +rand = { version = "0.9" } diff --git a/src/song.rs b/src/song.rs index 467a99d..1ff20f7 100644 --- a/src/song.rs +++ b/src/song.rs @@ -1,6 +1,10 @@ use std::default::Default; use std::io::Read; +use crate::constants; +use crate::types; + +use rand::Rng; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Deserialize, Serialize)] @@ -42,14 +46,14 @@ pub struct Song { pub data: Vec, #[serde(skip)] pub directory: String, - #[serde(skip)] - pub album_id: i32, - #[serde(skip)] - pub artist_id: i32, - #[serde(skip)] - pub genre_id: i32, - #[serde(skip)] - pub coverart_id: i32, + // #[serde(skip)] + // pub album_id: i32, + // #[serde(skip)] + // pub artist_id: i32, + // #[serde(skip)] + // pub genre_id: i32, + // #[serde(skip)] + // pub coverart_id: i32, } fn is_zero(num: &i32) -> bool { @@ -81,10 +85,10 @@ impl Default for Song { user_id: 0, data: Vec::new(), directory: String::new(), - album_id: 0, - artist_id: 0, - genre_id: 0, - coverart_id: 0, + // album_id: 0, + // artist_id: 0, + // genre_id: 0, + // coverart_id: 0, } } } @@ -149,6 +153,55 @@ impl Song { } } } + + pub fn generate_filename(&self, typ: types::types::Types, randomize: bool) -> String { + let mut filename: String = String::new(); + let filename_len = 10; + + let file_extension = match typ { + types::types::Types::DefaultMusicExtension => + { + String::from(constants::DEFAULTMUSICEXTENSION) + } + + types::types::Types::WavExtension => + { + String::from(constants::WAVEXTENSION) + } + types::types::Types::FlacExtension => + { + String::from(constants::FLACEXTENSION) + } + types::types::Types::MPThreeExtension => + { + String::from(constants::MPTHREEEXTENSION) + } + }; + + if randomize { + let some_chars: String = String::from("abcdefghij0123456789"); + let mut rng = rand::thread_rng(); + + for _i in 0..filename_len { + let random_number: i32 = rng.gen_range(0..=19); + let index = random_number as usize; + let rando_char = some_chars.chars().nth(index); + + match rando_char { + Some(c) => { + filename.push(c); + } + None => {} + }; + } + } else { + filename += "track-output"; + } + + filename += &file_extension; + + return filename; + } } mod embedded { @@ -258,14 +311,14 @@ mod embedded { pub data: Vec, #[serde(skip)] pub directory: String, - #[serde(skip)] - pub album_id: i32, - #[serde(skip)] - pub artist_id: i32, - #[serde(skip)] - pub genre_id: i32, - #[serde(skip)] - pub coverart_id: i32, + // #[serde(skip)] + // pub album_id: i32, + // #[serde(skip)] + // pub artist_id: i32, + // #[serde(skip)] + // pub genre_id: i32, + // #[serde(skip)] + // pub coverart_id: i32, } fn is_embed_zero(num: &i32) -> bool { @@ -297,10 +350,10 @@ mod embedded { user_id: 0, data: Vec::new(), directory: String::new(), - album_id: 0, - artist_id: 0, - genre_id: 0, - coverart_id: 0, + // album_id: 0, + // artist_id: 0, + // genre_id: 0, + // coverart_id: 0, } } } diff --git a/src/types.rs b/src/types.rs index 03881fb..1cc0995 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,6 @@ - -mod types { +pub mod types { pub enum Types { + DefaultMusicExtension, WavExtension, FlacExtension, MPThreeExtension,